//------------------------------------------------------------------- // File :dictionary.C // Author :Alicia Thorsen // Course :CS1129 // // Functions which handle the dictionary. //------------------------------------------------------------------- #include "dictionary.h" #include "strCaseConvert.h" #include "board.h" #include "score.h" #include "search.h" #include #include #include #include #include using namespace std; //------------------------------------------------------------------- // Precondition :None // Postcondition :Returns dictionary in an array. //------------------------------------------------------------------- string * loadDictionary(int & dictSize) { const string filename = "dictionary.txt"; string * dictionary; string s; ifstream inputFile(filename.c_str()); if (inputFile.fail()) { cout << "\nError: Could not read dictionary file. " << "Aborting.\n"; exit(1); } inputFile >> dictSize; dictionary = new string[dictSize]; if (dictionary == NULL) { cout << "\nError: Could not allocate memory for dictionary. " << "Aborting.\n"; exit(1); } for (int i = 0; i < dictSize; i++) { inputFile >> s; dictionary[i] = toLowerCase(s); } return dictionary; } //------------------------------------------------------------------- // Precondition :None // Postcondition :Returns the index of s in the solution or -1 // otherwise //------------------------------------------------------------------- int inSolution(string s, string solution[], int solSize) { s = toLowerCase(s); for (int i = 0; i < solSize; i++) if (s == solution[i]) return i; return -1; } //------------------------------------------------------------------- // Precondition :Board and dictionary have been loaded // Postcondition :Returns all valid words on the board //------------------------------------------------------------------- string * getSolution(char board[][SIZE], string dictionary[], int dictSize, int & solSize) { int capacity = 100; string s; string * solution = new string[capacity]; solSize = 0; for (int i = 0; i < dictSize; i++) { s = dictionary[i]; if ((s.length() > 2) && findWord(board, s)) { if (solSize == capacity) solution = resize(solution, capacity); solution[solSize] = s; solSize++; } } return solution; } //------------------------------------------------------------------- // Precondition :None // Postcondition :Resizes a dynamic array //------------------------------------------------------------------- string * resize(string array[], int & capacity) { const int increment = 50; string * temp; temp = new string[capacity + increment]; if (temp == NULL) { cout << "Memory Allocation Error!. Aborting!"<< endl; exit(1); } for (int i = 0; i < capacity; i++) temp[i] = array[i]; capacity += increment; delete [] array; return temp; } //------------------------------------------------------------------- // Precondition :Answers has all the words on the board // Postcondition :Displays all missed words //------------------------------------------------------------------- void showMissedWords(string answers[], int ansSize, bool found[]) { int score = 0; cout << "\nYou missed the following words:\n" << endl; for (int i = 0 , col = 1; i < ansSize; i++) { if (!found[i]) { cout << setw(10) << answers[i]; if (col % 11 == 0) cout << endl; score += calcScore(answers[i]); col++; } } cout << "\n\nTotal missed points: " << score << endl; }