Guessing Game ADT Need Help in C++ program. All program detail is present. I am
ID: 3874182 • Letter: G
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 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
Explanation / Answer
main.cpp
#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 <iostream>
using namespace std;
int* getRange() {
String* whatis = readString("Enter the file name containing the range for the secret number: ");
ReadFile* rf = createReadFile(getText(whatis));
String* range = readLine(rf);
String* range2 = readLine(rf);
int* rangeint;
rangeint = new int [2];
rangeint[0] = a_to_i(range);
rangeint[1] = a_to_i(range2);
close(rf);
return rangeint;
}
int getSecret (int* range) {
int high, low;
high = range[1];
low = range[0];
randomInit();
int random = getRandomInt(low, high);
return random;
}
GuessEnum processGuess (int guess, int secret) {
if(guess < secret) {
return TOO_LOW;
}
if(guess > secret) {
return TOO_HIGH;
}
if(guess == secret) {
return EXACT;
}
}
int play (int* range, int secret) {
int high,low,mid;
bool trigger = false;
int totalguess = 0;
high = range[1];
low = range[0];
mid = (high + low)/2;
cout << "Secret: " << secret << endl;
while(!trigger) {
cout << "Mid: " << mid << endl;
if(processGuess(mid, secret) == EXACT) {
cout << "EXACT" << endl;
mid = secret;
trigger = true;
}
if(processGuess(mid, secret) == TOO_HIGH) {
cout << "HIGH" << endl;
high = mid - 1;
mid = (high + low)/2;
}
if(processGuess(mid, secret) == TOO_LOW) {
cout << "LOW" << endl;
low = mid + 1;
mid = (high + low)/2;
}
totalguess = totalguess + 1;
}
return totalguess;
}
int main()
{
String* n = createString("n");
String* ready_str = readString("Are you ready to play? (y/n) ");
int* rangeM;
int total_guess, secret;
while (compare(n, ready_str) != 0)
{
rangeM = getRange();
secret = getSecret(rangeM);
destroyString(ready_str);
//DO THIS
total_guess = play(rangeM,secret);
cout << "You got it in " << total_guess << " guesses!" << endl;
cin.ignore();
ready_str = readString("Are you ready to play? (y/n) ");
delete rangeM;
}
destroyString(ready_str);
destroyString(n);
return 0;
}
Text.h
#if !defined STRING_STRUCT
#define STRING_STRUCT
struct String
{
const char* text;
int sz; //length of string not including null terminator
};
String* createString(const char* char_array);
void displayString(String* str);
void destroyString(String* str);
int length(String* str);
const char* getText(String* str);
int a_to_i(String* str);
float a_to_f(String* str);
String* i_to_a(int number);
String* f_to_a(float number);
int find(String* str, char delimiter, int start);
String* substr(String* str, int start, int end);
//need to document that this compare only has three possible return values (-1, 0, 1)
int compare(String* str1, String* str2);
#endif
Keyboard.cpp
#include "Keyboard.h"
#include <iostream>
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;
}
Keyboard.h
#if !defined KEYBOARD
#define KEYBOARD
#include "Text.h"
#include <string>
using namespace std;
//pre: the string (character literal) that will prompt the user for input
//post: the input read from the keyboard interpreted as an int is returned
int readInt(string prompt);
int getValidatedInt(string prompt, int min, int max);
//pre: the string that will prompt the user for input
//post: the input read from the keyboard interpreted as a double is returned
double readDouble(string prompt);
double getValidatedDouble(string prompt, double min, double max);
//pre: the string that will prompt the user for input
// the string to store the user input and the length of the input storage string
//post: the text read from the keyboard is copied into the storage string
String* readString(string prompt);
#endif
Random.cpp
#include "Random.h"
#include <time.h>
#include <stdlib.h>
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;
}
Random.h
#if !defined RANDOM_H
#define RANDOM_H
void randomInit();
int getRandomInt(int lower, int upper);
float getRandomFloat(float lower, float upper);
#endif
ReadFile.cpp
#include "ReadFile.h"
#include <iostream>
#include <string>
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;
}
ReadFile.h
#if !defined READ_FILE
#define READ_FILE
#include "Text.h"
#include <fstream>
using namespace std;
struct ReadFile
{
ifstream input_file;
bool _eof;
bool closed;
};
ReadFile* createReadFile(const char* file_name);
void destroyReadFile(ReadFile* rf);
String* readLine(ReadFile* rf);
bool eof(ReadFile* rf);
void close(ReadFile* rf);
#endif
String.cpp
#include "Text.h"
#include <stdlib.h> //needed for atoi and atof
#include <cstring> //needed for strlen and strcmp
#include <sstream>
#include <iostream>
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);
}
Text.h
#if !defined STRING_STRUCT
#define STRING_STRUCT
struct String
{
const char* text;
int sz; //length of string not including null terminator
};
String* createString(const char* char_array);
void displayString(String* str);
void destroyString(String* str);
int length(String* str);
const char* getText(String* str);
int a_to_i(String* str);
float a_to_f(String* str);
String* i_to_a(int number);
String* f_to_a(float number);
int find(String* str, char delimiter, int start);
String* substr(String* str, int start, int end);
//need to document that this compare only has three possible return values (-1, 0, 1)
int compare(String* str1, String* str2);
#endif
WriteFile.cpp
#include "WriteFile.h"
#include <sstream>
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;
}
}
WriteFile.h
#if !defined WRITE_FILE
#define WRITE_FILE
#include "Text.h"
#include <fstream>
using namespace std;
struct WriteFile
{
ofstream output_file;
bool closed;
};
WriteFile* createWriteFile(const char* file_name);
void destroyWriteFile(WriteFile* wf);
void writeLine(WriteFile* wf, String* line);
void close(WriteFile* wf);
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.