Question: Guessing Game ADT Need Help in C++ program. All program detail is present. I am not able to clearly understand what exactly I am required
Guessing Game ADT
Need Help in C++ program. All program detail is present. I am not able to clearly understand what exactly I am required to do. If you need more information please ask. Thanks
I have forgot to attach the header file please see the Url link (all file are present in folder L1file )
https://drive.google.com/open?id=0B8lr6uov5-0DVHdpR1I1bnJmUW8
Description
In this lab you will write a simple guessing game. The computer will generate a random integer within a valid specified range, inclusive (min, max) read in from a text file. The user then tries to guess the generated secret number. If the guess is not correct, the user is told whether they are too high or too low. When the secret number has been correctly guessed, the total number of guesses required is reported. The user is then asked if they want to play again.
You will need to use the String, ReadFile, Keyboard and Random structs I have provided to complete this lab. To use these structs, you can refer to the method signatures in the header files for each struct.
#include "Text.h"
#include "Readfile.h"
#include "Random.h"
#include "Keyboard.h"
You will need to complete the following methods for your project
int getSecret (int* range) //Obtain a randomly generated secret number inside the range
int* getRange() //Get the name of the text file and read the range from the text file. (range is an int array of size 2, dynamically allocated using new, delete pointers when finished with them)
int getGuess (int* range) //Obtain a guess (ask again if guess is not in range)
GuessEnum processGuess (int guess, int secret) //determine whether a guess is correct. Return EXACT, TOO_HIGH or TOO_LOW
int play (int* range, int secret) //Loops until the secret number has been guessed and returns the total number of guesses required (calls getGuess and processGuess)
int main() //Starts the game and loops as long as the user wants to keep playing (calls getRange, getSecret and play. Deletes the dynamic assigned int array
SAMPLE RUN
Are you ready to play (y/n)? y Enter the file name containing the range for the secret number: range.txt Enter your guess: 500 Too high Enter your guess: 250 Too low Enter your guess: 400 Too high Enter your guess: 300 Too high Enter your guess: 275 Too low Enter your guess: 285 Too low Enter your guess: 290 Too high Enter your guess: 287 You got it in 8 guesses! Are you ready to play again? (y/n) n
FILES
_____________________________________________________________________________________________________________________________________________________
ReadFile.cpp
#include "ReadFile.h" #include #include
ReadFile* createReadFile(const char* file_name) { ReadFile* rf = new ReadFile;
rf->input_file.open(file_name); rf->closed = false; rf->_eof = false;
return rf; }
void destroyReadFile(ReadFile* rf) { close(rf); delete rf; }
bool eof(ReadFile* rf) { return rf->_eof; }
void close(ReadFile* rf) { if (!rf->closed) { rf->input_file.close(); rf->closed = true; } }
String* readLine(ReadFile* rf) { if (rf->closed) return NULL; if (rf->_eof) return NULL;
string text; rf->_eof = !(getline(rf->input_file, text));
String* str = createString(text.c_str()); return str; }
____________________________________________________________________________________________________________________________________________
Random.cpp
#include "Random.h"
#include #include
void randomInit() { srand (time(NULL)); }
int getRandomInt(int lower, int upper) { int diff = upper - lower + 1; int random_num = rand()%diff; random_num = random_num + lower; //gives a number between lower and upper, inclusive return random_num; }
float getRandomFloat(float lower, float upper) { float r_float_1 = (float) rand(); float r_float_2 = (float) RAND_MAX;
float random_normalized = r_float_1/r_float_2; //between 0.0 and 1.0 float random_float = lower + random_normalized*(upper - lower); return random_float; }
_____________________________________________________________________________________________________________________________________________________
String.cpp
#include "Text.h"
#include //needed for atoi and atof #include //needed for strlen and strcmp #include #include using namespace std;
String* createString(const char* char_array) { int sz = strlen(char_array); char* text = new char[sz+1]; for (int i = 0; i < sz; i++) { text[i] = char_array[i]; } text[sz] = 0; //null terminator
String* str = new String; str->text = text; str->sz = sz; return str; }
int length(String* str) { return str->sz; }
const char* getText(String* str) { return str->text; }
int compare(String* str1, String* str2) { return strcmp(str1->text, str2->text); }
void displayString(String* str) { const char* text = str->text; cout << text; }
void destroyString(String* str) { const char* text = str->text; delete[] text; delete str; }
int find(String* str, char delimiter, int start) { int sz = str->sz; const char* text = str->text; if (start >= sz || start < 0) return -1;
int loc = sz; for (int i = start; i < sz; i++) { if (text[i] == delimiter) { loc = i; break; } }
return loc; }
//the substring will use the characters from start to end inclusive String* substr(String* str, int start, int end) { if (start > end || start < 0) return NULL;
int sz = str->sz; if (start > sz || end > sz) return NULL;
String* sub = new String; const char* text = str->text; int sub_len = end - start + 1; char* sub_text = new char[sub_len + 1];
int count = 0; for (int i = start; i <= end; i++) { sub_text[count] = text[i]; count++; } sub_text[count] = 0;
sub->text = sub_text; sub->sz = sub_len; return sub; }
int a_to_i(String* str) { const char* text = str->text; return atoi(text); }
float a_to_f(String* str) { const char* text = str->text; return atof(text); }
String* i_to_a(int number) { stringstream out; out << number; const char* text = out.str().c_str(); return createString(text); }
String* f_to_a(float number) { stringstream out; out << number; const char* text = out.str().c_str(); return createString(text); }
_____________________________________________________________________________________________________________________________________________________
WriteFile.cpp
#include "WriteFile.h" #include
WriteFile* createWriteFile(const char* file_name) { WriteFile* wf = new WriteFile; wf->output_file.open(file_name); wf->closed = false; return wf; }
void destroyWriteFile(WriteFile* wf) { close(wf); delete wf; }
void close(WriteFile* wf) { if (!wf->closed) { wf->output_file.close(); wf->closed = true; } }
void writeLine(WriteFile* wf, String* line) { if (!wf->closed && length(line) > 0) { wf->output_file << getText(line) << endl; } }
_____________________________________________________________________________________________________________________________________________________
Keyboard.cpp
#include "Keyboard.h" #include
int readInt(string prompt) { cout << prompt; int val = 0; cin >> val; return val; }
int getValidatedInt(string prompt, int min, int max) { int validated = readInt(prompt); cout << validated << endl;
while(validated < min || validated > max) { validated = readInt(prompt); cout << validated << endl; }
return validated; }
double readDouble(string prompt) { cout << prompt; double val = 0; cin >> val; return val; }
double getValidatedDouble(string prompt, double min, double max) { double validated = readDouble(prompt); cout << validated << endl;
while(validated < min || validated > max) { validated = readDouble(prompt); cout << validated << endl; }
return validated; }
String* readString(string prompt) { cout << prompt; string text; getline(cin, text);
String* str = createString(text.c_str()); return str; }
_____________________________________________________________________________________________________________________________________________________
PlayerGuessDriver.cpp (All the changes will be done in this file)
#if !defined (GUESS_ENUM) #define GUESS_ENUM enum GuessEnum {EXACT, TOO_LOW, TOO_HIGH}; #endif
#include "Text.h" #include "ReadFile.h" #include "Random.h" #include "Keyboard.h"
#include using namespace std;
//insert your methods here
int main() { String* n = createString("n"); String* ready_str = readString("Are you ready to play? (y/n) ");
while (compare(n, ready_str) != 0) { destroyString(ready_str); //DO THIS
cout << "You got it in " << total_guess << " guesses!" << endl; cin.ignore(); ready_str = readString("Are you ready to play? (y/n) "); }
destroyString(ready_str); destroyString(n); return 0; }
__________________________________________________________________________________________________________________________________________________
For more information Please view Url link
URL LINK TO VIEW ALL THIS FILE AS WELL AS WHOLE ASSIGNMENT
Assignment page and all prgram file are attached here
https://drive.google.com/open?id=0B8lr6uov5-0DVHdpR1I1bnJmUW8
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
