Why won't this run properly?

I created a little game based off of a video by MakingGamesWithBen. It was a challenge video though, so I didn't copy any of his code. I feel like my code should work, but no matter what I do it messes up. It's supposed to be a game where you input a number of allies and enemies, it calculates the battles, and tells you who wins. I even added a temporary counter so I could see the numbers of allies and foes (dubbed Plants and Zombies). Instead, the code just waits until the plants numbers hit 0, while the zombies go into the negatives.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  #include <iostream>
#include <string>
#include <random>
#include <ctime>

using namespace std;

int main()
{
	mt19937 randomGenerator((unsigned int)time(NULL));
	uniform_int_distribution<int> attackRollP(2, 10);
	uniform_int_distribution<int> attackRollZ(1, 12);


	int Plants;
	int Zombies;
	int plantHealth = 200;
	int zombieHealth = 300;
	char turn = 'P';
	
	




	cout << ("How many plants are there? ");
	cin >> Plants;
	cout << ("How many zombies are there? ");
	cin >> Zombies;

	cout << ("There are ") << Plants << (" plants and ") << Zombies << (" zombies.") << endl;

	while (true) {
		if ((Plants > 0) || (Zombies > 0)) {
			{
				if (turn = 'Z') {
					zombieHealth = zombieHealth - attackRollP(randomGenerator);
					turn = 'P';
				}

				if (turn = 'P') {
					plantHealth = plantHealth - attackRollZ(randomGenerator);
					turn = 'Z';
				}
				

				if (plantHealth <= 0) {
					plantHealth = 200;
					Plants--;

				}


				if (zombieHealth <= 0) {
					zombieHealth = 300;
					Zombies--;
				}
				cout << Plants << (" ") << Zombies << endl;
			}
		}	
	}
	cout << "The battle is done." << endl;

	if ((Zombies > 0) && (Plants <= 0)) {
		cout << "Zombies win!" << endl;
		system("PAUSE");
	}

	if ((Plants > 0) && (Zombies <= 0)) {
		cout << "Plants win!" << endl;
		system("PAUSE");
	}
	






	return 0;
}


If the code looks weird it's because I messed around with it a bunch before asking.
Last edited on
if (turn = 'Z') The comparison operator is == not =

Enabling all warnings in your compiler will help you to spot such problems.
I switched those, but it still didn't work. I believe that when the number hits 0 of the side with more "troops", it ends. It doesn't care about the side that you input a lower number into. But I still have no clue what the fix could be.
Line 33: You have no exit from your loop. You will never reach line 62.

Try eliminating the if statement at line 34 and replacing your while statement:
 
  while (Zombies > 0 && Plants > 0)


Topic archived. No new replies allowed.