#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0

    Question Boggle/word search isn't working


    I'm trying to make a boggle search algorithm, where a word is given, and returns whether it is found in a grid using boggle search rules.
    Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class LetterGrid
    {
    	private char[][] grid;
    
    	/**
    	 * Constructs a new LetterGrid using the given text file
    	 * 
    	 * @param fileName the name of the file with the grid
    	 */
    	public LetterGrid(String fileName)
    	{
    		// Since a FileNotFoundException may be thrown when opening a file
    		// we will include a try...catch block to handle this exception
    		try
    		{
    			Scanner inFile = new Scanner(new File(fileName));
    			ArrayList<String> lines = new ArrayList<String>();
    			while (inFile.hasNextLine())
    			{
    				lines.add(inFile.nextLine());
    			}
    			inFile.close();
    			grid = new char[lines.size()][];
    			int row = 0;
    			for (String next : lines)
    			{
    				grid[row++] = next.toCharArray();
    			}
    		} // end try
    		catch (FileNotFoundException e)
    		{
    			grid = new char[][] { { 'B', 'A', 'D', ' ' },
    					{ 'F', 'I', 'L', 'E' }, 
    					{ 'N', 'A', 'M', 'E' } };
    		} // end catch
    	}
    
    	public String toString()
    	{
    		StringBuilder gridStr = new StringBuilder(grid.length
    				* (grid[0].length + 1));
    		for (int row = 0; row < grid.length; row++)
    		{
    			for (int col = 0; col < grid[row].length; col++)
    			{
    				gridStr.append(grid[row][col]);
    			}
    			gridStr.append('\n');
    		}
    		return gridStr.toString();
    	}
    
    	public boolean wordSearch(String word) 
    	{		
    		for (int row = 0; row < grid.length; row++) 
    		{
    			for (int col = 0; col < grid[row].length; col++)
    			{
    				if (grid[row][col]==word.charAt(0))
    				{
    					for (int vertical=row-1;vertical<=row+1;vertical++)
    					{
    						for (int horizontal=col-1;horizontal<=col;horizontal++)
    						{
    							if (vertical!=row&&horizontal!=col)
    							{
    								int index = 1;
    								int count=1;
    								int y=vertical;
    								int x=horizontal;
    								while (y>=0&&y<grid.length&&x>=0&&x<grid[row].length&&grid[y][x]==word.charAt(index))
    								{
    									count++;
    									index++;
    									y+=vertical;
    									x+=horizontal;
    								}
    								if (count == word.length())	                
    									return true;
    							}
    						}
    					}
    				}
    			}				                
    		}
    		return false;
    	}	
    	public boolean boggleSearch(String word)
    	{
    		for (int col=0;col<grid.length;col++)
    			for (int row=0;row<grid.length;row++)
    				if (word.charAt(0)==grid[row][col])
    					return searchAround(word, col, row);
    		return true;
    	}
    	public boolean searchAround(String word, int col, int row)
    	{
    		// If row,col is off the grid, return false
    		if (col<0||col>=grid.length||row<0||row>=grid[col].length)
    			return false;
    		// If the charcter at the row,col is not equal to the first letter, return false
    		if (word.charAt(0)!=grid[col][row])
    			return false;
    		// If the length of the word is 1, return true
    		if (word.length()==1)
    			return true;		
    		String nextWord=word.substring(1);
    		// Mark character at row,col as used
    		Character.toLowerCase(grid[col][row]);
    		boolean found=searchAround(nextWord,col+1,row);		
    		found=searchAround(nextWord,col-1,row);	
    		found=searchAround(nextWord,col,row);
    		found=searchAround(nextWord,col,row+1);	
    		found=searchAround(nextWord,col,row-1);	
    		found=searchAround(nextWord,col,row);	
    		found=searchAround(nextWord,col+1,row+1);	
    		found=searchAround(nextWord,col-1,row-1);	
    		// Unmark character
    		//Character.toUpperCase(grid[col][row]);
    		return found;
    	}
    }
    Code:
    import java.io.FileNotFoundException;
    public class LetterGridMain 
    {
    	public static void main(String[] args) throws FileNotFoundException
    	{
    		LetterGrid grid=new LetterGrid("wordgrid.txt");
    		String word="apple";
    		System.out.println(grid.wordSearch(word.toUpperCase()));
    		System.out.println(grid.boggleSearch(word.toUpperCase()));
    	}
    }
    Code:
    APPLES
    NEEORA
    IAEANG
    WPAMCE
    INABIH
    KNOMEL
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Do you have any specific questions?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    As I said, it's just not working, I have no idea why
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    not working
    There are a lot of options for not working, could you be more specific:
    Does it compile without errors?
    Does it execute without errors?
    What does it do when it executes?
    What do you want it to do differently?

    Try debugging the code by adding lots of println statements that shows where the code is executing.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    I was thinking you could copy and paste into eclipse or whatever, I put everything there, it doesn't find the word, apple, even when it's the very first line.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Please answer the questions I asked if you want help. I'll try to help you understand what the code is doing if you will ask some specific questions about it. I'm not going to do your work for you. You need to learn how to debug your code. I use the println() statement when I debug. Many IDEs have interactive debugging which can be easier. Your choice.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    24
    Rep Power
    0
    ..

IMN logo majestic logo threadwatch logo seochat tools logo