Sep 23, 2009 at 9:31pm Sep 23, 2009 at 9:31pm UTC
You should read input after displaying the information and you should use a single variable to get input
Sep 23, 2009 at 9:50pm Sep 23, 2009 at 9:50pm UTC
Insert this in while loop to allow repeated attacks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
while (player1health >0 && player2health >0)
{
if (response == 1)
{
cout<< "Player 1 did" << player2health - attackdamage << "Damage to player 2!" ;
}
if (response == 2)
{
cout<< "Player 2 did" << player1health - attackdamage << "Damage to player 1!" ;
}
if (player1health <= 0)
{
cout << "Player 2 has won! \n" ;
}
if (player2health <= 0)
{
cout << "Player 1 has won! \n" ;
}
cin >> response;
}
Last edited on Sep 23, 2009 at 9:51pm Sep 23, 2009 at 9:51pm UTC
Sep 23, 2009 at 10:08pm Sep 23, 2009 at 10:08pm UTC
Whenever a player's health is at 0, it always says player one wins!
I don't see how it is happening...
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
#include <iostream>
#include <ctime>
#include <cstdlib>
int main(void )
{
srand((unsigned int )time(0));
using namespace std;
int response = 0;
int player1health = 100;
int player2health = 100;
int player2attackdamage = rand() % 100 + 0;
int player1attackdamage = rand() % 100 + 0;
cout << "Welcome to my turn - based game! " << " Player one presses one to attack player two, and player two use two to attack player one." ;
while (player1health >0 && player2health >0)
{
if (response == 1)
{
cout<< "Player 1 has " << player1health - player2attackdamage<< " Health left!" ;
}
if (response == 2)
{
cout<< "Player 2 has " << player2health - player1attackdamage << " Health Left!" ;
}
if (player1health - player2attackdamage<=0)
{
cout << "Player 2 has won! \n " ;
}
if (player2health - player1attackdamage<=0)
{
cout << "Player 1 has won! \n " ;
}
cin >> response;
}
system("pause" );
return 0;
}
Last edited on Sep 24, 2009 at 8:04pm Sep 24, 2009 at 8:04pm UTC