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 Python program to simulate a very simple game of 21 •Greet the user. •Deal...
Write a Python program to simulate a very simple game of 21 •Greet the user. •Deal the user a card and display the card with an appropriate message. •Deal the user another card and display the card .•Ask the user if they would like a third card. If so, deal them another card and display the value with an appropriate message. •Generate a random number between 10 and 21 representing the dealer’s hand. Display this value with an appropriate message....
in C language only. Write a program called gamecards.c that has a card game logic by...
in C language only. Write a program called gamecards.c that has a card game logic by comparing the cards from 4 people and calculating which player has the best card. 1. Every card must be represented by exactly two chars representing a rank and a suit. ranks: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. ('T' = 10) Suits: 'H', 'D', 'S', 'C'. 7D represents the “7 of Diamond” etc.. 2. Write a function called...
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...
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...
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...
This is a very simple program that will ONLY print a heading and columns of text...
This is a very simple program that will ONLY print a heading and columns of text to emulate the following information: Sales This Month Admin Fee Commission Rate ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- Less than $10,000 $150 5% $10,000 – 14,999 $150 10% $15,000 – 17,999 $150 12% $18,000 – 21,999 $200 15% $22,000 or more $200 16% ******Salesperson of the month****** Name: STUDENT NAME ID: STUDENT ID public class SalesResult { public static void main(String[] args) { System.out.println(); . . ....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT