0

I have to write a program for a Boggle-like game, and I currently have it check each letter below the current one to see if they make a word. So for a board like so:

W O Y R
F U M F
H T R V
I G S W

The only word it would find is "OUT" going from top to bottom. When it finds part of a word it puts that letter into a string and sets it to null so it won't use a letter twice in the same word (the full algorithm has to be able to search in multiple directions). I use a stack to keep track of the coordinates of the letters I've used so I can backtrack, and every time I pop the stack I take the last letter of the string and put it back into the board in its original position. But the issue is that if multiple letters are removed, it places them all in the same index, overwriting the previous one. So in the case of "OUT" the board ends up looking like this after replacing the three letters:

W null Y R
F null M F
H  O   R V
I  G   S W

I've gone through my code and tried rewriting it twice but it always does this. Do you have any insight as to why this is happening?

private void checkNeighbors(LetterCoor center){
    String check = out;
    while (!path.empty()){
        if(center.getDirec()==0){//If the direction to check is down
            System.out.println("Bottom");
            if((center.getRow())+1<sideLength && board[(center.getRow())+1][center.getCol()]!=null){//makes sure the space below is !null and !out of bounds
                check+=board[center.getRow()+1][center.getCol()];
                System.out.println("Checking " + check);
                if(isValidWord(check)){//checks if string is part of the lexicon
                    center.nextNeighbor();
                    board[center.getRow()+1][center.getCol()]=null;
                    center = new LetterCoor(center.getRow()+1, center.getCol(), 0);
                    System.out.println("push " + check.substring(check.length()-1));
                    path.push(center);
                    out=check;

                }
                else{
                    center=(LetterCoor) path.pop();
                    center.nextNeighbor();
                    path.push(center);
                }
            }//end of null if
            else{
                System.out.println("Null or end of board");
                center=(LetterCoor) path.pop();
                center.nextNeighbor();
                path.push(center);
            }
        }//end of direc 0 if
        else{
            System.out.println("pop " + out.substring(out.length()-1,out.length()));
            center=(LetterCoor) path.pop();
            center.nextNeighbor();
            board[center.getRow()][center.getCol()]=out.substring(out.length()-1,out.length());
            out=out.substring(0,out.length()-1);
            if (center.getDirec()<1){
                path.push(center);
            }
        }
        System.out.println("Current string is " + out);
    }//end of while loop
}

If you need any clarification of my code please let me know.

Also, as clarification the LeterCoor object stores three ints. The first is the row index of the letter, the second is the column index and the third indicates which direction it is searching in (0=down, 1=down right, 2=right, etc)

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
CheeseCoder
  • 701
  • 1
  • 7
  • 11
  • can you make your post more readable? if u get any error , plz post it up plz – Kick Buttowski Jul 14 '14 at 02:35
  • @KickButtowski The most I could do is put in some comments to clarify or explain in depth how this code runs. No actual errors are generated, it just gives an improper output. – CheeseCoder Jul 14 '14 at 02:38
  • so you logical errors – Kick Buttowski Jul 14 '14 at 02:39
  • It appears to be logical, but I simply can't seem to find the error, nor can the people I'm working with. The code scans through the board as it should and properly replaces the letter as long as there is only one to be replaced. If there's more than one then it puts it all in the same spot yet afterward continues to scan as it should – CheeseCoder Jul 14 '14 at 02:44
  • Consider replacing your direction with an enum: `enum Direction{UP(-1,0), DOWN(1,0), LEFT(0,-1), RIGHT(0,1), UP_LEFT(-1,-1), UP_RIGHT(-1,1), DOWN_RIGHT(1,1), DOWN_LEFT(1,-1);};`, where the numbers are [constructor parameters](http://stackoverflow.com/questions/24706576/rank-of-cards-in-a-game/24706671#24706671) indicating the increments for moving to the next cell (such as `DOWN_RIGHT` is 1 down, 1 to the right). – aliteralmind Jul 14 '14 at 02:58
  • @aliteralmind I see how that would make it more readable for other people, but would that do anything in terms of the overwriting? – CheeseCoder Jul 14 '14 at 03:14
  • No. Which is why I wrote it as a comment, and not an answer. Sorry I didn't mention that it's a secondary comment. – aliteralmind Jul 14 '14 at 04:02

1 Answers1

0

I ended up coming across the solution on my own. The issue was with my LetterCoor object. Eclipse required the variables be set as static since I had the object class in a separate file, so when I updated the coordinate data in one LetterCoor object, it set the data for every LetterCoor object to that coordinate. I resolved this by moving the object class into the same file as this class and removing the static declaration from the variables.

CheeseCoder
  • 701
  • 1
  • 7
  • 11