1

This is a C Assignment for a Boggle Board.

The assignment is to search a 4x4 board of characters for words in the dictionary by starting at a tile and moving up, down, left, right, or diagonally at any point. My program "jumps" from one point to another, spelling words from letters that aren't adjacent, and adding words many times.

void goToNextLetter(struct trie* dictionary, char boggleBoard[SIDELENGTH][SIDELENGTH], int usedLetters[SIDELENGTH][SIDELENGTH], char word[MAX], int row, int column){
    int i, rowTemp, colTemp;
    if (isPrefix(dictionary, word, 0)){
        if (isInDictionary(dictionary, word, 0)){
            printf("%s\n", word);
        }
        word[strlen(word)]=boggleBoard[row][column];
        usedLetters[row][column]=1;
        //printf("%d\t%d\t%s\n", row, column, word);
        for (i=0; i<D_SIZE; i++){
            rowTemp=row+DY[i];
            colTemp=column+DX[i];
            if ((rowTemp<SIDELENGTH) && (rowTemp>=0) && (colTemp>=0) && (colTemp<SIDELENGTH) && (usedLetters[rowTemp][colTemp]==0)){
                goToNextLetter(dictionary, boggleBoard, usedLetters, word, rowTemp, colTemp);
            }
        }
        //remove the last letter of word
        word[strlen(word)-1] = 0;
        usedLetters[row][column]=0;
    }
}
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • 1
    Debugging tip: add an extra parameter `int depth`, which is 0 in the initial call, and where you pass `depth + 1` to the recursive call. At the top of the function, print `depth` spaces or tabs followed by `row` and `column`. That way, you can see how your recursion moves around in the grid, which will be helpful in tracking down erroneous movements. – Aasmund Eldhuset Nov 22 '15 at 01:10
  • 1
    after `word[strlen(word)]=boggleBoard[row][column];` , `word` is not null-terminated but you go on to pass it to a function and use `strlen(word)` – M.M Nov 22 '15 at 21:21

2 Answers2

2

You don't have enough code posted here to actually test this, but it looks to me like your duplicates problem stems from the fact that you check if a word is in the dictionary before you add the letter from the current square to the current word. The way that the code is written now, it looks like each matched word will be "added" (printed?) D_SIZE times...

In other words, you should move the update word/usedLetters code outside of the conditionals:

void goToNextLetter(struct trie* dictionary, char boggleBoard[SIDELENGTH][SIDELENGTH], int usedLetters[SIDELENGTH][SIDELENGTH], char word[MAX], int row, int column){
    int i, rowTemp, colTemp;
    /*** START HOISTED CODE ***/
    word[strlen(word)]=boggleBoard[row][column];
    usedLetters[row][column]=1;
    /*** END HOISTED CODE ***/
    if (isPrefix(dictionary, word, 0)){
        if (isInDictionary(dictionary, word, 0)){
            printf("%s\n", word);
        }
        //printf("%d\t%d\t%s\n", row, column, word);
        for (i=0; i<D_SIZE; i++){
            rowTemp=row+DY[i];
            colTemp=column+DX[i];
            if ((rowTemp<SIDELENGTH) && (rowTemp>=0) && (colTemp>=0) && (colTemp<SIDELENGTH) && (usedLetters[rowTemp][colTemp]==0)){
                goToNextLetter(dictionary, boggleBoard, usedLetters, word, rowTemp, colTemp);
            }
        }
    }
    /*** START HOISTED CODE ***/
    //remove the last letter of word
    word[strlen(word)-1] = 0;
    usedLetters[row][column]=0;
    /*** END HOISTED CODE ***/
}

I don't see any obvious mistakes that would cause your "jumping" problem.

DaoWen
  • 32,589
  • 6
  • 74
  • 101
  • Thank you for your input! Would it help if I posted the rest of the code? I was afraid it would be too long. – Iryna Protasova Nov 22 '15 at 01:51
  • 1
    @IrynaProtasova - I think you should do what Aasmund suggested above to help debug your "jumping" problem. Posting the definitions of `DX` and `DY` might help. Posting a specific trace from your `printf("%d\t%d\t%s\n", row, column, word);` output—where you see the "jump" behavior—would also be a good diagnostic. – DaoWen Nov 22 '15 at 02:11
  • const int D_SIZE = 8; const int DX[] = {-1,-1,-1,0,0,1,1,1}; const int DY[] = {-1,0,1,-1,1,-1,0,1}; They're arrays I use to quickly access every location around a location. 0 0 mac 0 2 mas 0 1 a 0 0 ac 1 0 ac 1 0 0 0 c 0 2 s 0 2 s 0 1 sa 0 0 sac 1 0 sac 1 0 s 0 0 sc 3 0 r 2 0 ri 1 0 ri 0 0 ric 0 1 ria 0 2 rias 1 1 rim From board: case mopi stre napd – Iryna Protasova Nov 22 '15 at 03:32
0

Alright, I've figured it out. It was actually a problem with the rest of the program, though. I had each line of board (the parameter passed into the function) set to a length of SIDELENGTH+1, because I wanted to keep a space for the end string character, but I didn't need that. Actually, I'm not sure how that caused that problem, but if it works it's fine. Thank you for your help, everybody!