#include #include #include #include #include #include #include // transform /** Mucho thanks to http://exceptional-code.blogspot.com/2012/02/solving-boggle-game-recursion-prefix.html **/ using namespace std; /** * GLOBAL VARIABLES FTW **/ const int N = 4; // board size char in[N * N + 1]; // max length of a word char prev[N * N + 1]; bool dp[N * N + 1][N][N]; char board[N][N]; bool visited[N][N]; // direction X-Y delta pairs for adjacent cells int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; int MIN_SCORE = 30; /** * Functions **/ // Separate a string based on a delimeter // This example splits it into a string and an int void SepString (string& myline, string& entry, int& score) { unsigned int first_comma = myline.find_first_of(";"); if (first_comma != string::npos) { entry = myline.substr(0,first_comma); string tmp_string = myline.substr(first_comma+1); score = atoi(tmp_string.c_str()); } } // Convert a string to uppercase void ToUpper(string& s) { transform(s.begin(), s.end(),s.begin(), ::toupper); } void print_grid() { for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { cout << board[i][j] << " "; } cout << endl; } cout << endl; } // Replace all occurrences of a substring with another string // via http://stackoverflow.com/questions/13446504/function-to-replace-specific-letters-wont-run-with-void-return-type string string_replace( string src, string const& target, string const& repl) { // handle error situations/trivial cases if (target.length() == 0) { // searching for a match to the empty string will result in // an infinite loop // it might make sense to throw an exception for this case return src; } if (src.length() == 0) { return src; // nothing to match against } size_t idx = 0; for (;;) { idx = src.find( target, idx); if (idx == string::npos) break; src.replace( idx, target.length(), repl); idx += repl.length(); } return src; } // Starting at r, c, build a grid with our word bool checkBoard(string word, int curIndex, int r, int c, int wordLen, vector& v) { if (curIndex == wordLen - 1) { //cout << "Returned TRUE!!" << endl; string brd = ""; for (int i=0; i= 0 && newR < N && newC >= 0 && newC < N && !visited[newR][newC]) { ++curIndex; visited[newR][newC] = true; board[newR][newC] = word.at(curIndex); ret = checkBoard(word, curIndex, newR, newC, wordLen, v); // if (ret) // break; --curIndex; if (curIndex < 0) {break;} visited[newR][newC] = false; //board[newR][newC] = ''; } } return ret; } void build_all_boards(const string word, vector& v) { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { memset(visited, false, sizeof(visited)); visited[i][j] = true; board[i][j] = word.at(0); checkBoard(word, 0, i, j, N*N, v); } } } /** * MAIN **/ int main() { // Read in the word list to various vectors //cout << "Reading in word list ..."; ifstream myfile ("xwordlist.txt"); vector four_letter_words, end_with_four_words, start_with_four_words; if (myfile.is_open()) { while (! myfile.eof() ) { string line; getline (myfile,line); string entry; int score; SepString(line,entry,score); // Skip unless we meet some minimum requirements if (score < MIN_SCORE) { continue; } string entry2 = string_replace(entry,"QU","2"); // Four-letter words if (entry2.length() == 4) {four_letter_words.push_back(entry2);} // Ends with if (entry2.length() >= 4) { end_with_four_words.push_back(entry2.substr(entry2.size()-4,4)); start_with_four_words.push_back(entry2.substr(0,4)); //cout << entry2.substr(entry2.size()-4,4) << endl; } } // end while myfile.close(); } // end if myfile is open // Sort so we can do binary search std::sort (four_letter_words.begin(), four_letter_words.end()); std::sort (end_with_four_words.begin(), end_with_four_words.end()); // Loop through the possible 17-letter words vector long_words; long_words.push_back("INCONSE2ENTIALLY"); long_words.push_back("2ADRICENTENNIALS"); long_words.push_back("SES2ICENTENNIALS"); // 17-letter words that aren't technically possible in Boggle long_words.push_back("AC2AINTANCESHIPS"); long_words.push_back("AC2ISITIVENESSES"); long_words.push_back("CONSE2ENTIALNESS"); long_words.push_back("COUNTER2ESTIONED"); long_words.push_back("DISE2ILIBRATIONS"); long_words.push_back("DIS2ALIFICATIONS"); long_words.push_back("IN2ISITIVENESSES"); long_words.push_back("PICTURES2ENESSES"); long_words.push_back("PRE2ALIFICATIONS"); long_words.push_back("2ARRELSOMENESSES"); long_words.push_back("2ATERCENTENARIES"); long_words.push_back("2ATTUORDECILLION"); long_words.push_back("SES2ICENTENARIES"); //long_words = sixteen_letter_words; for(std::vector::iterator lw_it = long_words.begin(); lw_it != long_words.end(); ++lw_it) { string long_word = *lw_it; cout << "*** " << long_word << " ***" << endl; // Set up all boards // Ideally we'd only do this once, but the program is fairly fast as is. vector allBoards; build_all_boards(long_word, allBoards); //cout << allBoards.size() << endl; // 343184 for (std::vector::iterator ab_it = allBoards.begin(); ab_it != allBoards.end(); ++ab_it) { //cout << *ab_it << endl; // Set up the actual board for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { board[i][j] = (*ab_it).at(j + 4 * i);; } } //print_grid(); // Check that the board consists of decent crossword words. // ACROSS WORDS string xword_word = ""; bool verified = true; int ctr = 16; int cutoff = 1; for (int i=0; i<4; i++) { xword_word = ""; for (int j=0; j<4; j++) {xword_word += board[i][j];} if (i < cutoff && !binary_search(end_with_four_words.begin(), end_with_four_words.end(), xword_word)) { verified = false; ctr--; } if (i >= cutoff && !binary_search(four_letter_words.begin(), four_letter_words.end(), xword_word)) { verified = false; ctr--; } } // end ACROSS //if (!verified) {continue;} // DOWN WORDS for (int i=0; i<4; i++) { xword_word = ""; for (int j=0; j<4; j++) {xword_word += board[j][i];} if (i < cutoff && !binary_search(end_with_four_words.begin(), end_with_four_words.end(), xword_word)) { verified = false; ctr--; } if (i >= cutoff && !binary_search(four_letter_words.begin(), four_letter_words.end(), xword_word)) { verified = false; ctr--; } } // end DOWN //cout << ctr << endl; // if ( ctr == 15 ) // { // cout << "This grid is very close." << endl; // print_grid(); // } if (verified) { // We have a match! Print the board. //cout << "We have a good board!" << endl; print_grid(); } } // end for all boards } // end for long_words return 0; }