be turned in at the beginning of lab. Overall Assignment For assignment you are
ID: 3850704 • Letter: B
Question
be turned in at the beginning of lab. Overall Assignment For assignment you are towrite a computer game in which a player to locat and this extensive use of defuse a bomb hidden in a simple of. This game makes of the branches and building before it goes The extent find building is 0 loops, with specific constructs required in specific 0 tries to to 99 in all (3) directions, the player has 200 seconds losing makes it easier. the bomb before it explodes. Winning makes the game harder. Program Pseudocode 20, call srandOtime(NULL)) 1. Initialize. Explain the program to the set to 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 th %operator for this step. Set "won' to false. 3. Repeat the following steps initial Tries a for loo with a loo counter "tries usin which starts out at 0 and c as long as triesisless than initialTries, incrementing tries at the end of each iteration. ("tries" is the number o tries already completed.) For each iteration, do the following: a. Inform the user that they have 10 remaining Tries seconds left to find the bomb, where remaining initialTries tries. Tries b. Ask the user for a location to te reading in xGuess, yGuess, and zGuess. Ask for each value separately, and use while loops to only accept numbers from 0 to 99 c. Check to see if the guess is right, by comparing the X, Y, and Z values for equality. E.g. does xBomb equal xGuess, etc. You will need to use & for this test.) If the guess is correct, set "won" to true, increment tries, and then use break to exit the for loop early. d. Calculate the bomb detector signal strength using the following distance) formula, and report to useri iGuess iBo signal 1000 1 100 V3 4. After the loop completes, check "won" to see if the user found the bomb in time If the user won, print "Congratulations", and report the time used and remaining b. If they did not, print "BOOM" and report the time used. 5. sk the user if they want to play again, using a while loop to accept only a yes or no answer. (Use string input and comparison.) Set "repeat" to either true or false. the and conditional ope to add -1 to initialTries ifwon is true, or +1 if won is to repeat back to step 2 if "repeat" is true. Deve nd test st en add steps 5 after 4 are wo The summation under the square root-(xGuess-xBomby. yGuess- yBomb) Guess-zlBombExplanation / Answer
Solution: See the code below:
----------------------------------------------------
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cmath>
using namespace std;
int main() {
string name = "XYZ"; //name of student. replace the value with your name
string id = "ABC"; //ACCC netID of student. replace the value with your id
//description of program
string desc =
"This is a simple computer game in which a player attempts to locate "
"and defuse a bomb hidden in a building before it goes off.";
//instructions for user
string instructions =
"Please note, there is a bomb detector that reports a signal "
"from 0 to 1000 depending on how close you are to the bomb location and each "
"try will take 10 seconds to complete.";
cout << "#####################################" << endl;
cout << "Name:" << name << endl;
cout << "ACCC netID:" << id << endl;
cout << "Description:" << desc << endl;
cout << "Instructions:" << instructions << endl;
cout << "#####################################" << endl;
//initial tries alloted to a player
int initialTries = 20;
srand(time(NULL));
bool won, repeat;
int min = 0;
int max = 99;
//directions to look for bomb
int xBomb, yBomb, zBomb, xGuess, yGuess, zGuess;
//bomb detection signal
double signal = 0.0;
int remainingTries, tries;
do {
//initialize
xBomb = min + rand() % (max - min);
yBomb = min + rand() % (max - min);
zBomb = min + rand() % (max - min);
won = false;
//loop to perform tries
for (tries = 0; tries < initialTries; tries++) {
remainingTries = initialTries - tries;
cout << "You have " << (10 * remainingTries)
<< " seconds left to find the bomb." << endl;
cout << "Enter a location to test(x,y,z)(>=0 and <=99):" << endl;
while (1) {
cout << "x:";
cin >> xGuess;
if (xGuess >= 0 && xGuess <= 99)
break;
}
while (1) {
cout << "y:";
cin >> yGuess;
if (yGuess >= 0 && yGuess <= 99)
break;
}
while (1) {
cout << "z:";
cin >> zGuess;
if (zGuess >= 0 && zGuess <= 99)
break;
}
//check if location right
if (xGuess == xBomb && yGuess == yBomb && zGuess == zBomb) {
won = true;
tries++;
break;
}
//calculate bomb detection signal strength
signal = 1000
* (1
- (sqrt(
pow(xGuess - xBomb, 2)
+ pow(yGuess - yBomb, 2)
+ pow(zGuess - zBomb, 2)))
/ (100 * sqrt(3)));
cout<<"Bomb detection signal strength:"<<signal<<endl;
}
//check if won
if (won) {
cout << "Congratulations!!!" << endl;
cout << "Time used:" << (10 * tries) << endl;
cout << "Time remaining:" << (10 * remainingTries) << endl;
} else {
cout << "BOOM!!!!!" << endl;
cout << "Time used:" << (10 * tries) << endl;
}
//ask user if to play again
string choice;
while (1) {
cout << "Do you want to play again(yes/no)?";
getline(cin, choice);
if (choice.compare("yes") == 0) {
repeat = true;
break;
} else if (choice.compare("no") == 0) {
repeat = false;
break;
}
}
//update initial tries
initialTries += (won ? -1 : 1);
} while (repeat);
return 0;
}
--------------------------------------
Sample output:
----------------------------------
#####################################
Name:XYZ
ACCC netID:ABC
Description:This is a simple computer game in which a player attempts to locate
and defuse a bomb hidden in a building before it goes off.
Instructions:Please note, there is a bomb detector that reports a signal
from 0 to 1000 depending on how close you are to the bomb location and each
try will take 10 seconds to complete.
#####################################
You have 200 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:23
y:57
z:21
Bomb detection signal strength:539.783
You have 190 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:25
y:60
z:20
Bomb detection signal strength:532.738
You have 180 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:23
y:57
z:56
Bomb detection signal strength:729.877
You have 170 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:40
y:70
z:80
Bomb detection signal strength:765.835
You have 160 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:60
y:80
z:80
Bomb detection signal strength:649.524
You have 150 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:35
y:70
z:85
Bomb detection signal strength:803.786
You have 140 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:32
y:73
z:87
Bomb detection signal strength:820.278
You have 130 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:28
y:75
z:90
Bomb detection signal strength:842.944
You have 120 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:25
y:78
z:91
Bomb detection signal strength:851.788
You have 110 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:23
y:80
z:92
Bomb detection signal strength:856.009
You have 100 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:19
y:83
z:93
Bomb detection signal strength:861.677
You have 90 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:16
y:87
z:98
Bomb detection signal strength:855.316
You have 80 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:16
y:87
z:93
Bomb detection signal strength:852.465
You have 70 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:18
y:85
z:92
Bomb detection signal strength:854.856
You have 60 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:19
y:93
z:94
Bomb detection signal strength:815.158
You have 50 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:19
y:84
z:94
Bomb detection signal strength:858.343
You have 40 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:19
y:83
z:94
Bomb detection signal strength:862.765
You have 30 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:19
y:85
z:96
Bomb detection signal strength:855.201
You have 20 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:19
y:80
z:93
Bomb detection signal strength:874.169
You have 10 seconds left to find the bomb.
Enter a location to test(x,y,z)(>=0 and <=99):
x:19
y:78
z:93
Bomb detection signal strength:881.819
BOOM!!!!!
Time used:200
Do you want to play again(yes/no)?Do you want to play again(yes/no)?no
--------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.