/******************************************************************************** * CS161 Sample Code ******************************************************************************** * Filename : PuzzleDemo.cpp * written By : Roger Webster, Ph.D. * : Department of Computer Science * : D&E Communications Inc.Computer Science Wing * : Caputo Hall * : Millersville University * Class : CS161 * - Info can be found on Internet World Wide Web server (WWW) site: * http://cs.millersville.edu/~webster/cs161/ *modifications: 07-07-2006 RWW made this ******************************************************************************/ #include #include #include using namespace std; string nothing; void printoutwords (string words[], int count) { cout << endl; cout << "dude there are " << count << " words. These are the words: " << endl; for (int i=0; i < count; i++) { cout << words[i] << endl; } cout << endl; } void printoutpuzzle (char wordsearch[15][15]) { cout << endl; cout << "dude this is the puzzle: " << endl; for (int rows=0;rows<15;rows=rows+1) { cout << endl; for (int cols=0;cols<15;cols=cols+1) cout << wordsearch[rows][cols]; } cout << endl; cout << endl; } void lookupbyrow(string aword, char wordsearch[15][15]) { int len; len = aword.length(); //cout << "dude looking for " << aword << " in the puzzle with len: " << len << endl; for (int rows=0;rows<15;rows=rows+1) { char ch; bool wordon=false; int index=0; ch = aword[index]; //cout << "dude looking for " << ch << " in the puzzle: " << endl; for (int cols=0;cols<15;cols=cols+1) if(ch == wordsearch[rows][cols]) { if (len == index+1) { cout << "found: " << " -> " << aword << " here: " << rows << " : " << cols-(len-1) << endl; } wordon = true; index = index + 1; ch = aword[index]; //cout << "dude looking for " << ch << " in the puzzle: " << endl; } else { if (wordon)//reset { wordon = false; index = 0; ch = aword[index]; //cout << "dude looking for " << ch << " in the puzzle: " << endl; } } } } void lookupbycol(string aword, char wordsearch[15][15]) { int len; len = aword.length(); //cout << "dude looking for " << aword << " in the puzzle with len: " << len << endl; for (int cols=0;cols<15;cols=cols+1) { char ch; bool wordon=false; int index=0; ch = aword[index]; //cout << "dude looking for " << ch << " in the puzzle: " << endl; for (int rows=0;rows<15;rows=rows+1) { if(ch == wordsearch[rows][cols]) { if (len == index+1) { cout << "found: " << " -> " << aword << " here: " << rows-(len-1) << " : " << cols << endl; } wordon = true; index = index + 1; ch = aword[index]; //cout << "dude looking for " << ch << " in the puzzle: " << endl; } else { if (wordon)//reset { wordon = false; index = 0; ch = aword[index]; //cout << "dude looking for " << ch << " in the puzzle: " << endl; } } } } } int readalldatain(string words[], char wordsearch[15][15]) { int count=0; ifstream wordfile, wordarray; string wordfilename="words.txt"; string wordarrayfilename="puzzle.txt"; wordfile.open(wordfilename.c_str()); if (wordfile.fail()) { cout << "can not open up that file: " << wordfilename << endl; } else cout << "file: " << wordfilename << " succesfully opened!!!" << endl; while (wordfile >> words[count]) { count = count + 1; } wordarray.open(wordarrayfilename.c_str()); if (wordarray.fail()) { cout << "can not open up that file: " << wordarrayfilename << endl; } else cout << "file: " << wordarrayfilename << " succesfully opened!!!" << endl; for (int rows=0;rows<15;rows=rows+1) for (int cols=0;cols<15;cols=cols+1) wordarray >> wordsearch[rows][cols]; return count; } int main(int argc, char** argv) { string words[30]; char wordsearch[15][15]; int count; int index; count = readalldatain(words, wordsearch); printoutwords (words, count); printoutpuzzle (wordsearch); for (int i=0;i> nothing; return 0; }