I am having issues with my program and am looking for some help. My program is supposed to randomly choose who gets to go first, the user or the computer. The program does that just fine, when I input the weapon I want to use it does the proper calculations and subtracts the right amount of randomly generated damage from the computer's health and vice versa if the computer gets to go first. Whether it is the user or the computer going first, after you press enter the program just stops. It is supposed to keep going until either the user or the computer's health reaches zero. I was wondering if I could get some assistance as to where my code is preventing this from happening. Any help would be appreciated, I am a novice with the C++ language. Thank you!
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main() // Setting the "stage" for the code to follow.
{
int Player_Health = 100; // Creating the necessary variables for the program.
int Comp_Health = 100;
int Turn;
int Weapon_Selection;
int Comp_Weapon;
int Cannon_Dmg;
int Grenade_Dmg;
int Rifle_Dmg;
int Player_Cannon_Ammo = 3;
int Player_Grenade_Ammo = 4;
int Comp_Cannon_Ammo = 3;
int Comp_Grenade_Ammo = 4;
srand(static_cast<unsigned int>(time(0)));
Turn = rand() % 2; // Randomizing who gets to go first.
while(Player_Health >= 0 && Comp_Health >= 0)
{
if(Turn == 0) // Player gets to go first.
{
cout << "You get to go first!\n";
cout << "Select a weapon using number keys 1, 2, or 3.\n";
cout << "1. Cannon\n";
cout << "2. Grenade\n";
cout << "3. Rifle\n";
cout << "\n";
cout << "You select: ";
cin >> Weapon_Selection;
while((Weapon_Selection < 1 || Weapon_Selection > 3) || (Weapon_Selection == 1 && Player_Cannon_Ammo == 0)
|| (Weapon_Selection == 2 && Player_Grenade_Ammo == 0))
{
cout << "Please enter a valid option.\n";
cout << "You select: ";
cin >> Weapon_Selection;
}
switch(Weapon_Selection)
{
case 1: // Player chooses to shoot the cannon.
Cannon_Dmg = 10 + rand() % 6;
Comp_Health = Comp_Health - Cannon_Dmg;
cout << "You caused " << Cannon_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout <<" The computer's health is " << Comp_Health << endl;
Player_Cannon_Ammo = Player_Cannon_Ammo - 1;
break;
case 2: // Player chooses to lob a grenade.
Grenade_Dmg = 7 + rand() % 6;
Comp_Health = Comp_Health - Grenade_Dmg;
cout << "You caused " << Grenade_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Player_Grenade_Ammo = Player_Grenade_Ammo - 1;
break;
case 3: // Player chooses to shoot the rifle.
Rifle_Dmg = 3 + rand() % 6;
Comp_Health = Comp_Health - Rifle_Dmg;
cout << "You caused " << Rifle_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
break;
}
}
else // Computer gets to go first.
{
Comp_Weapon = rand() % 3;
switch(Comp_Weapon) // Computer randomly selects a weapon.
{
case 1:
Cannon_Dmg = 10 + rand() % 6;
Player_Health = Player_Health - Cannon_Dmg;
cout << "Your enemy used a cannon and caused " << Cannon_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Comp_Cannon_Ammo = Comp_Cannon_Ammo - 1;
break;
case 2:
Grenade_Dmg = 7 + rand() % 6;
Player_Health = Player_Health - Grenade_Dmg;
cout << "Your enemy used a grenade and caused " << Grenade_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Comp_Grenade_Ammo = Comp_Grenade_Ammo - 1;
break;
case 3:
Rifle_Dmg = 3 + rand() % 6;
Player_Health = Player_Health - Rifle_Dmg;
cout << "Your enemy used a rifle and caused " << Rifle_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
break;
}
}
if (Comp_Health < 0)
cout << "Congratulations, you beat your enemy!" << endl;
if (Player_Health < 0)
cout << "You have been defeated by your enemy!" << endl;;
cin.ignore(2);
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire