int main (int argc, char* argv[]) { BogglePlayer * p = new BogglePlayer(); //test getAllValid set<string> lex; string line; std::ifstream fword("boglex.txt",ios::binary); while(fword.good()){ std::getline(fword,line); lex.insert(line); } fword.close(); p->buildLexicon(lex); // std::ifstream f("brd.txt",ios::binary); // // int row=0; // // int col=0; // std::getline(f,line); // //row=std::stoi(line); // std::getline(f,line); // //col=std::stoi(line); // string brd[20][23]; // for(int i=0;i<20;i++){ // for(int j=0;j<23;j++){ // std::getline(f,line); // brd[i][j]=line; // } // } // f.close(); // string* bd[20]; // for(int i=0;i<20;i++){ // bd[i]=brd[i]; // } // p->setBoard(20,23,bd); // for(int i=0;i<20;i++){ // for(int j=0;j<23;j++){ // cout << p->board[i*23+j].letter<<" "; // } // cout<<endl; // } // std::vector<int> v; // v= p->isOnBoard("acidified"); // for(int i=0;i<v.size();i++){ // cout<<v[i]<<endl; // } //cout << "size " << p->isOnBoard("acidified").size() <<endl; // string wordA("a"); // string wordX("x"); // lex.insert(wordA); // lex.insert("aba"); string row0[] = {"w"}; string row1[] = {"e"}; string row2[] = {"r"}; string row3[] = {"e"}; string* board[] = {row0,row1,row2,row3}; set<string> words; // vector<int> locations; //p->buildLexicon(lex); //cout << "build" << endl; p->setBoard(4,1,board); // // for(int i=0;i<4;i++){ // // cout << p->board[i].letter << endl; // // } // if(p->isOnBoard("aba").size() == 3){ // cout << "found" << endl; // } // if(p->isInLexicon(wordX)) { // std::cerr << "Apparent problem with isInLexicon #1." << std::endl; // return -1; // } // if(!p->isInLexicon(wordA)) { // std::cerr << "Apparent problem with isInLexicon #2." << std::endl; // return -1; // } // if(p->isOnBoard(wordA).size() != 1) { // std::cerr << "Apparent problem with isOnBoard #1." << std::endl; // return -1; // } // locations.clear(); // locations = p->isOnBoard(wordA); // if(locations.size() != 1 || locations[0] != 3) { // std::cerr << "Apparent problem with isOnBoard #2." << std::endl; // return -1; // } // if(!p->getAllValidWords(0,&words)) { // std::cerr << "Apparent problem with getAllValidWords #1." << std::endl; // return -1; // } // if(words.size() != 1 || words.count(wordA) != 1) { // std::cerr << "Apparent problem with getAllValidWords #2." << std::endl; // return -1; // } //set<string> words; p->getAllValidWords(0,&words); int j=0; for (std::set<string>::iterator it = words.begin() ; it != words.end(); ++it){ j++; cout<<j<< " valid words "<<*it<<endl; } cout<<"total number"<<words.size()<<endl; delete p; return 0; }
void testSpeed(){ cout << "*** TESTING WORD RETRIEVAL ( getAllValidWords() ) ***" << endl; BogglePlayer bp; // Initialize board cout << "Initializing board... "; std::ifstream brd ("brd.txt"); if(!brd){ cerr << "brd.txt does not exist. Aborting test." << endl; return; } unsigned int rows, cols; brd >> rows; brd >> cols; std::string** sbrd = new std::string*[rows]; for (unsigned int r=0;r<rows;r++) { sbrd[r] = new std::string[cols]; for (unsigned int c=0;c<cols;c++) { brd >> sbrd[r][c]; } } bp.setBoard(rows, cols, sbrd); brd.close(); cout << "[DONE]" << endl; // Initialize lexicon cout << "Initializing lexicon... "; std::set<std::string> word_list; std::ifstream dict ("lex.txt"); if(!dict){ cerr << "lex.txt does not exist. Aborting test." << endl; return; } while (!dict.eof()) { std::string tmp; dict >> tmp; word_list.insert(tmp); } bp.buildLexicon(word_list); dict.close(); cout << "[DONE]" << endl; // Time word retrieval cout << "Timing word retrieval using getAllValidWords(): "; std::set<std::string> words_found; clock_t start = clock(); bool result = bp.getAllValidWords(2, &words_found); clock_t end = clock(); float runtime = (float)(end-start) / CLOCKS_PER_SEC; cout << "Found " << words_found.size() << "/5450 words in " << runtime << " seconds." << endl; cout << "Return value when setBoard() and buildLexicon() previously called is: " << (result ? "true" : "false") << endl; cout << "getAllValidWords() return value when only buildLexicon() called: "; BogglePlayer bp2; bp2.buildLexicon(word_list); std::set<std::string> words_found2; result = bp2.getAllValidWords(2, &words_found2); cout << (result ? "true" : "false") << endl; cout << "getAllValidWords() return value when only setBoard() called: "; BogglePlayer bp3; bp3.setBoard(rows, cols, sbrd); std::set<std::string> words_found3; result = bp3.getAllValidWords(2, &words_found3); cout << (result ? "true" : "false") << endl; cout << "getAllValidWords() return value when neither buildLexicon() nor setBoard() are called: "; BogglePlayer bp4; std::set<std::string> words_found4; result = bp4.getAllValidWords(2, &words_found4); cout << (result ? "true" : "false") << endl; cout << "getAllValidWords() return value when buildLexicon() called on nonempty lexicon, setBoard() called on empty board: "; BogglePlayer bp5; std::string** empty_brd = new std::string*[0]; for (unsigned int r=0;r<0;r++) { empty_brd[r] = new std::string[0]; } bp5.setBoard(0,0,empty_brd); bp5.buildLexicon(word_list); std::set<std::string> words_found5; result = bp5.getAllValidWords(2, &words_found5); cout << (result ? "true" : "false"); cout << ", Words found: " << words_found5.size() << endl; cout << "getAllValidWords() return value when buildLexicon() called on empty lexicon, setBoard() called on nonempty board: "; BogglePlayer bp6; bp6.setBoard(rows, cols, sbrd); std::set<std::string> empty_wordlist; bp6.buildLexicon(empty_wordlist); std::set<std::string> words_found6; result = bp6.getAllValidWords(2, &words_found6); cout << (result ? "true" : "false"); cout << ", Words found: " << words_found6.size() << endl; cout << "getAllValidWords() return value when buildLexicon() called on empty lexicon, setBoard() called on empty board: "; BogglePlayer bp7; bp6.setBoard(0,0,empty_brd); bp6.buildLexicon(empty_wordlist); std::set<std::string> words_found7; result = bp6.getAllValidWords(2, &words_found7); cout << (result ? "true" : "false"); cout << ", Words found: " << words_found7.size() << endl; cout << "Cleaning up test... "; for (unsigned int r=0;r<rows;r++) delete[] sbrd[r]; delete[] sbrd; for(unsigned int r=0; r<0; r++) delete[] empty_brd[r]; delete[] empty_brd; cout << "[DONE]" << endl << endl; }