Question

In: Computer Science

Write a program for a (very) rudimentary shooter "game". You are the only shooter and you...

Write a program for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function until you run out of ammo, at which point you lose. Report both hits and misses (see Sample Outputs). If your shot is a hit as determined by the value returned by shoot, your code should lower the enemy's health. If you are lucky, the health of the enemy will be reduced to zero before you run out of ammo. If this happens, report the enemy's demise and use the break keyword to stop the loop. You have won.
Sample Output 1
Shot fired. Enemy was hit!
Shot fired. Shot missed
Shot fired. Enemy was hit!
Shot fired. Enemy was hit!
Shot fired. Enemy was hit!
Shot fired. Enemy was hit!
Enemy destroyed. You won!
GAME OVER

Sample Output 2
Shot fired. Shot missed
Shot fired. Enemy was hit!
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Enemy was hit!
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Shot missed
Shot fired. Enemy was hit!
You are out of ammo! You lose!
GAME OVER

Solutions

Expert Solution

Since the language was not specified, I wrote the program in C++. Please refer to the program given below:

#include<bits/stdc++.h>
using namespace std;

//this function is used to generate a random number between 0 and 1
bool shoot(){
   int n;
   cout<<"Shot fired.";
   n = rand() % 2; //rand is a function that is used to generate a random number
   if(n)
       return true; //if n = 1
   else
       return false; //if n = 0
}

int main(){
   int ammo = 10, health = 5;
   bool result;
   srand(time(NULL)); //srand sets the starting point for producing a series of pseudo-random integers
   while(ammo){ //if ammo = 0 loop will end
       result = shoot();
       if(result == true){
           cout<<" Enemy was hit!"<<endl;
           health--; //decrement the health by 1 every time the enemy is hit
           if(health == 0){
               cout<<"Enemy destroyed. You won!"<<endl;
               break;
           }
       }
       else
           cout<<" Shot missed!"<<endl;
       ammo--; //decrement ammo by 1 after every iteration
   }
   if(health > 0) //if enemy is not dead
       cout<<"You are out of ammo! You lose!"<<endl;;
   cout<<"GAME OVER";
   return 0;
}


Related Solutions

Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter...
Write a program in PYTHON for a (very) rudimentary shooter "game". You are the only shooter and you start with ammo of 10. The one enemy doesn't shoot back and starts with health of 5. Code a custom function named shoot that prints "Shot fired" and returns True for a hit or False for a miss. Generate a random 0 to assign False or 1 to assign True. In the main function, use a while loop that runs the shoot function...
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through...
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through H and rows 1 through 20. Accept input in the first row of the screen. The commands are of the form column row entry, where entry is a number, a cell address preceded by a plus (e.g., +A5), a string, or a function preceded by an at sign, @. The functions are: max, min, avg, and sum. During execution of your program, build and...
Write a program in C that implements Conway's Game of Life. You will be expected to...
Write a program in C that implements Conway's Game of Life. You will be expected to apply the principles of Design by Contract to your code. The rules for the Game of Life are simple. The universe consists of a two-dimensional matrix of cells with each cell being alive or dead. For each generation every cell determines its next phase of life as follows: If the cell is alive: it dies if it has 0, 1, 4 or more living...
For this problem, you will write a program in which the computer plays a dice game...
For this problem, you will write a program in which the computer plays a dice game called Craps. You might need to explore some Python documentation on random() for this homework assignment. PROVIDE A GRAPHICAL FLOWCHART as part of your submission for this solution Rules: The player rolls two six-sided dice. After rolling, the sum of what the dice show is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is...
Write a program in Python to simulate a Craps game: 1. When you bet on the...
Write a program in Python to simulate a Craps game: 1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Write a program in Basic to play the game of Nim with acom­puter.
Write a program in Basic to play the game of Nim with acom­puter.
Develop a rudimentary Java program that will allow anyone to enter any number of grades and...
Develop a rudimentary Java program that will allow anyone to enter any number of grades and then calculate grade point average. For reference, a grade point average is a weighted average of a set of grades based on the relative number of credits. For example, a 1 credit “A” (4.0) should count twice as much as a .5 credit “C” (3.0), and a 1.5 credit “A+”(4.33) should count three times as much as the “C” and 1.5 times as much...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT