//------------------------------------------------------------------- // File :board.C // Author :Alicia Thorsen // Course :CS1129 // // Functions which handle the Boggle board. //------------------------------------------------------------------- #include "board.h" #include #include #include #include #include #include using namespace std; //------------------------------------------------------------------- // Precondition :None // Postcondition :A random Boggle board is generated //------------------------------------------------------------------- void getBoard(char board[][SIZE]) { //File containing the letters on each die const char filename[] = "dice.txt"; char temp; int row, col, randNum, k = 0; ifstream inputFile; //# of faces on a die const int sides = 6; //Array containing all dice char dice[SIZE * SIZE][sides]; //Open file inputFile.open(filename); //Check for error if (inputFile.fail()) { cout << "Could not read from file " << filename << "! "; cout << "Aborting." << endl; exit(1); } //Read letters on the side of each die for (int i = 0; i < (SIZE * SIZE); i++) for (int j = 0; j < sides; j++) inputFile >> dice[i][j]; inputFile.close(); //Randomly choose letters from each die for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) { randNum = rand() % sides; board[i][j] = tolower(dice[k][randNum]); k++; } //Shake up board for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) { row = rand() % SIZE; col = rand() % SIZE; temp = board[i][j]; board[i][j] = board[row][col]; board[row][col] = temp; } }//end getBoard() //------------------------------------------------------------------- // Precondition :board contains a Boggle board // Postcondition :Boggle board is displayed //------------------------------------------------------------------- void displayBoard(char board[][SIZE]) { char c; cout << "\n. - . - . - . - .\n"; for (int i = 0; i < SIZE; i++) { cout << "| "; for (int j = 0; j < SIZE; j++) { c = toupper(board[i][j]); //Display Q and U on the same die face if (c == 'Q') cout << "QU | "; else cout << c << " | "; } cout << "\n. - . - . - . - .\n"; } }//end displayBoard() //------------------------------------------------------------------- // Precondition :None // Postcondition :Boggle logo is displayed //------------------------------------------------------------------- void showLogo() { cout << "\t\t\t ______ _______ _______ _______ _ _______ " << endl; cout << "\t\t\t ( ___ \\ ( ___ )( ____ \\( ____ \\( \\ ( ____ \\ " << endl; cout << "\t\t\t | ( ) )| ( ) || ( \\/| ( \\/| ( | ( \\/ " << endl; cout << "\t\t\t | (__/ / | | | || | | | | | | (__ " << endl; cout << "\t\t\t | __ ( | | | || | ____ | | ____ | | | __) " << endl; cout << "\t\t\t | ( \\ \\ | | | || | \\_ )| | \\_ )| | | ( " << endl; cout << "\t\t\t | )___) )| (___) || (___) || (___) || (____/\\| (____/\\ " << endl; cout << "\t\t\t |/ \\___/ (_______)(_______)(_______)(_______/(_______/ " << endl; }//end showLogo() //------------------------------------------------------------------- // Precondition :None // Postcondition :Boggle instructions are displayed //------------------------------------------------------------------- void showInstructions() { cout << "\n\t\t\t\t+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"; cout << "\n\t\t\t\t| Welcome to the game of Boggle! |"; cout << "\n\t\t\t\t+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"; cout << "\n\t\t\t\t| |"; cout << "\n\t\t\t\t| INSTRUCTIONS |"; cout << "\n\t\t\t\t| |"; cout << "\n\t\t\t\t| - You will be shown a 4x4 grid of letters which |"; cout << "\n\t\t\t\t| you will use to find as many words as |"; cout << "\n\t\t\t\t| possible in 3 mins. |"; cout << "\n\t\t\t\t| |"; cout << "\n\t\t\t\t| - You can form words by connecting letters |"; cout << "\n\t\t\t\t| vertically, horizontally and diagonally, |"; cout << "\n\t\t\t\t| however you cannot use the same tile more |"; cout << "\n\t\t\t\t| than once in a word. |"; cout << "\n\t\t\t\t| |"; cout << "\n\t\t\t\t| - The words are scored as follows: |"; cout << "\n\t\t\t\t| 2 or less letters = 0 pts |"; cout << "\n\t\t\t\t| 3 or 4 letters = 1 pt |"; cout << "\n\t\t\t\t| 5 letters = 2 pts |"; cout << "\n\t\t\t\t| 6 letters = 3 pts |"; cout << "\n\t\t\t\t| 7 letters = 4 pts |"; cout << "\n\t\t\t\t| 8 or more letters = 11 pts |"; cout << "\n\t\t\t\t| |"; cout << "\n\t\t\t\t| - Words not in the dictionary receive 0 pts. |"; cout << "\n\t\t\t\t| |"; cout << "\n\t\t\t\t+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"; }//end showInstructions()