Basic Combat Game?

Hi, I am trying to pick up C++ I started about 2 weeks ago and took on a combat game as practice. Using Visual Studio 2015 to run.

I keep looking, and I feel like my code is logically correct, but it keeps looping. The game will not end, even though I have an if statement declaring a gameOver at the end of the code.

The idea of the game is to input a number of humans, then a number of demons, and the program should spit out who the victor is based off of the variables provided. A random generator is used to determine who hits first and later on again to see if the hit applies (hit or missed attack)(It is a 1-6 just cause I took it off the dice game I was practicing).

Here is my code. If someone could just point me in the right direction I would appreciate it! Thanks!


#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <ctime>
#include <random>

using namespace std;

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
int main()
{
	cout << "Demon Battle! \n";
	int numberOfHumans;
	int numberOfDemons;

	cout << "How many humans are there? : \n";
	cin >> numberOfHumans;
	cout << "There are " << numberOfHumans << " humans" << endl;

	cout << "How many demons are there? : \n";
	cin >> numberOfDemons;
	cout << "There are " << numberOfDemons << " demons" << endl;

	int DemonDamage;
	DemonDamage = numberOfDemons * 3;

	int DemonHit;

	int DemonHealth;
	DemonHealth = numberOfDemons * 2;

	int HumanDamage;
	HumanDamage = numberOfHumans * 1.7;

	int HumanHit;

	int HumanHealth;
	HumanHealth = numberOfHumans * 4;

	int firstAttack;

	bool gameOver = false;





do {

		srand(time(NULL));
		firstAttack = rand() % 6 + 1;
		if (firstAttack >= 4) {
			cout << "The Demons are strong under this dark sky!  They attack first!\n";
			srand(time(NULL));
			DemonHit = rand() % 6 + 1;
			if (DemonHit >= 2) {
				HumanHealth - DemonDamage == HumanHealth;
				cout << "Humans have taken a hit!  Human health is now: \n" << HumanHealth;
			}
			else {
				cout << "Demon attack has no effect on the humans!";
			}
		}
		else {
			cout << "Humans are strong in the daylight!  They attack first! \n";
			srand(time(NULL));
			HumanHit = rand() % 6 + 1;
			if (HumanHit >= 3) {
				DemonHealth - HumanDamage == DemonHealth;
				cout << "Demons have taken a hit! Demon health is now: \n" << DemonHealth;
			}
			else {
				cout << "Human attack has had no effect on the demons! \n";
			}
		}

		if (HumanHealth <= 0 || DemonHealth <= 0) {
			gameOver = true;
			cout << "The war has ended!  Battle Results: \n";
			cout << "Demons: " << DemonHealth << "\n";
			cout << "Humans: " << HumanHealth << "\n";
		}
} while (gameOver == false);

		system("PAUSE");
    return 0;
}
Last edited on
lines 41,45,57: Do not call srand() within a loop or a random number function. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Line 48, 60: These lines don't do anything and don't make any sense. You're not decrementing HumanHealth or DemonHealth so neither one will go to 0.
Last edited on
closed account (E0p9LyTq)
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
#include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
   std::srand(std::time(NULL));

   std::cout << "Demon Battle! \n";

   std::cout << "How many humans are there? ";
   int numberOfHumans;
   std::cin >> numberOfHumans;
   std::cout << "There are " << numberOfHumans << " humans\n";

   std::cout << "How many demons are there? ";
   int numberOfDemons;
   std::cin >> numberOfDemons;
   std::cout << "There are " << numberOfDemons << " demons\n";

   bool gameOver = false;

   do
   {
      int DemonDamage = numberOfDemons * 3;
      int DemonHealth = numberOfDemons * 2;
      int DemonHit;

      int HumanDamage = numberOfHumans * 1.7;
      int HumanHealth = numberOfHumans * 4;
      int HumanHit;

      std::cout << "\n";
      int firstAttack = std::rand() % 6 + 1;

      if (firstAttack >= 4) 
      {
         std::cout << "The Demons are strong under this dark sky!  They attack first!\n";

         DemonHit = std::rand() % 6 + 1;

         if (DemonHit >= 2) 
         {
            HumanHealth -= DemonDamage;
            std::cout << "Humans have taken a hit!  Human health is now: " << HumanHealth << "\n";
         }
         else 
         {
            std::cout << "Demon attack has no effect on the humans!\n";
         }
      }
      else
      {
         std::cout << "Humans are strong in the daylight!  They attack first! \n";

         HumanHit = std::rand() % 6 + 1;

         if (HumanHit >= 3) 
         {
            DemonHealth -= HumanDamage;
            std::cout << "Demons have taken a hit! Demon health is now: " << DemonHealth << "\n";
         }
         else 
         {
            std::cout << "Human attack has had no effect on the demons! \n";
         }
      }

      if (HumanHealth <= 0 || DemonHealth <= 0) 
      {
         gameOver = true;
         std::cout << "The war has ended!  Battle Results: \n";
         std::cout << "Demons: " << DemonHealth << "\n";
         std::cout << "Humans: " << HumanHealth << "\n";
      }
   } while (gameOver == false);
}
Last edited on
Thank you!! I tried it out and I got it out of the looping state. I'm going to keep messing around with it and trying new things out. Big help, thanks again AbstractionAnon and FurryGuy.
Last edited on
closed account (E0p9LyTq)
binghamhunter wrote:
I'm going to keep messing around with it and trying new things out.

Look at replacing your C library psuedo-random number functions with the C++ STL <random> library. :)
http://www.cplusplus.com/reference/random/
Topic archived. No new replies allowed.