(Part A) : The Machine Guessing Game– 50 points Create the following Three-funct
ID: 3896946 • Letter: #
Question
(Part A) : The Machine Guessing Game– 50 points Create the following Three-function C++ program: This is a game program that can be played with any number of players. A goal dollar amount must be set at the beginning of the game (i.e. $20). The object of this game is to see who can guess which number is selected between 1 and 5 that the computer selects. If the player guesses correctly then the player will earn the dollar amount given next to the number in the table below and if the player guesses incorrectly then the player will deduct the dollar amount. The player who reaches the goal dollar amount first, wins the game and the program ends. Each player takes a turn guessing.
Number Dollar Amount ($)
getNumberOfPlayersAndGoalAmount(numPlayers,goalAmount) : a void function that retuns the number of players and goal dollar amount. Use pass by reference to return information to main(). getMachineGuess() : a value-returning function that retuns the random number between 1 and 5. Use a return statement to send it to main(). isGoalReached(playerAmount,goalAmount) : a value-returning function that determines if player balance reaches goal. Returns true or false. The main() function should display the Winner on the computer screen.
Number Dollar Ammount 1 4 2 11 3 5 4 10 5 3Explanation / Answer
#include<iostream> //Header files
#include<stdlib.h>
#include<time.h>
using namespace std;
void getNumberOfPlayersAndGoalAmount(int *numPlayers, int *goalAmount) //Function to input numPlayers and goalAmount using call by reference
{
do
{
cout<<"Enter the number of players: ";
cin>>(*numPlayers);
}while(*numPlayers < 1 || *numPlayers > 20); //Maximum number of players can be 20, we can change it by replacing 20 with any number
cout<<"Enter the goal amount: ";
cin>>(*goalAmount);
}
int getMachineGuess() //Function to return a random number from 1 to 5
{
return((rand() % 5) + 1);
}
bool isGoalReached(int playerAmount, int goalAmount) //Function to check if player won or not
{
if(playerAmount >= goalAmount) //Player won if he has amount equal or more than goalAmount
return(true);
else
return(false);
}
int main()
{
int numPlayers, goalAmount, arr[20], i, temp; //Variable declaration
//We have assumed maximum number of player as 20 because
//we have to mention the size of array in function decalaration,
//So we cant take a variable numPlayer
srand(time(0)); //Seeding the rand()
getNumberOfPlayersAndGoalAmount(&numPlayers, &goalAmount); //Input numPlayers and goalAmount
for(i = 0; i < numPlayers; i++)
arr[i] = 0; //Initially all have 0 amount
for(i = 0;;i++) //Inifinite loop (until someone wins)
{
do
{
cout<<"Player "<<i % numPlayers + 1<<"'s turn: "; //Input players's guess
cin>>temp;
}while(temp < 1 || temp > 5); //Repeat if guess is not from 1 to 5
if(temp == getMachineGuess()) //Compare guess with random number
{
switch(temp) //Increase the amount of respective player accordingly
{
case 1: arr[i % numPlayers] += 4;
break;
case 2: arr[i % numPlayers] += 11;
break;
case 3: arr[i % numPlayers] += 5;
break;
case 4: arr[i % numPlayers] += 10;
break;
case 5: arr[i % numPlayers] += 3;
break;
}
}
cout<<"Player "<<i % numPlayers + 1<<"'s amount = "<<arr[i % numPlayers]<<" "; //Print amount after every turn
if(isGoalReached(arr[i % numPlayers], goalAmount)) //Check if the player won
{
cout<<"Player "<<i % numPlayers + 1<<" won."; //If won, print and break out of infinite loop
break;
}
}
return(0);
}
The output screenshots are below for some sample inputs. User inputs are in bold and underline.
Test Run 1
Enter the number of players: 2
Enter the goal amount: 20
Player 1's turn: 1
Player 1's amount = 0
Player 2's turn: 2
Player 2's amount = 0
Player 1's turn: 3
Player 1's amount = 0
Player 2's turn: 4
Player 2's amount = 0
Player 1's turn: 5
Player 1's amount = 0
Player 2's turn: 1
Player 2's amount = 0
Player 1's turn: 2
Player 1's amount = 0
Player 2's turn: 3
Player 2's amount = 5
Player 1's turn: 4
Player 1's amount = 0
Player 2's turn: 5
Player 2's amount = 5
Player 1's turn: 1
Player 1's amount = 0
Player 2's turn: 2
Player 2's amount = 5
Player 1's turn: 3
Player 1's amount = 0
Player 2's turn: 4
Player 2's amount = 5
Player 1's turn: 5
Player 1's amount = 3
Player 2's turn: 1
Player 2's amount = 9
Player 1's turn: 2
Player 1's amount = 3
Player 2's turn: 3
Player 2's amount = 9
Player 1's turn: 4
Player 1's amount = 3
Player 2's turn: 5
Player 2's amount = 12
Player 1's turn: 1
Player 1's amount = 7
Player 2's turn: 2
Player 2's amount = 12
Player 1's turn: 3
Player 1's amount = 7
Player 2's turn: 4
Player 2's amount = 12
Player 1's turn: 5
Player 1's amount = 7
Player 2's turn: 1
Player 2's amount = 12
Player 1's turn: 2
Player 1's amount = 7
Player 2's turn: 3
Player 2's amount = 12
Player 1's turn: 4
Player 1's amount = 7
Player 2's turn: 5
Player 2's amount = 15
Player 1's turn: 1
Player 1's amount = 7
Player 2's turn: 2
Player 2's amount = 26
Player 2 won.
Test Run 2
Enter the number of players: 2
Enter the goal amount: 20
Player 1's turn: 1
Player 1's amount = 0
Player 2's turn: 2
Player 2's amount = 0
Player 1's turn: 3
Player 1's amount = 0
Player 2's turn: 4
Player 2's amount = 0
Player 1's turn: 5
Player 1's amount = 0
Player 2's turn: 1
Player 2's amount = 0
Player 1's turn: 2
Player 1's amount = 11
Player 2's turn: 3
Player 2's amount = 0
Player 1's turn: 4
Player 1's amount = 11
Player 2's turn: 5
Player 2's amount = 0
Player 1's turn: 1
Player 1's amount = 11
Player 2's turn: 2
Player 2's amount = 11
Player 1's turn: 3
Player 1's amount = 11
Player 2's turn: 4
Player 2's amount = 21
Player 2 won.
Test Run 3
Enter the number of players: 50
Enter the number of players: 2
Enter the goal amount: 20
Player 1's turn: 0
Player 1's turn: 6
Player 1's turn: 2
Player 1's amount = 0
Player 2's turn: 2
Player 2's amount = 0
Player 1's turn: 2
Player 1's amount = 11
Player 2's turn: 2
Player 2's amount = 11
Player 1's turn: 2
Player 1's amount = 11
Player 2's turn: 2
Player 2's amount = 22
Player 2 won.
NOTE: It is not that always Player 2 wins. These outputs are completely random and it is thoroughly checked that all players have equal chances.
Kindly give a thumbs up, if found useful. Comment for queries. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.