Hello! First, I apologize for the long post.
I am new to C++. I'm working on an assignment for school and I have a few errors I would like to work out.
The program executes, but there are a few things I would like to add but not sure how to implement it into the program..
1.) I would like to create a system pause in between the player output and the computer output. I have tried (you can see I have commented out at the end) a few different pauses, however none of them actually paused the game. I tried placing the pause in multiple places...
2.) This game is supposed to be turn based, so it is suppose to function where the player takes a turn, then the computer, then the player.. so on. It does that MOST of the time, however in the process of randomizing the weapon damage I think I randomized whether the computer takes a turn at all.
3.) I want to add a function where the player from the very beginning has an option of <1> easy <2> hard. Easy would mean that the computer randomly picks weapons (already have that as default). Hard would be where the computer depletes all cannons first. You can see where I have started it and commented it out.
Any help would be greatly appreciated!
Code:
// Issues...........
// Cannot get system pause to work.
// Cannot get easy or hard mode to work
// The program seems to randomize if the computer gets a turn at all.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int Player_Health = 100,
Computer_Health = 100,
Turn = 0,
Weapon_Choice,
Computer_Weapon,
easy = 1,
hard = 2,
difficultyChoice,
Cannon_Damage = 0,
Grenade_Damage = 0,
Rifle_Damage = 0;
int Player_CannonQty = 3;
int Player_GrenadeQty = 4;
int Computer_CannonQty = 3;
int Computer_GrenadeQty = 4;
// cout << "Please select <1> easy or <2> hard:" << endl;
//cin >> difficultyChoice;
//if (difficultyChoice == 1)
srand(static_cast<int>(time(0)));
do
{
if(Turn == 0)// Player Turn
{
cout << "\nPick a weapon. <1> <2> or <3>:\n";
cout << "1. Cannon\n";
cout << "2. Grenade\n";
cout << "3. Rifle\n";
cin >> Weapon_Choice;
//Validate weapon choice
while ((Weapon_Choice < 1 || Weapon_Choice > 3) || (Weapon_Choice == 1
&& Player_CannonQty == 0) || (Weapon_Choice == 2 && Player_GrenadeQty ==0) )
{
cout << "\nPlease enter a valid option\n";
cin >> Weapon_Choice;
}
switch(Weapon_Choice)
{
case 1: // player chooses to attack with cannon
Cannon_Damage = (10 + rand() % 6);
cout << "\nYou chose a cannon"<< endl;
Computer_Health = Computer_Health - Cannon_Damage;
cout << "You inflicted " << Cannon_Damage << " points on your enemy." << endl;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
Player_CannonQty = Player_CannonQty - 1;
break;
case 2:
Grenade_Damage = (7 + rand() % 13);
cout << "\nYou chose a grenade"<< endl;
Computer_Health = Computer_Health - Grenade_Damage;
cout << "You inflicted " << Grenade_Damage << " points on your enemy." << endl;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
Player_GrenadeQty = Player_GrenadeQty - 1;
break;
case 3:
Rifle_Damage = (3 + rand() % 9);
cout << "\nYou chose a Rifle"<< endl;
Computer_Health = Computer_Health - Rifle_Damage;
cout << "You inflicted " << Rifle_Damage << " points on your enemy." << endl;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
break;
}
}
Turn == 1; // Computer Turn
Computer_Weapon = rand() % 3;
switch(Computer_Weapon)
{
case 1:
Cannon_Damage = (10 + rand() % 6);
cout<<"\nYour opponent used a cannon and inflicted " << Cannon_Damage << " points on you." << endl;
Player_Health = Player_Health - Cannon_Damage;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
Computer_CannonQty = Player_CannonQty - 1;
break;
case 2:
Grenade_Damage = (7 + rand() % 6);
cout<<"\nYour opponent used a grenade and inflicted " << Grenade_Damage << " points on you." << endl;
Player_Health = Player_Health - Grenade_Damage;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
Computer_GrenadeQty = Computer_GrenadeQty - 1;
break;
case 3:
Rifle_Damage = (3 + rand() % 10);
cout<<"\nYour opponent used a rifle and inflicted " << Rifle_Damage << " points on you." << endl;
Player_Health = Player_Health - Rifle_Damage;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
break;
}
} while(Player_Health >= 0 && Computer_Health >= 0); //loops while both players are alive
if (Computer_Health < 0)
cout << "Congratulations! You won!" << endl;
if (Player_Health < 0)
cout << "YOU HAVE DIED! GAME OVER!" << endl;;
// std::cout<< "Press ENTER to continue..." << flush;
// cin.ignore(cin.rdbuf()->in_avail()+1);
return 0;
}