1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//Choices from the menu and the amount of rounds left
const int CANNON_CHOICE = 1,
GRENADE_CHOICE = 2,
RIFLE_CHOICE = 3;
int CANNON_ROUNDS = 3,
GRENADE_ROUNDS = 4,
COMP_CANNON_ROUNDS = 3,
COMP_GRENADE_ROUNDS = 4;
//Getting the system time, seeding the random number
//generator, and the damages that each weapon does
unsigned seed = time(0);
srand(seed);
int cannon;
int grenade;
int rifle;
//The health of the user and the computer
int userHealth = 100,
compHealth = 100;
//The user's and computer's choice variables
int userChoice,
compChoice = 1 + rand() %3;
//Directions
cout << "You must do battle with the computer!\nEach player has 100 health as of now, begin!" << endl;
//Big picture loop, continue turns until one player's health is zero or less
while(userHealth > 0 && compHealth > 0)
{
cout << "1. Cannon with " << CANNON_ROUNDS << " rounds left" << endl
<< "2. Grenade with " << GRENADE_ROUNDS << " rounds left" << endl
<< "3. Rifle" << endl;
cin >> userChoice;
switch(userChoice)
{
case CANNON_CHOICE:
{
int cannon = 10 + rand() %15;
compHealth = compHealth - cannon;
CANNON_ROUNDS = CANNON_ROUNDS - 1;
cout << "The computer's health is now " << compHealth << ".";
break;
}
case GRENADE_CHOICE:
{
grenade = 7 + rand() %12;
compHealth = compHealth - grenade;
GRENADE_ROUNDS = GRENADE_ROUNDS - 1;
cout << "The computer's health is now " << compHealth << ".";
break;
}
case RIFLE_CHOICE:
{
rifle = 3 + rand() %8;
compHealth = compHealth - rifle;
cout << "The computer's health is now " << compHealth << ".";
break;
}
}
switch(compChoice)
{
case CANNON_CHOICE:
{
int cannon = 10 + rand() %15;
userHealth = userHealth - cannon;
COMP_CANNON_ROUNDS = COMP_CANNON_ROUNDS - 1;
cout << "The computer chose the cannon!\nYour health is now " << userHealth << endl;
break;
}
case GRENADE_CHOICE:
{
grenade = 7 + rand() %12;
userHealth = userHealth - grenade;
COMP_GRENADE_ROUNDS = COMP_GRENADE_ROUNDS - 1;
cout << "The computer chose the grenade!\nYour health is now " << userHealth << endl;
break;
}
case RIFLE_CHOICE:
{
rifle = 3 + rand() %8;
userHealth = userHealth - rifle;
cout << "The computer chose the rifle!\nYour health is now " << userHealth << endl;
break;
}
}
}
//Closing if statement
if(userHealth > 0)
{
cout << "Congratulations! You have defeated the evil computer!";
}
else
{
cout << "Better luck next time, the computer has won!";
}
}
|