Code Problem

Ok guys, I'm trying to make a turn - based strategy game in which player one presses 1 to attack player two, and player two presses 2 to attack player one.
The problem is though is I can't get it to work how I want to.
Here is the code.
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
#include <iostream>
#include <ctime>
#include <cstdlib>
int main(void)
{
	using namespace std;
	srand((unsigned int)time(0));
	int wepkey1 = 1;
	cin >> wepkey1;
	int wepkey2 = 2;
	cin >> wepkey2;
    int player1health = 100;
	int player2health = 100;
	int attackdamage = rand() % 100 + 1;
	cout << "Welcome to my turn - based game!" << "Player 1 presses 1 to attack player 2, and player 2 use 2 to attack player 1. \n";
	if(wepkey1 == 1)
	{
	 	cout<< "Player 1 did" << player2health - attackdamage << "Damage to player 2!";
	}
	if(wepkey2 == 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";
	}
	system("pause");
	return 0;
}
Thanks for your help!
You should read input after displaying the information and you should use a single variable to get input
Here is the updated code
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
#include <iostream>
#include <ctime>
#include <cstdlib>
int main(void)
{
	using namespace std;
	srand((unsigned int)time(0));
	int response = 0;
    int player1health = 100;
	int player2health = 100;
	int attackdamage = rand() % 100 + 1;
	cout << "Welcome to my turn - based game!" << "Player 1 presses 1 to attack player 2, and player 2 use 2 to attack player 1. \n";
	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;
	
	system("pause");
	return 0;
}
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
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
Topic archived. No new replies allowed.