Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Turn Based Battle

madelinelisemadelinelise United StatesPosts: 2Member

I apologize for such a long post...

Hello. I am VERY new to C++ (and programming in general). There is an assignment I'm working on for school. I'm stuck. I feel like I have a foundation for the program. It executes, but there a few holes.

**1.) ** The teacher wants us to be able to choose from hard and easy. Easy mode sets the computer to randomly pick weapons, hard mode has the computer pick all cannons first.

I cannot figure out where to put this! As you can see I have an if statement based on input but I don't know where to put it. The easy mode works fine, however when I tried hard mode it would not turn to the computer's turn?

2.) The program is supposed to count how many of each weapon you have used... For example, the player is only allowed 3 cannons, so the program should say "You are out of cannons!" - on the 4th attempt. I have no idea. As you can see I had totalCannon -= Cannon, that didn't work.

3.) I would like to pause the program in between the player selection output and the computer output. I have tried this:

std::cout<< "Press ENTER to continue..." << flush;
cin.ignore(cin.rdbuf()->in_avail()+1);

and this:

cout<<"Press enter to continue";
cin.get();

However, no luck.

4.) I cannot get the damage from each weapon to stay within the range I want it to.

Here is the assignment description.

PLEASE HELP!

Death Battle

You and an opponent will battle each other to the DEATH!! Each time you battle, you will
inflict damage on your opponent, but your opponent will do the same to you.
You will be able to choose from 3 weapons, each inflicting damage (or reducing health) of a
certain range. The damage incurred will be selected randomly. The catch is that you may only
use some of the weapons a limited number of times.
Weapon / Damage Range / Number of rounds
Canon / 10-15 / 3 /
Grenade / 7-12 / 4 /
Rifle / 3-8 / Unlimited /

You and your opponent will both start with a health level of 100. At the beginning of each turn,
the user will select the weapon for the round. The computer will randomly select how much
damage to incur from the range listed, and subtract that from the remaining health. Then, the
computer will select a weapon at random and randomly select how much damage to incur from
the range listed. After each turn, list the remaining health for both teams.

The team whose health reaches 0 first is the loser. The team with health remaining is the victor.

Nice additions:
 Randomly select which team fires first.
 Allow the player to play again after the game ends.

Note:
The computer team shouldn't be able to fire more rounds of each weapon than the maximum, just
like the player.

  #include <iostream>
#include <ctime>
using namespace std;

int main()
{
int Player_Health = 100,
Computer_Health = 100,
Counter = 0,
Weapon_Choice,
Computer_Weapon,
easy = 1,
hard = 2,
difficultyChoice,
Cannon = 1,
Grenade = 2,
Rifle = 3,
totalCannon = 3,
totalGrenade = 4;


cout << "Please select <1> easy or <2> hard:" << endl;
cin >> difficultyChoice;
srand(static_cast<int>(time(0)));

if (difficultyChoice == 1)


do
{

if(Counter == 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 > 3 || Weapon_Choice < 0)
{
cout << "Please enter a valid menu choice:";
cin >> Weapon_Choice;
}



switch(Weapon_Choice)
{
case 1:// player chooses to attack with cannon
Cannon = (1 + rand() % 10 + 15);//attack power can be between 10-15
cout << "You chose a cannon"<< endl;
Computer_Health = Computer_Health - Cannon;
cout << "You inflicted " << Cannon << " points on your enemy." << endl;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
totalCannon -= Cannon;
if (totalCannon == 0)
cout << "You are out of Cannon's!" << endl;


break;


case 2:
Grenade = (1 + rand() % 7 + 12);//attack power can be between 10-15
cout << "You chose a grenade"<< endl;
Computer_Health = Computer_Health - Grenade;
cout << "You inflicted " << Grenade << " points on your enemy." << endl;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
totalGrenade -= Grenade;

break;
case 3:
Rifle = (1 + rand() % 3 + 8);//attack power can be between 10-15
cout << "You chose a Rifle"<< endl;
Computer_Health = Computer_Health - Rifle;
cout << "You inflicted " << Rifle << " points on your enemy." << endl;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;

break;
}

}

Counter == 1; // Computer Turn

Computer_Weapon = rand() % 3;


switch(Computer_Weapon)
{

case 1:
Cannon = 1+rand() % (10 + 15);
cout<<"\nYour opponent used a cannon and inflicted " << Cannon << " points on you." << endl;
Player_Health = Player_Health - Cannon;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
totalCannon -= Cannon;
break;

case 2:
Grenade = 1+rand() % (7 + 12);
cout<<"\nYour opponent used a grenade and inflicted " << Grenade << " points on you." << endl;
Player_Health = Player_Health - Grenade;
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Computer_Health << endl;
totalGrenade -= Grenade;
break;

case 3:
Rifle = 1+rand() % (3 + 8);
cout<<"\nYour opponent used a rifle and inflicted " << Rifle << " points on you." << endl;
Player_Health = Player_Health - Rifle;
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;;



return 0;
}

Comments

Sign In or Register to comment.