This project is for the boggle game. I get negative score for the game.The score
ID: 666088 • Letter: T
Question
This project is for the boggle game. I get negative score for the game.The score is depend on the length of the word. I know that would be wrong, but I have no ideas to change it. #include <iostream> #include <cmath> #include<string> #include <iomanip> #include <ctime> #include <stdlib.h> #include <fstream> using namespace std; string arr[16]={"AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS", "AOOTTW", "CIMOTU", "DEILRX", "DELRVY", "DISTTY", "EEGHNW", "EEINSU", "EHRTVW", "EIOSST", "ELRTTY", "HIMNUQ", "HLNNRZ"}; void printGrid(char[4][4]); void letterPicker(char[4][4]); bool doesWordExist(string wordList[], int wlsize, string target); void ScoreWords(); // scores and prints wordscoreWords(string); void BogScorerPrintReport(); // prints final values bool inputMonitor(string words[], double timeDiff,double leftTime, int numWords); int main() { cout <<"Welcome to boggle game program."<<endl; cout << "The boggle board is a grid randomly distribute."<<endl; cout << "The letters used come from 16 6-sided cubes with one letter per side." <<endl; cout << "Each of the 16 locations in the 4-by-4 grid will use one letter from one of the dice."<<endl; cout <<"If the word length between 0 to 2, you will get 0 points"<<endl; cout <<"If the word length between 3 to 4, you will get 1 point"<<endl; cout <<"If the word length is 5, you will get 2 points"<<endl; cout <<"If the word length is 6, you will get 3 points"<<endl; cout <<"If the word length is 7, you will get 5 points"<<endl; cout <<"If the word length is 8 or more than 8, you will get 11 points"<<endl; time_t now; time_t then; double seconds; //get the starting time time(&now); char arrGrid[4][4]; string words[16]; string target; double timeDiff; int numWords; int wlsize; string wordList[6013]; ofstream fout ("WhereAmI.txt"); fout << "I am Here" << endl; fout.close(); //file name char fileName[6013]="dictionary-algs4.txt"; //declare a file stream ifstream fin; //open a file fin.open(fileName); int index=0; if(!fin) { cout<<"File does not exist."<<endl; //exit the program if file does not exist exit; cout<<"Never executed"; } //reading the file contents into the arrays while(fin>>wordList[index]) { index++; } fin.close(); cout<<"ENTER"; string s; cin >> s; letterPicker(arrGrid); printGrid(arrGrid); doesWordExist(wordList,wlsize, target); ScoreWords(); BogScorerPrintReport(); //get the ending time time(&then); seconds = difftime(then, now); cout << "You took " << seconds << " seconds to type in your name." << endl; return 0; } void letterPicker(char arrGrid[4][4]) { //function to fill random chars srand (time(NULL)); string arr1[16]={"AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS", "AOOTTW", "CIMOTU", "DEILRX", "DELRVY", "DISTTY", "EEGHNW", "EEINSU", "EHRTVW", "EIOSST", "ELRTTY", "HIMNUQ", "HLNNRZ"}; int idx = 0; for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { string seq = arr1[idx++]; int letterNum = rand() % 6; char letter = seq.at(letterNum); arrGrid [i][j] = letter; } } } void printGrid(char arr[4][4]) { //function to print random chars to console for (int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(arr[i][j]=='Q') { cout << "Qu" << " "; } else cout<<arr[i][j]<<" "; } cout<<endl; } } bool doesWordExist ( string wordList[], int wlsize, string target ) { int i; int numWords; int score(string); // returns score of single string int totalWords; // total valid words int totalPoints; // total points earned string isValid, word; for ( i = 0 ; i < wlsize ; i++ ) { if ( wordList[i] == target ) { return true; cout << score(word) << " " << isValid << " " << word << endl; totalPoints += score(word); totalWords++; } } cout << 0 << " " << isValid << " " << word << endl; return false; } // calling score function to print scores of word based on length int ScoreWords(string s) { if(s.length() == 3 || s.length() == 4) { return 1; } else if (s.length() == 5) { return 2; } else if(s.length() == 6) { return 3; } else if(s.length() == 7) { return 5; } else { return 11; } } // declaring printreport to prints final word and score counts void BogScorerPrintReport() { int totalWords; int totalPoints; cout << totalWords << " words " << totalPoints << " points "<<endl; } bool inputMonitor(string words[], double timeDiff,int numWords) { if (timeDiff > 180) { cout << "Time is Up!" << endl; cout << endl; cout << "You Entered " << numWords << " words!" << endl; } else { int leftTime; leftTime=180-timeDiff; cout << "You have " << leftTime << " seconds left!" << endl; } }
Explanation / Answer
// here find the error cleared program for Boggle game. Inputs should be given in dictionary-algs4.txt in the proper folder before execution
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
#include <fstream>
using namespace std;
string arr[16]={"AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS", "AOOTTW", "CIMOTU", "DEILRX", "DELRVY", "DISTTY", "EEGHNW", "EEINSU", "EHRTVW", "EIOSST", "ELRTTY", "HIMNUQ", "HLNNRZ"};
void printGrid(char[4][4]);
void letterPicker(char[4][4]);
bool doesWordExist(string wordList[], int wlsize, string target);
int ScoreWords(string s); // scores and prints wordscoreWords(string);
void BogScorerPrintReport(); // prints final values
bool inputMonitor(string words[], double timeDiff, int numWords);
int totalWords; // total valid words
int totalPoints; // total points earned
int main()
{
cout <<"Welcome to boggle game program."<<endl;
cout << "The boggle board is a grid randomly distribute."<<endl;
cout << "The letters used come from 16 6-sided cubes with one letter per side." <<endl;
cout << "Each of the 16 locations in the 4-by-4 grid will use one letter from one of the dice."<<endl;
cout <<"If the word length between 0 to 2, you will get 0 points"<<endl;
cout <<"If the word length between 3 to 4, you will get 1 point"<<endl;
cout <<"If the word length is 5, you will get 2 points"<<endl;
cout <<"If the word length is 6, you will get 3 points"<<endl;
cout <<"If the word length is 7, you will get 5 points"<<endl;
cout <<"If the word length is 8 or more than 8, you will get 11 points"<<endl;
time_t now;
time_t then;
double seconds;
//get the starting time
time(&now);
char arrGrid[4][4];
string words[16];
string target;
// double timeDiff;
// int numWords;
int wlsize;
string wordList[100];
ofstream fout ("WhereAmI.txt");
fout << "I am Here" << endl;
fout.close();
//file name
char fileName[50]="dictionary-algs4.txt";
//declare a file stream
ifstream fin;
//open a file
fin.open(fileName);
int index=0;
if(!fin)
{
cout<<"File does not exist."<<endl;
//exit the program if file does not exist
exit(0);
cout<<"Never executed";
}
//reading the file contents into the arrays
while(fin>>wordList[index])
{
cout<<"hi";
index++;
}
wlsize=index;
fin.close();
cout<<"ENTER";
string s;
cin >> s;
letterPicker(arrGrid);
printGrid(arrGrid);
doesWordExist(wordList,wlsize, s);
BogScorerPrintReport();
//get the ending time
time(&then);
seconds = difftime(then, now);
cout << "You took " << seconds << " seconds to type in your name." << endl;
bool rr;
rr=inputMonitor( wordList,seconds,index);
if(rr==true)
cout<<"Game Over";
else
cout<<"Continue";
return 0;
}
void letterPicker(char arrGrid[4][4])
{
//function to fill random chars
// srand (time(NULL));
string arr1[16]={"AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS", "AOOTTW", "CIMOTU", "DEILRX", "DELRVY", "DISTTY", "EEGHNW", "EEINSU", "EHRTVW", "EIOSST", "ELRTTY", "HIMNUQ", "HLNNRZ"};
int idx = 0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
string seq = arr1[idx++];
int letterNum = rand() % 6;
char letter = seq.at(letterNum);
arrGrid [i][j] = letter;
}
}
}
void printGrid(char arr[4][4])
{ //function to print random chars to console
for (int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(arr[i][j]=='Q')
{
cout << "Qu" << " ";
}
else
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
bool doesWordExist ( string wordList[], int wlsize, string word )
{
int i;
totalWords=0;
totalPoints=0;
int numWords=0;
// int score(string); // returns score of single string
string isValid;
for ( i = 0 ; i < wlsize ; i++ )
{
numWords++;
if ( wordList[i] == word )
{
return true;
cout << ScoreWords(word) << " " << isValid << " " << word << endl;
totalPoints += ScoreWords(word);
totalWords++;
}
}
cout << 0 << " " << isValid << " " << word << endl;
return false;
}
// calling score function to print scores of word based on length
int ScoreWords(string s)
{
if(s.length() == 3 || s.length() == 4)
{
return 1;
}
else if (s.length() == 5)
{
return 2;
}
else if(s.length() == 6)
{
return 3;
}
else if(s.length() == 7)
{
return 5;
}
else
{
return 11;
}
}
// declaring printreport to prints final word and score counts
void BogScorerPrintReport()
{
cout << totalWords << " words " << totalPoints << " points "<<endl;
}
bool inputMonitor(string words[], double timeDiff,int numWords)
{
if (timeDiff > 180)
{
cout << "Time is Up!" << endl;
cout << endl;
cout << "You Entered " << numWords << " words!" << endl;
return true;
}
else
{
double leftTime;
leftTime=180-timeDiff;
cout << "You have " << leftTime << " seconds left!" << endl;
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.