//------------------------------------------------------------------- // File :score.C // Author :Alicia Thorsen // Course :CS1129 // // Functions which handle scoring. //------------------------------------------------------------------- #include "score.h" #include "search.h" #include "board.h" #include "dictionary.h" #include "strCaseConvert.h" #include #include #include using namespace std; //------------------------------------------------------------------- // Precondition :Board was loaded, words contains the user's words, and // solution contains all the words on the board // Postcondition :Scores all the user's words and marks all the words // the user found in the found array //------------------------------------------------------------------- void scoreWords(char board[][SIZE], string words[], int wordCt, string solution[], int solSize, bool found[]) { int totalScore = 0, score, location; string reason; cout.setf(ios::left); cout << "-----------------------------------------" << endl; cout << " " << setw(16) << "WORD" << "| SCORE"; cout << "\n-----------------------------------------" << endl; for (int i = 0; i < wordCt; i++) { score = 0; reason = ""; if (words[i].length() < 3) reason = " - Less than 3 letters"; else if(!findWord(board, words[i])) reason = " - Not on board"; else if (duplicate(words[i], words, i)) reason = " - Duplicate"; else { location = inSolution(words[i], solution, solSize); if (location == -1) reason = " - Not in dictionary"; else { score = calcScore(words[i]); found[location] = true; } } cout << " " << setw(16) << words[i] << "| " << score << reason << endl; totalScore += score; } cout << "-----------------------------------------" << endl; cout << " " << setw(16) << "TOTAL SCORE " << "| " << totalScore << endl; cout << "=========================================" << endl; } //------------------------------------------------------------------- // Precondition :words contains the user's words // Postcondition :Returns true if the word appears more than once in the // user's words //------------------------------------------------------------------- bool duplicate(string s, string words[], int stop) { for (int i = 0; i < stop; i++) if (s == words[i]) return true; return false; } //------------------------------------------------------------------- // Precondition :s contains a word // Postcondition :Returns the score of the word //------------------------------------------------------------------- int calcScore(string s) { int l = s.length(); if (l <= 4) return 1; if (l == 5) return 2; if (l == 6) return 3; if (l == 7) return 4; return 11; }