For this assignment you are to write a simple computer game in which a player at
ID: 3670057 • Letter: F
Question
For this assignment you are to write a simple computer game in which a player attempts to locate and defuse a bomb hidden m a building before it goes off. This game makes extensive use of branches and loops, with specific constructs required m specific situations. The extent of the building is 0 to 99 m all (3) directions, and the player initially has 200 seconds (20 tries) to find the bomb before it explodes. Winning makes the game harder, losing makes it easier. Program Pseudocode 1. Initialize. Explain the program to the user, set initial Tries to 20. call srand(time(null)). Declare all variables, including bool variables named 'Won" and "repeat". 2. Calculate bomb location as random integers for xBomb, yBomb, and zBomb in the range from 0 to 99 inclusive. (Use rand() and the % operator for this step.) 3. Repeat the following steps initial Tries times, using a for loopExplanation / Answer
/**C++ program that prompts user to enter the guess of x, y and z location.
Then if the user guess is correct of bomb location then exit the program and print
message otherwise repeat the program until the user tries are off.*/
//header files
#include<iostream>
#include<string>
#include<iomanip>
#include<time.h>
using namespace std;
int main()
{
cout<<"Bomb diffusion program ."<<endl;
//Set intialTries =20
int initialTries=20;
//set won and repeat boolean values
bool won=false;
bool repeat=true;
srand(time(NULL));
int xBomb,xGuess;
int yBomb,yGuess;
int zBomb,zGuess;
do
{
//generage random values for bomb location
xBomb=rand()%99;
yBomb=rand()%99;
zBomb=rand()%99;
//Calculate the remainging seconds
int seconds=initialTries*10;
//Set intialTries remainingTries
int remainingTries=initialTries;
//repeat until intialTries are off or the user guess is correct
for(int start=1;start<initialTries;start++)
{
cout<<"You have "<<seconds<<" to find the bomb"<<endl;
cout<<"Enter guess values of bomp location for x , y and z :";
cin>>xGuess>>yGuess>>zGuess;
if((xBomb==xGuess) && (yBomb==yGuess) && (zBomb==zGuess))
break;
//Calculate signal stregth
double signal=
1000*(1-sqrt(pow((double)(xGuess-xBomb),2.0)+pow((double)(yGuess-yBomb),2.0)+pow((double)(zGuess-zBomb),2.0))/100*sqrt(3.0));
cout<<"Bomb Detector signal strenght is "<<fixed<<setprecision(2)<<signal<<endl;
//decrease remaingTries
remainingTries--;
//calcuate seconds
seconds=remainingTries*10;
}
//Check if remaingTries are 0
if(remainingTries==0)
{
//print message and set won to false
cout<<"BOOM"<<" time used : "<<seconds;
won=false;
}
else
{
cout<<"Congratulations"<<" time used : "<<seconds;
won=false;
}
string choice=" ";
cout<<endl<<"Do you want to paly yes or no : ";
cin>>choice;
//read user input until user enter yes or no
while(repeat && (choice!="yes" && choice!="no"))
{
cout<<"Do you want to paly yes or no : ";
cin>>choice;
if(choice=="yes" || choice=="no")
repeat=false;
}
//Set repeat to true if choice is yes
if(choice=="yes")
repeat=true;
//Set repeat to false if choice is no
else if(choice=="no")
repeat=false;
//Check if won is true then increment the intialTries
if(won)
initialTries+=1;
//Decreament the intialTries by 1
else if(!won)
initialTries-=1;
}while(repeat);
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Bomb diffusion program .
You have 200 to find the bomb
Enter guess values of bomp location for x , y and z :48 96 58
Bomb Detector signal strenght is 312.83
You have 190 to find the bomb
Enter guess values of bomp location for x , y and z :51 45 26
Bomb Detector signal strenght is 235.15
You have 180 to find the bomb
Enter guess values of bomp location for x , y and z :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.