Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a program for a game where the user and the computer compete to build the

ID: 3531812 • Letter: W

Question


write a program for a game where the user and the computer compete to build the biggest sandcastle. The game is played in turns. Each turn, the user and the computer decide which tool they will use (Bucket, pail, shovel etc) to add sand to the castle.
The first player to build a 100-foot-high sandcastle wins!
Each tool will add a random amount of sand to the castle, between a minimum amount and a maximum amount.
Information about the tools available should be stored in two arrays:
int minSand[] int MaxSand[]
The index for these arrays is the tool you are using. for example, Tool 0 (some kind of bucket, perhaps) might add at least 2 feet and at most 5 feet to your sandcastle. In this case,minSand[0]would be 2 andmaxSand[0]would be 5.
A third array (toolName[]) should be used to store the name of the tool, as a string. In this case,toolName[3] = "bucket"
These three arrays are related, and known as "parallel arrays"
The game logic should be implemented using the following main game loop:
while(!Winner()) { DoUserTurn(); DoComputerTurn(); DisplayInfo(); }
Each of these functions may or may not have parameters. This should be determined by you.
bool Winner()should check the state of the game and determine if there is a winner. If not, another round should be played. void DoUserTurn()should ask the user which tool they want to use, randomly add sand to their castle based on that tool. void DoComputerTurn()should use a random number to pick a tool for the computer, and add sand to the computer's castle. void DisplayInfo()should report the current game state to the user.
In addition to these four functions, You should implement the following:
void PromptUser()should be called from withinDoUserTurn()to handle the interaction with the user to select a tool. Additionally, within this function or as another function, you should go through the arrays to list all of the tools and the range of sand they can add to a castle.
int RandomBetween(int low, int high)should return a random number between (and including) low and high. This function will be used to determine exactly how much sand is added to each sandcastle using the tools from the previous arrays. It can also be used to allow the computer player to randomly choose one of the available tools. The general formula for random numbers in a range isrand() % range + min, so your first task in this function should be to determine the range from the high and low integers. Don't forget to seed your random number once at the beginning of the program. Use a global constant variable for the number of tools available, and create your arrays based on that.

write a program for a game where the user and the computer compete to build the biggest sandcastle. The game is played in turns. Each turn, the user and the computer decide which tool they will use (Bucket, pail, shovel etc) to add sand to the castle.
The first player to build a 100-foot-high sandcastle wins!
Each tool will add a random amount of sand to the castle, between a minimum amount and a maximum amount.
Information about the tools available should be stored in two arrays:
int minSand[] int MaxSand[]
The index for these arrays is the tool you are using. for example, Tool 0 (some kind of bucket, perhaps) might add at least 2 feet and at most 5 feet to your sandcastle. In this case,minSand[0]would be 2 andmaxSand[0]would be 5.
A third array (toolName[]) should be used to store the name of the tool, as a string. In this case,toolName[3] = "bucket"
These three arrays are related, and known as "parallel arrays"
The game logic should be implemented using the following main game loop:
while(!Winner()) { DoUserTurn(); DoComputerTurn(); DisplayInfo(); }
Each of these functions may or may not have parameters. This should be determined by you.
bool Winner()should check the state of the game and determine if there is a winner. If not, another round should be played. void DoUserTurn()should ask the user which tool they want to use, randomly add sand to their castle based on that tool. void DoComputerTurn()should use a random number to pick a tool for the computer, and add sand to the computer's castle. void DisplayInfo()should report the current game state to the user.
In addition to these four functions, You should implement the following:
void PromptUser()should be called from withinDoUserTurn()to handle the interaction with the user to select a tool. Additionally, within this function or as another function, you should go through the arrays to list all of the tools and the range of sand they can add to a castle.
int RandomBetween(int low, int high)should return a random number between (and including) low and high. This function will be used to determine exactly how much sand is added to each sandcastle using the tools from the previous arrays. It can also be used to allow the computer player to randomly choose one of the available tools. The general formula for random numbers in a range isrand() % range + min, so your first task in this function should be to determine the range from the high and low integers. Don't forget to seed your random number once at the beginning of the program. Use a global constant variable for the number of tools available, and create your arrays based on that.

Explanation / Answer

#include<iostream>

#include<string>

#include <random>

#include <chrono>

#include<ctime>

typedef std::mt19937 G;

typedef std::uniform_int_distribution<> D;

G g;

//int*minSand;

//int*maxSand;

//std::string* toolName;

std::string toolName[4]={"shovel","spade","bucket","pail"};

int minSand[4]={1,4,2,12};

int maxSand[4]={42,20,30,15};

int choice;

int userSand,RobinSand;

std::string winner;

int RandomBetween(int low, int high)

{

/*D d(low, high);

return d(g);*/

return low+rand()%(high-low+1);

}

void PromptUser()

{

choice=1;

while (choice>=0&&choice<5)

{

std::cout<<"Which tool would you like to use? (1-4, 0 to list) -> ";

std::cin>>choice;

if(choice==0){

for (int i = 0; i < 4; i++)

{

std::cout<<"["<<i+1<<"] "<<toolName[i]<<" ( "<<minSand[i]<<" to "<<maxSand[i]<<" feet)"<<std::endl;

}

}

else

{

break;

}

}

}

void DoUserTurn()

{

PromptUser();

userSand+=RandomBetween(minSand[choice-1],maxSand[choice-1]);

}

void DoComputerTurn()

{

/*D d1(0,toolName->length()-1);

int ch=d1(g);*/

int ch=rand()%4;

RobinSand+=RandomBetween(minSand[ch],maxSand[ch]);

}

void DisplayInfo()

{

std::cout<<"You"<<userSand<<std::endl;

std::cout<<"Robin"<<RobinSand<<std::endl;

}

bool Winner()

{

if (userSand==RobinSand)

{

winner="not decided";

return false;

}

if( (userSand>=100)&&(userSand>RobinSand))

{

winner="You";

return true;

}

else if((RobinSand>=100)&&(RobinSand>userSand))

{

winner="Robin";

return true;

}

return false;

}

int main()

{

srand(time(NULL));

std::cout<<"Welcome to the "Sandcastles in the Sand" game."<<std::endl;

std::cout<<"Your opponent is Robin. Good luck!"<<std::endl;

userSand=0;RobinSand=0;

while(!Winner())

{

DoUserTurn();

DoComputerTurn();

DisplayInfo();

}

std::cout<<"the winner is"<<winner<<std::endl;

return 0;

}