/* * Function: InputGuesses * ------------------------- * Allows the user to input guesses. Runs checks on user guess to see if it meets all boggle requirements. */ void InputGuesses(Grid<char> boggleBoard, Lexicon wordList, Lexicon &usedWords, Grid<bool> usedDice) { cout << endl << "Ok, take all the time you want and find all the words you can!" << endl; cout << "Signal that you're finished by entering an empty line." << endl << endl; while (true) { int row= 0, col = 0; string userGuess = CheckUserGuess(wordList, usedWords); //checks if format rules are met if (userGuess == "") break; //checks if sentinel of user hitting return has been occured while (true) { if (FindUserGuessOnBoard(boggleBoard, row, col, "", userGuess, usedDice)) { //checks if user guess can be created from boggleBoard usedWords.add(userGuess); //records user guess in lexicon of used words RecordWordForPlayer(userGuess, Human); UnhighlightBoard(boggleBoard); PlayNamedSound("excellent.wav"); break; } if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) { //checks if end of board has been reached without finding appropriate dice config cout << "You can't make that word! " << RandomizeResponse() << endl; PlayNamedSound("whoops.wav"); break; } } } }
/* * Function: FindWordsonBoard * ------------------------------ * Recursively searchs for all possible words that can be created from dice configuration of boggleBoard starting from * the specified location. * *@return bool true if the char arrangement found is an actual word. * false if all possibilities have been exhausted and no word is created. */ bool FindWordsOnBoard(Grid<char> boggleBoard, int row, int col, string partialWord, Grid<bool> usedDice, Lexicon wordList, Lexicon &usedWords) { int newRow, newCol; if (OffBoard(boggleBoard, row, col) || usedDice.getAt(row, col) == true) return false; partialWord += boggleBoard.getAt(row, col); //keeps track of current char arrangement if (!wordList.containsPrefix(partialWord)) return false; //checks if partialWord is a prefix of any word in wordList usedDice.setAt(row, col, true); //marks the dice as used if (wordList.containsWord(partialWord) && partialWord.length() >= minWordLength && !usedWords.containsWord(partialWord)) { //checks if partialWord is an actual word meeting the minimum length and has not been used by either the player or computer RecordWordForPlayer(partialWord, Computer); usedWords.add(partialWord); //adds the word found to list of used words } for(int i = 0; i < 8; i++) { //there are 8 possible paths the char arrangement can take FindPossiblePath(newRow, newCol, row, col, i); if (FindWordsOnBoard(boggleBoard, newRow, newCol, partialWord, usedDice, wordList, usedWords)) return true; } usedDice.setAt(row, col, false); //unmarks the dice as unused when current char configuration turns out fruitless return false; }
void FindAllWords(int row, int col, Grid<string> & board, Lexicon & lex, Set<string> & wordsSeen, string soFar, Vector<locationT> visited) { locationT here; here.numRow = row; here.numCol = col; visited.add(here); soFar += board(row,col); if (!lex.containsPrefix(soFar)) { //return if this is a dead end return; } if (lex.containsWord(soFar) && (!wordsSeen.contains(soFar)) && soFar.size() > 3) { RecordWordForPlayer(soFar, Computer); //found a word, so record it wordsSeen.add(soFar); } for (int i = 0; i < board.numRows(); i++) { for (int j = 0; j < board.numCols(); j++) { if (AreNeighbors(row, col, i, j) && NotDuplicated(i, j, visited)) { //recur on the rest of the puzzle for all FindAllWords(i, j, board, lex, wordsSeen, soFar, visited); //neighbors that haven't already been seen } } } }
void putWordOnBoard(Grid<string> & board, string word, Set<string> & wordsSeen) { wordsSeen.add(word); RecordWordForPlayer(word, Human); }