Create a game where a the user and the computer compete to build the biggest san
ID: 3531410 • Letter: C
Question
Create a game where a 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.
You will need to determine which parameters and of what type you need to send to each function. You may choose to use global variables for some things, but you should justify your decision.
Make sure to adhere to functional cohesion and functional equivalence. Generalize wherever possible. Don't write the code as if there is only and will always be 4 tools. Use a global constant variable for the number of tools available, and create your arrays based on that.
Create amodule structure chartshowing the above listed functions and how they relate to each other, as well as any other functions you are using. Submit this structure chart as an image file (.jpg, .gif, .pdf etc.)
?Sample Output:
Welcome to the "Sandcastles in the Sand" game. Your opponent is Robin. Good luck!Round 1 Which tool would you like to use? (1-4, 0 to list) -> 0 [1] shovel ( 1 to 42 feet) [2] spade ( 4 to 20 feet) [3] bucket ( 2 to 30 feet) [4] pail ( 12 to 15 feet) Which tool would you like to use? (1-4, 0 to list) -> 1
You picked shovel. It added 32 feet to the height of your castle. Robin picked spade. It added 20 feet to the height of her castle. Your castle is 32 feet high, and Robin's castle is 20 feet high. You are winning!
Round 2 Which tool would you like to use? (1-4, 0 to list) -> 9 You only have 4 tools to choose from. Which tool would you like to use? (1-4, 0 to list) -> 3
You picked bucket. It added 20 feet to the height of your castle. Robin picked shovel. It added 39 feet to the height of her castle. Your castle is 52 feet high, and Robin's castle is 59 feet high. Robin is winning!
Round 3 Which tool would you like to use? (1-4, 0 to list) -> 2
You picked spade. It added 12 feet to the height of your castle. Robin picked shovel. It added 42 feet to the height of her castle. Your castle is 64 feet high, and Robin's castle is 101 feet high. Robin won! You are sad.
Create a game where a 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.
You will need to determine which parameters and of what type you need to send to each function. You may choose to use global variables for some things, but you should justify your decision.
Make sure to adhere to functional cohesion and functional equivalence. Generalize wherever possible. Don't write the code as if there is only and will always be 4 tools. Use a global constant variable for the number of tools available, and create your arrays based on that.
Create amodule structure chartshowing the above listed functions and how they relate to each other, as well as any other functions you are using. Submit this structure chart as an image file (.jpg, .gif, .pdf etc.)
Welcome to the "Sandcastles in the Sand" game. Your opponent is Robin. Good luck!
Round 1 Which tool would you like to use? (1-4, 0 to list) -> 0 [1] shovel ( 1 to 42 feet) [2] spade ( 4 to 20 feet) [3] bucket ( 2 to 30 feet) [4] pail ( 12 to 15 feet) Which tool would you like to use? (1-4, 0 to list) -> 1
You picked shovel. It added 32 feet to the height of your castle. Robin picked spade. It added 20 feet to the height of her castle. Your castle is 32 feet high, and Robin's castle is 20 feet high. You are winning!
Round 2 Which tool would you like to use? (1-4, 0 to list) -> 9 You only have 4 tools to choose from. Which tool would you like to use? (1-4, 0 to list) -> 3
You picked bucket. It added 20 feet to the height of your castle. Robin picked shovel. It added 39 feet to the height of her castle. Your castle is 52 feet high, and Robin's castle is 59 feet high. Robin is winning!
Round 3 Which tool would you like to use? (1-4, 0 to list) -> 2
You picked spade. It added 12 feet to the height of your castle. Robin picked shovel. It added 42 feet to the height of her castle. Your castle is 64 feet high, and Robin's castle is 101 feet high. Robin won! You are sad.
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.