Please help with C++? I've updated the functions in the skeletonas best I could with the exception of the TestAndDoublefunction. I'm having trouble visualizing how to execute that one.Thank you for any help you can provide.
*********************************************************************************Program_Skeleton*************************************************************************
*************************************************************/#include <iostream> //standard library for i/o#include <string> // always include this when you use thestring class#include <fstream>#include <ctime> //random number generatorusing namespace std;
/*********************************************************//Following is the declaration of a round of gameNumberGuess**********************************************************/class gameNumberGuess{public: int level = 0; //game level (1 or2) int maxGuesses = 0; //max number of guesses(example 5 or 8) int upperValue = 0; //upper range value (example100 or 1000) int currentGuess = 0; //current guess input bythe user int solution = 0; //pseudo randomgenerated number int smallNum = 0; //lower value forfeedback int largeNum = 0; //higher value forfeedback bool above = false; //current guess above thesolution bool below = false; //current guess below thesolution string name; //name of current player bool wonOrLost = 0; //true or false for thecurrent round of the game};
//Declaration/ Prototypes for your functions will go here
void TestAndDouble(gameNumberGuess*& gameList, int count,int& size);//doubles the size of the array when count equals size
void PrintPlayerResults(gameNumberGuess gameList[], intgameCount);//Connects to an output file//Prints the array results onto the screen and into the outputfile
void PlayOneRound(const string& name, gameNumberGuess¤tGame);//Play one round of the number guess game
void GenerateSolutionNumber(gameNumberGuess¤tGame);//Generates the solution number in the correct range
//add the rest of the prorotypes
//HERE IS THE main function
int main(){ string yesOrNo = "y";
gameNumberGuess* gameList;//declare an arrayof gameNumberGuess objects //count the number of games
//declare and initialize size to 1 int size = 1;
//use new to allocate memory to gameList ofsize 1 gameList = new gameNumberGuess[size];//array ofsize one
//want to play?
while (yesOrNo != "n" && yesOrNo !="N") { //make sure the array islarge enough //TestAndDouble
//ask for the name here,it may be a different player //into the member variablename //cin >>gameList[gameCount].name;
//play one round
//add one to thegamecount //again? } //print results
//good bye and thanks return 0;}
//Function Implementations will go here
//*************************************************************************************//Name: TestAndDouble//Precondition: //Postcondition: //Description: //*************************************************************************************
void TestAndDouble(gameNumberGuess*& gameList, int count,int& size){ if (count == size) { size *= 2;
//create a new temparray gameNumberGuess* temp = newgameNumberGuess[size];
//copy all the objectsinto the temp array for (int i = 0; i < count;i++) { temp =gameList; }
//delete the gameListpointer delete[] gameList;
//point gameList totemp gameList = temp; } cout << "\ncount is " <<count; cout << "\nsize is " << size<< endl;}
//*************************************************************************************//Name: PrintPlayerResults//Precondition://Postcondition://Description://*************************************************************************************
void PrintPlayerResults(gameNumberGuess gameList[], intgameCount){ ofstream out; char filename[40]; //user should enter filename with .txtextension cout << "Please enter a filename for the NumberGame Results." << endl; cout << "Be sure to include the .txt extensionwith filename." << endl; cout << "Enter the filename: "; cin >> filename;
// Add to the file if the user enters the samefile name out.open(filename, ios::app);
int i;
cout <<"\n***********************************\n"; cout << "Name" << "\t" << "Level"<< "\t" << "Won Or Lost" << "\n";
for (i = 0; i < gameCount; i ++) { cout << gameList.name << "\t"<< gameList.level << "\t" <<gameList.wonOrLost << "\n"; }
out <<"\n***********************************\n"; out << "Name" << "\t" << "Level"<< "\t" << "Won Or Lost" << "\n";
for (i = 0; i < gameCount; i ++) { out << gameList.name << "\t"<< gameList.level << "\t" <<gameList.wonOrLost << "\n"; } out.close();
}
//*************************************************************************************//Name: PlayOneRound//Precondition://Postcondition: //Description: //*************************************************************************************
void PlayOneRound(gameNumberGuess& currentGame){ SetUpLevel(currentGame); GenerateSolutionNumber(currentGame);
// Initalize bool and high/low values
currentGame.above = 0; currentGame.below = 0; currentGame.smallNum = 1; currentGame.largeNum = currentGame.upperValue;
//cout << "PLAY small: " <<currentGame.smallNum << endl; //cout << "PLAY large: " <<currentGame.largeNum << endl;
int i = 1;
// Execute Counting Loop:
while (i <= currentGame.maxGuesses) { //tell the user what guess number theyare on cout << endl; cout << "This is guess number ("<< i << " of " << currentGame.maxGuesses <<")"; //get the next guess GetGuessInput(currentGame); //reset above and below bool values tofalse currentGame.below = 0; currentGame.above = 0;
if (ProcessGuess(currentGame)) { cout << "\nYou wonthat round, "<< currentGame.name << "!" <<endl; i =currentGame.maxGuesses; // Stop loop without a breakstatement }
i++; }
// Display the solution when user wins orloses
cout << "The solution was " <<currentGame.solution << endl;}
//*************************************************************************************//Name: SetUpLevel//Precondition://Postcondition://Description: //*************************************************************************************
void SetUpLevel(gameNumberGuess& currentGame){ do { // Display and get the levels. cout << endl; cout << "what level(Enter 1 or2)?" << endl; cout << "(1) Level 1 = 5 guesses,number 1 through 100" << endl; cout << "(2) Level 2 = 8 guesses,number 1 through 1000" << endl; cin >> currentGame.level; } while (currentGame.level > 2 ||currentGame.level < 1);
if (currentGame.level == 1) { currentGame.upperValue = 100; currentGame.maxGuesses = 5; } else if (currentGame.level == 2) { currentGame.upperValue = 1000; currentGame.maxGuesses = 8; } else { cout << "That is not a validlevel" << endl; }}
//*************************************************************************************//Name: GetGuessInput//Precondition://Postcondition: //Description: //*************************************************************************************
void GetGuessInput(gameNumberGuess& currentGame){
if (currentGame.below) { if (currentGame.smallNum <currentGame.currentGuess) { currentGame.smallNum =currentGame.currentGuess; } }
if (currentGame.above) { if (currentGame.largeNum >currentGame.currentGuess) { currentGame.largeNum =currentGame.currentGuess; } }
cout << endl; cout << "Enter a guess between " <<currentGame.smallNum << " and " << currentGame.largeNum<< ": "; cin >> currentGame.currentGuess; cout << endl;}
//*************************************************************************************//Name: ProcessGuess//Precondition: //Postcondition://Description: //*************************************************************************************
bool ProcessGuess(gameNumberGuess& currentGame){ if (currentGame.currentGuess >currentGame.solution) { cout << "\nYour guess was toohigh."; currentGame.wonOrLost = 0; currentGame.above = true; return false; }
else if (currentGame.currentGuess <currentGame.solution) { cout << "\nYour guess was toolow."; currentGame.wonOrLost = 0; currentGame.below = true; return false; }
else { currentGame.wonOrLost = 1; return true; }}
//*************************************************************************************//Name: GenerateSolutionNumber//Precondition://Postcondition://Description: //*************************************************************************************
void GenerateSolutionNumber(gameNumberGuess¤tGame){ srand((unsigned)time(0)); currentGame.solution = 1 + rand() %currentGame.upperValue;
}
COP 3014-program 7-game number guess with a dynamic array Assignment purpose: programmer defined functions, call by reference and call by value, i/o in C++, loop, data types string, int, and bool, class declaration, dynamic array practice This program uses many of the features from programs 4 and 5. You should be familiar with programs 4 and 5 before working on this assignment. You will write a program that will play a number guessing game. This program will allow multiple players to play the game. You will store each game object (one round of the game) in an array, there are 2 additional member variables: The game will have 2 levels: You can decide the name of the levels, number of guesses, and the range, please limit it to only 2 levels. Here is an example: (1) Intermediate - 5 guesses, numbers 1 through 100 (2) Advanced - 8 guesses, numbers 1 through 1000 ● ● . ● ● ● ● ● • . •● ● ● The number to guess will be generated with a value between 1 and an upper range depending on the level selected Get one guess at a time from the player and provide feedback, see the sample output below Ask the player if they want to play before getting started (use a while loop) The game will allow more than one player, notice the string name; Interact with the EACH player by getting the first name Assume the player will enter an integer for each guess (assume correct data type input) If the player enters a negative number, it will be too low If the player enters a number out of the range, it will be either too high or too low You do not need to force the player to enter a number in the correct range Display the solution number at the end of each round of the game (whether they win or lose that round) The next player should be able to continue to play another round by entering (y or Y) quit by entering (n or N) You do not need to force the player to enter y or n Store all the information about each round of the game in a dynamic array The array (gameList) will begin at size one (for one player) each time a new player wants to play the game you will need to see if the size of the array is big enough to add a new player, if it is not, you will double the size of the array. Use the operator new to allocate memory for the dynamic array Use the operator delete to de-allocate memory See the dynamic_array_example8.cpp code When the player(s) wish to close the program, display the game results from the array both on the screen and in an output file, the player will enter the name of the output file char filename [16]; //user should enter filename with .txt extension cout << "Enter the filename: "; cin >> filename; //add to the file if the user enters the same file name out.open(filename, ios::app); See the sample output below Use the class below to store the data for each round of gameNumberGuess
class gameNumberGuess { public: int level = 0; //game level (1 or 2) int maxGuesses = 0; //max number of guesses (example 5 or 8) int upperValue = 0; //upper range value (example 100 or 1000) int currentGuess = 0; //current guess input by the user int solution = 0; //pseudo random generated number int smallNum = 0; //lower value for feedback int largeNum = 0; //higher value for feedback bool above = false; //current guess above the solution bool below = false; //current guess below the solution string name; //name of current player bool wonOrLost = 0; //true or false for the current round of the game }; Use functions to break up the program //BE SURE TO ADD COMMENTS TO THE FUNCTION PROTOTYPES (above main) //AND TO THE FUNCITON DEFINITIONS (below main) //use pre and post conditions, see the sample provided //ADD comments throughout the code making it easier to read //Declaration/ Prototypes for your functions will go here void TestAndDouble (gameNumberGuess*& gameList, int count, int& size); //doubles the size of the array when count equals size //Functions below are the same as program 5 void PrintPlayerResults(gameNumberGuess gameList[], int gameCount); //Connects to an output file //Prints the array results onto the screen and into the output file void PlayOneRound(const string& name, gameNumberGuess& currentGame); //Play one round of the number guess game void SetUpLevel(gameNumberGuess& current Game); //sets the upper value and max guesses of the current game based on the level void GetGuessInput (gameNumberGuess& current Game); //Displays the range, prompts, and gets the next guess from the user bool ProcessGuess (game NumberGuess& currentGame); //returns true if the guess matches the solution returns false if they do not match //lets the user know if the guess was too high or too low void Generate Solution Number(gameNumberGuess& current Game); //Generates the solution number in the correct range Additional instructions: ● ● ● You may add additional functions, but do not modify the functions provided Be sure to comment your code Include a program header with the following information: O Name, due date, course, assignment number, professor name, and a brief description of the assignment - see the sample skeleton provided with the assignment. Read all comments in the sample code provided before getting started Read the problem and determine what to do
● ● . ● Write the algorithm (you DO NOT need to submit the algorithm) Implement one component at a time in your code and do not move onto the next component until you are sure it is correct. The code should be tested and run on an IDE before it is uploaded onto Canvas The code must be submitted on time in order to receive credit (11:59 PM on the due date) Late submissions will not be accepted or graded ● All programming assignments are individual work, sharing code is considered cheating • Test your program before submitting One source code file (unformatted text) will be submitted The file name must match the assignment Here is sample output: (You do not need to print the count and size, I added that to follow the dynamic array) Do you want to play the guessing game? (y (Y) or n (N)) : y count is 0 size is 1 Enter your first name: Tami what level (Enter 1 or 2) ? (1) Level 1 - 5 guesses, numbers 1 through 100 (2) Level 2 - 8 guesses, numbers 1 through 1000 1 This is guess number (1 of 5) Enter a guess between 1 and 100: 20 Your guess was too low. This is guess number (2 of 5) Enter a guess between 20 and 100: 60 Your guess was too low. This is guess number (3 of 5) Enter a guess between 60 and 100 80 Your guess was too low. This is guess number (4 of 5) Enter a guess between 80 and 100: 92 Your guess was too high. This is guess number (5 of 5) Enter a guess between 80 and 92: 84 You won that round, Tami! THE SOLUTION WAS 84 Do you want to play another round? (y (Y) or n (N)) : y size is 1 count is 1 size is 2 Enter your first name: Gio what level (Enter 1 or 2)? (1) Level 1 - 5 guesses, numbers 1 through 100 (2) Level 2 - 8 guesses, numbers 1 through 1000 2 This is guess number (1 of 8) Enter a guess between 1 and 1000: 500 Your guess was too low.
This is guess number (2 of 8) Enter a guess between 500 and 1000 600 Your guess was too low. This is guess number (3 of 8) Enter a guess between 600 and 1000 700 Your guess was too low. This is guess number (4 of 8) Enter a guess between 700 and 1000 800 : Your guess was too low. This is guess number (5 of 8) Enter a guess between 800 and 1000 900 Your guess was too low. This is guess number (6 of 8) Enter a guess between 900 and 1000: 999 Your guess was too high. This is guess number (7 of 8) Enter a guess between 900 and 999 998 Your guess was too high. This is guess number (8 of 8) Enter a guess between 900 and 998: 997 Your guess was too high. THE SOLUTION WAS 963 You did not win that round, Gio! Do you want to play another round? (y (Y) or n (N)): y size is 2 count is 2 size is 4 Enter your first name: Gio what level (Enter 1 or 2) ? (1) Level 1 - 5 guesses, numbers 1 through 100 (2) Level 2 - 8 guesses, numbers 1 through 1000 1 This is guess number (1 of 5) Enter a guess between 1 and 100 33 Your guess was too low. This is guess number (2 of 5) Enter a guess between 33 and 100 : 77 Your guess was too high. This is guess number (3 of 5) Enter a guess between 33 and 77: 55 Your guess was too high. This is guess number (4 of 5) Enter a guess between 33 and 55 44 Your guess was too low. This is guess number (5 of 5) Enter a guess between 44 and 55 51 Your guess was too low. THE SOLUTION WAS 54 You did not win that round, Gio! Do you want to play another round? (y (Y) or n (N)) : у
size is 4 count is 3 size is 4 Enter your first name: Tami what level (Enter 1 or 2)? (1) Level 1 - 5 guesses, numbers 1 through 100 (2) Level 2 - 8 guesses, numbers 1 through 1000 1 This is guess number (1 of 5) Enter a guess between 1 and 100 12 Your guess was too high. This is guess number (2 of 5) Enter a guess between 1 and 12: 9 Your guess was too high. This is guess number (3 of 5) Enter a guess between 1 and 9: 4 Your guess was too low. This is guess number (4 of 5) Enter a guess between 4 and 9: 7 Your guess was too high.. This is guess number (5 of 5) Enter a guess between 4 and 7: 5 Your guess was too low. THE SOLUTION WAS 6 You did not win that round, Tami! Do you want to play another round? (y (Y) or n (N)): y size is 4 count is 4 size is 8 Enter your first name: Gio what level (Enter 1 or 2) ? (1) Level 1 - 5 guesses, numbers 1 through 100 (2) Level 2 - 8 guesses, numbers 1 through 1000 1 This is guess number (1 of 5) Enter a guess between 1 and 100 44 Your guess was too low. This is guess number (2 of 5) Enter a guess between 44 and 100: 72 Your guess was too high. This is guess number (3 of 5) Enter a guess between 44 and 72: 56 Your guess was too low. This is guess number (4 of 5) Enter a guess between 56 and 72: 61 Your guess was too low. This is guess number (5 of 5) Enter a guess between 61 and 72: 66
Your guess was too high. THE SOLUTION WAS 62 You did not win that round, Gio! Do you want to play another round? (y (Y) or n(N)): size is 8 Thank you for playing, have a great day! Enter the filename: out.txt Name Tami Gio Gio Tami Gio Level won or lost 1 0 0 0 0 ******** n
Please help with C++? I've updated the functions in the skeleton as best I could with the exception of the TestAndDouble
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am