0
void scoreBoardHelper(Grid<char>& board, Lexicon& lex, Set<string>& visitedPrefixes, string current, GridLocation location, Set<string>& foundWords, Set<GridLocation> visitedTiles){
        if(lex.contains(current) && current.size()>3){
            foundWords.add(current);
        }
        if(current == ""){
            current += board[location];
            visitedTiles.add(location);
        }
        //check neighbors
        GridLocation neighbor = location;
        for(int i = 0; i<8; i++){
            switch (i) {
                case 0:
                    neighbor.col++;
                    break;
                case 1:
                    neighbor.col--;
                    break;
                case 2:
                    neighbor.row++;
                    break;
                case 3:
                    neighbor.row--;
                    break;
                case 4:
                    neighbor.col++;
                    neighbor.row++;
                    break;
                case 5:
                    neighbor.col--;
                    neighbor.row--;
                    break;
                case 6:
                    neighbor.col--;
                    neighbor.row++;
                    break;
                case 7:
                    neighbor.col++;
                    neighbor.row--;
                    break;
            }
            if(board.inBounds(neighbor) && board[neighbor]!=current[current.size()-2] && !visitedTiles.contains(neighbor) && lex.containsPrefix(current + board[neighbor]) && !visitedPrefixes.contains(current + board[neighbor])){
                visitedPrefixes.add(current + board[neighbor]);
                visitedTiles.add(neighbor);
                scoreBoardHelper(board, lex, visitedPrefixes, current + board[neighbor], neighbor, foundWords, visitedTiles);
            }
            neighbor = location;
        }
}
int scoreBoard(Grid<char>& board, Lexicon& lex) {
    Set<string> visitedPrefixes;
    Set<GridLocation> visitedTiles;
    string current = "";
    GridLocation location;
    Set<string> foundWords;
    for(int r=0;r<board.numRows();r++){
        location.row=r;
        for(int c=0;c<board.numCols();c++){
            location.col=c;
            scoreBoardHelper(board, lex, visitedPrefixes, current, location, foundWords, visitedTiles);
        }
    }
    int previousPoints = 0;
    for(string word : foundWords){
        cout<<"found word: "<<word<<endl;
        previousPoints += points(word);
    }
    return previousPoints;
}

I wrote these 2 functions but they don't seem to solve boggle. When I remove the !visitedTiles.contains(neighbor) check, it works as expected but it finds extra words that it should discard. When it has this check it narrows the solution unexpectedly and finds 2 words where it should find many more. Note: The points function works fine.

nunop
  • 2,036
  • 2
  • 20
  • 36
  • Well I'm not really sure (what is boggle?) but looks to be that you need to remove `neighbor` from `visitedTiles` after your recursive call, otherwise you aren't going to visit all possible paths. – john Aug 15 '20 at 11:03
  • it is a board game. You can check here http://www.fun-with-words.com/play_boggle.html. – Aybars1111 Aug 15 '20 at 11:13
  • that seems to work thanks but shouldn't I also be deleting location if current length is equal to 1 since I added it to the set too. – Aybars1111 Aug 15 '20 at 11:15
  • also there seems to be another bug which I cannot put my finger onto given this input {{'E','Z','R','R'}, {'O','H','I','O'}, {'N','J','I','H'}, {'Y','A','H','O'}}; it must produce { "HORIZON", "OHIA", "ORZO", "JOHN", "HAJI"} this output but it only finds john and haji – Aybars1111 Aug 15 '20 at 11:17

0 Answers0