// main.cpp in WordSearch // by DK, JP, CZ, and EM // 1-Nov-00 // This code was developed in class, and is not fully completed // (eg, it lacks sufficient comments, among other things). // It has no warranty of any kind, of course. // Use at your own risk. #include #include #include #include #include #include //////////////////////////////////////////////////////////////////// //------------------------Globals, Constants, and Other Neat Things //////////////////////////////////////////////////////////////////// const char* INPUT_FILENAME = "c:\\words.txt"; const char* OUTPUT_FILENAME = "c:\\wordsearch.txt"; const int MAX_ITERATIONS = 100; const int MAX_STRINGS = 1000; const int MAX_STRING_LENGTH = 30; const int MAX_ROWS = 100; const int MAX_COLS = 100; const int DIRS = 8; int g_rows; int g_cols; char g_strings[MAX_STRINGS][MAX_STRING_LENGTH]; int g_stringCount; char g_board[MAX_ROWS][MAX_COLS]; //////////////////////////////////////////////////////////////////// //------------------------Begin Random Code-----------------------// //////////////////////////////////////////////////////////////////// // returns a random between 0 and 1 double random1() { static bool seeded = false; if (seeded == false) { srand(time(NULL)); // seed the random # generator seeded = true; } return double(rand()) / RAND_MAX; } double random(double lo, double hi) { if (hi < lo) { double temp = hi; hi = lo; lo = temp; } double range = hi - lo; return lo + range * random1(); } int random(int lo, int hi) { int result; do { result = int(random(double(lo),double(hi+1))); } while (result == (hi+1)); return result; } //////////////////////////////////////////////////////////////////// //------------------------Begin Fairly UnRandom Code--------------// //////////////////////////////////////////////////////////////////// // @TODO: This is debug code, it will go away when we write our file stuff const int TEMP_SIZE = 6; char* g_strings_temp[TEMP_SIZE] = {"Four","Score","And","Seven","Years","Ago"}; void loadStrings() { g_stringCount = 0; FILE *fp; fp = fopen(INPUT_FILENAME,"r"); if (fp == NULL) { cout << "Could not open file '" << INPUT_FILENAME << "'. Sorry." << endl; cout << "Hit 'Enter' to exit." << endl; getchar(); exit(1); } while (!feof(fp)) { int i = 0; char c; // eat whitespace while (c=getc(fp),((c == '\n') || (c == '\t') || (c == ' '))) ; // do nothing // now put back the non-space if (feof(fp)) break; ungetc(c,fp); // now get the word while (c=getc(fp),((c != '\n') && (c != ' ') && (c != EOF))) g_strings[g_stringCount][i++] = toupper(c); // add null terminator g_strings[g_stringCount][i] = NULL; ++g_stringCount; } } void fillBoard() { // fill empty spaces on board with random characters int row, col, count = 0; for (row=0; row longest) longest = len; } int perLine = 80 / (longest + 5); for (i=0;i= g_rows) || (col2 < 0) || (col2 >= g_cols)) // we ran off the board, so fail by returning -1 return -1; // verify target board position is either // empty or matches this character in the string bool positionEmpty = (g_board[row2][col2] == NULL); bool positionMatches = (g_board[row2][col2] == string[i]); if ((positionEmpty == false) && (positionMatches == false)) // position is occupied and it doesn't match, // so we must fail and go home. return -1; // Increment the number of characters if we do not // have a match if (positionMatches == false) numChars++; // Place the character if (reallyPlace) g_board[row2][col2] = string[i]; } return numChars; } bool loadString(int i) { char* string = g_strings[i]; int col1 = random(0,g_cols-1); int row1 = random(0,g_rows-1); int dir1 = random(0,DIRS-1); bool foundOne = false; int bestCol, bestRow, bestDir, bestNumChars; int row, col, dir, rowDelta, colDelta, dirDelta; // Iterate over every row,col,dir to find best choice for (rowDelta=0; rowDelta= 0) && ((foundOne == false) || (bestNumChars > numChars))) { // We just found a better one than the one // we have so far, if any bestRow = row; bestCol = col; bestDir = dir; bestNumChars = numChars; foundOne = true; } } // Place it in the best location, if any if (foundOne) { placeString(string,bestRow,bestCol,bestDir,true); return true; } else return false; } bool loadBoard() { // Repeatedly try to loadBoard until maxIterations int iteration; for (iteration=0; iteration