Vector<string> sloveBoggle(Grid<char> &grid, Point start, int depth, Vector<Point> &path, string &word, Lexicon &lex) { Vector<string> result; if (depth > MAX_LENGTH) { // greater than maxium length, return // return lex.containsWord(word); } else { //recursion to the element around the current element. int i = start.x; int j = start.y; if(lex.containsWord(word) && depth >= MIN_LENGTH) { result.add(word); } for(Direction dir = TOPLEFT; dir <= LEFT; dir++) { if(isBound(grid, i, j, dir)) { Point new_start = adjustPoint(start, dir); if(isContainPoint(path, new_start)) continue; string new_word = word + grid.getAt(new_start.x, new_start.y); if(!lex.containsPrefix(new_word)) continue; path.add(new_start); depth++; Vector<string> res = sloveBoggle(grid, new_start, depth, path, new_word, lex); result = concateVec(result, res); path.removeAt(path.size() - 1); depth--; } } } return result; }
int ListWords(const map<char,string> &keypads,string prex,string digitSequence,string alber,Lexicon &lex){ int count=0; if(digitSequence.empty()&&lex.containsPrefix(prex)){ if(lex.containsWord(prex)){ count++; cout<<prex<<endl; } for(unsigned int i=0;i<alber.size();i++){ string p=prex; p.push_back(alber[i]); count+=ListWords(keypads,p,digitSequence,alber,lex); } return count; } if(!digitSequence.empty()&&lex.containsPrefix(prex)){ string nextchar=(keypads.find(digitSequence[0]))->second; digitSequence.erase(digitSequence.begin()); for(unsigned int i=0;i<nextchar.size();i++){ string p=prex; p.push_back(nextchar[i]); count+=ListWords(keypads,p,digitSequence,alber,lex); } } return count; }
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; } } }
// checks lexicon and prints all words that begin with the given prefix void printValidWords(string prefix, Lexicon &lex){ string alpha = "abcdefghijklmnopqrstuvwxyz"; if(lex.containsWord(prefix)) cout << prefix << " "; if(lex.containsPrefix(prefix)){ for(int i = 0; i < alpha.length(); i++){ printValidWords(prefix + alpha.at(i), lex); } } }
/* * 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; }
/* * Function: CheckUserGuess * --------------------------- * Checks if the format of user guess meets all requirements of the boggle rules. * *@return string userGuess when function has confirmed that all requirements have been met. */ string CheckUserGuess(Lexicon wordList, Lexicon usedWords) { while (true) { //prompts until appropriate guess is received cout << "Enter a word: "; string userGuess = ConvertToUpperCase(GetLine()); if (userGuess == "") return ""; if (userGuess.length() < minWordLength) { //checks if minimum word length is met cout << "I'm sorry, but we have our standards." << endl; cout << "That word doesn't meet the minimum word length of 4." << endl; PlayNamedSound("whoops.wav"); } else if (!wordList.containsWord(userGuess)) { //checks if the guess is actually a word cout << "That's not a word! " << RandomizeResponse() << endl; PlayNamedSound("whoops.wav"); } else if (usedWords.containsWord(userGuess)) { //checks if the guess has already been used cout << "You've already guessed that! " << RandomizeResponse() << endl; PlayNamedSound("whoops.wav"); } else { return userGuess; } } }
/** * @brief Gets possible completions for a given word * @param word The word to check for completions * @param lex The lexicon to check completions against * @param words A set that will be filled with possible completions * * This function recursively checks for possible completions of a given * word, using the provided lexicon. */ void getCompletions(string word, Lexicon &lex, Set<string> &words) { if (lex.containsWord(word)) { words.add(word); } for (int i = 0; i < ALPHABET.size(); i++) { string newWord = word + ALPHABET[i]; if (lex.containsPrefix(newWord)) { getCompletions(newWord, lex, words); } } }
bool verifyGuess(string &guess, Grid<char> &grid, Lexicon &lex) { guess = ConvertToUpperCase(guess); if(!lex.containsWord(guess)) return false; Vector<Point> starts = getStartPoints(guess[0], grid); Vector<string> humVec,humVecTotal; string word = ""; Vector<Point> path; for (int i = 0; i < starts.size(); i++) { Point start = starts.getAt(i); path.clear(); path.add(start); word = ""; word += grid.getAt(start.x, start.y); humVec = sloveBoggle(grid, start, 1, path, word, lex); humVecTotal = concateVec(humVecTotal, humVec); } return isContainWord(humVecTotal, guess); }
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 } } } }
bool validWord(string s, Lexicon &lex) { return lex.containsWord(s); }