1

A question came up where I had to make a recursive boggle search. I've done similar programs (like wordsearches, mazes, connect4, etc.) but can't get this one to work.

I can't tell you exactly what errors pop up (as most of the time the word is present in the grid but it adamantly prompts me that it isn't)

The code randomly generates a table in which random capital letters (ASCII 65-90?) are converted to char.

The dictionary feature (a feature I commented out for testing purposes) checks whether or not the word input (for the boggle game) exists in the dictionary and responds accordingly.

I feel like I should explain what some of the variables do in more detail:

  • pos[][] is the table in which ill play boggle
  • posbackup[][] is the backup for when I don't find the word during the search (so the original values aren't lost)
  • dem is a randomly generated integer, which co-relates to the dimensions of the table (dem is basically a mispell of dim).

Here is the code:

package boggle;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
//ASCII conversion 65-90; Uppercase A-Z
public class Boggle {


 ArrayList<String> s = new ArrayList<>();
 char[][] pos ;
int max =90;
int min = 65;
int dem;
char[][] posbackup ;


public boolean Solve(int x, int y,String word, int c) throws IOException{





     if(x>=pos.length || x<0 || y>pos[0].length || y<0) return false; 



     if(pos[x][y]=='+') return false;

      if(word.length()==0 || c>word.length())return true;


     if(pos[x][y]!= word.charAt(0)) return false;
   pos[x][y]='+';
   word = word.substring(1, word.length());


    if(Solve(--x,y, word,c) == true) return true;
    if(Solve(x,--y,word,c) == true) return true;
   if(Solve(++x,y,word,c) == true) return true;
   if(Solve(x,++y,word,c) == true) return true;
    if(Solve(--x,--y, word,c) == true) return true;
    if(Solve(++x,--y,word,c) == true) return true;
   if(Solve(++x,--y,word,c) == true) return true;
   if(Solve(++x,++y,word,c) == true) return true;



      pos[x][y] = posbackup[x][y]; //Gives an out of bound error when removed it solves the table but not properly






       return false;



}







public void Output() throws IOException, FileNotFoundException, 
InterruptedException{

for(int c=0; c<dem; c++){
for(int d=0;d<dem;d++){
System.out.print(pos[c][d]+" ");

}
System.out.println();
}


System.out.println("Word found! Here is the path taken");
WordInput(1);


}
    public void Backup(){
    for(int c=0; c<dem; c++){
for(int d=0;d<dem;d++){
pos[c][d] = posbackup[c][d];

}

}


}

public void Position(String word) throws IOException, InterruptedException{
    System.out.println("Word found in the dictionary!");

    boolean match=false;
    System.out.println("Working...");
    Thread.sleep(1000);
    for(int x =0; x<pos.length;x++){
 for(int y =0; y<pos[0].length; y++){

 if(pos[x][y] == word.charAt(0)){

 if(Solve(x,y,word,0)==true)match = true;
 if(match== false)Backup();
 }




 }
 }
if(match == true) Output();
else{System.out.println("Word not found, try again"); WordInput(1);}
}


public void WordInput(int c) throws FileNotFoundException, IOException, InterruptedException{
 Scanner get = new Scanner(System.in);
 System.out.println("Enter a word from the dictionary");
 BufferedReader read = new BufferedReader(new FileReader("dictionary.txt"));
String line="";
if(c<=0){ 
while((line=read.readLine())!=null){
    line = line.toUpperCase();
    s.add(line);
c++;
}
Collections.sort(s); // tfw it actually worked

}
String word = get.next();
word = word.toUpperCase();
  // if(s.contains(word))
    {
    Position(word);                                                                  
// will be replaced with a binary search l8tr
    }
  /* else
    {

    System.out.println("Word not found in dictionary, please try again.");
    WordInput(c);
    }*/
    }
    public void PrintAdd( ) throws IOException, FileNotFoundException, 
 InterruptedException{
  Random r = new Random();
   int random;
   dem = r.nextInt((20-5)+1)+5;
   pos = new char[dem][dem];
   posbackup = new char[dem][dem];
    for(int c=0;c<dem;c++){
    for(int d=0; d<dem;d++){
   random = r.nextInt((max-min)+1)+min;
       pos[c][d] = ((char)random);
       posbackup[c][d] = pos[c][d];

   System.out.print(pos[c][d]+" ");
  }
    System.out.println();
    }

   WordInput(0);







}

 public static void main(String[] args) throws FileNotFoundException, 
IOException, InterruptedException {
       Boggle b = new Boggle();
       b.PrintAdd();

    }

}

It might be that my choice of ordering my methods is unconventional, but this is what I've got so far.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Marz
  • 11
  • 2
  • 3
    Welcome to Stack Overflow. There's a couple things I can suggest to improve your post. First, your question is unclear. "This doesn't work fix it for me" means nothing to almost all of us. If you can, clearly state what exactly the problem is. Secondly, if you can, try to extrapolate what exactly you've tried to do so far to remedy the problem and what routes have seemed most promising. – dddJewelsbbb May 03 '17 at 19:46
  • You can't tell us what the errors are? That makes it difficult to fix. You need to state clearly what output you're expecting, and what output you're actually getting. Otherwise, it's very unlikely that anyone here will be able to help you. – Dawood ibn Kareem May 03 '17 at 20:01

0 Answers0