bool WordIsValid(string word, Grid<string> & board, Lexicon & lex, Set<string> & wordsSeen) { if (word.length() < 4) { return false; } else if (wordsSeen.contains(word)) { //already seen the word return false; } else if (!lex.containsWord(word)) { //not a word according to the dictionary return false; } else { Vector<locationT> empty; Vector<Vector<locationT> > results; Findable(board, word, -1, -1, empty, results); if (results.size() == 0) { //no paths were found for the word, so word can't be found return false; } else { Vector<locationT> answer = results[0]; //at least one path was found, so return true and highlight the cubes for (int i = 0; i < answer.size(); i++) { HighlightCube(answer[i].numRow, answer[i].numCol, true); } Pause(.5); for (int i = 0; i < answer.size(); i++) { //then un-highlight the cubes HighlightCube(answer[i].numRow, answer[i].numCol, false); } return true; } } }
void clearHighlights(Grid<char> &board) { for (int row = 0; row < board.numRows(); row++) { for (int col = 0; col < board.numCols(); col++) { HighlightCube(row,col, false); } } }
/* * UnhighlightBoard * -------------------- * Unhighlights the graphic display of the boggle board. Ensures the highlighted word is * returned to normal. */ void UnhighlightBoard(Grid<char> boggleBoard) { int row = 0, col = 0; Pause(pauseTime); //creates highlighting animation of word while (true) { HighlightCube(row, col, false); if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) break; } }
/** * Function: FindUserGuessOnBoard * --------------------------------- * Recursively checks if user guess can be created from the dice configuration of boggleBoard starting from the specified * location. * *@return bool true if the user guess has been created from boggleBoard. * false if all possibilities have been exhausted and user guess has not been created. */ bool FindUserGuessOnBoard(Grid<char> boggleBoard, int row, int col, string partialWord, string userGuess, Grid<bool> usedDice) { int newRow, newCol; if (OffBoard(boggleBoard, row, col) || usedDice.getAt(row, col) == true) return false; partialWord += boggleBoard.getAt(row, col); //keeps track of the current char arrangement if (userGuess.substr(0, partialWord.length()) != partialWord) return false; //checks if partialWord is a prefix of the user guess usedDice.setAt(row, col, true); //marks the dice as used if (partialWord == userGuess) { //checks if user guess has been successfully created HighlightCube(row, col, true); return true; } for(int i = 0; i < 8; i++) { //there are 8 possible paths the char arrangement can take FindPossiblePath(newRow, newCol, row, col, i); if (FindUserGuessOnBoard(boggleBoard, newRow, newCol, partialWord, userGuess, usedDice)) { HighlightCube(row, col, true); return true; } } usedDice.setAt(row, col, false); //unmarks the dice as unused when current char configuration turns out fruitless return false; }
void highlightPath(Vector<pointT> &path) { for (int i = 0; i < path.size(); i++) { HighlightCube(path[i].row, path[i].col, true); } }