help in a 1v1 battle sim

I'm trying to make a 1v1 battle sim which will make random values and generate a random health for my opponent. This should run the attacks in turns till someone hits 0 health. I can sue the current while loop to test 1 sort of, but it doesnt repeat. And if I split it into 2 while loops to test both it runs infinite times. Any help please? :s

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  #include <cmath>
#include <cstdlib>
#include <iostream>
#include <ctime>

int main()
{
    using namespace std;
    
    int health = 100;
    int health2;
    int selection;
    int damage;
    int damage2;
    string name;
    
    system("COLOR 1f"); 
    
    cout << "Welcome to my 1v1 battle simulator" << endl;
    cout << "Your character has " << health << " health." << endl << endl;
    
    srand(time(0));
    selection = 1 + (rand()%4);
    
    switch (selection)
    {
           case 1: health2 = rand()%100;
                   cout << "Your oponent is called Peter."
                        << "" << endl // Enter the Description inside the ""
                        << "He has a total health of " << health2 << "." << endl;
                        name = "Peter";
                        break;
                        
           case 2: health2 = rand()%100;
                   cout << "Your oponent is called Gavin."
                        << "" << endl // Enter the Description inside the ""
                        << "He has a total health of " << health2 << "." << endl;
                        name = "Gavin";
                        break;
                        
           case 3: health2 = rand()%100;
                   cout << "Your oponent is called Rich."
                        << "" << endl // Enter the Description inside the ""
                        << "He has a total health of " << health2 << "." << endl;
                        name = "Rich";
                        break;
                        
           case 4: health2 = rand()%100;
                   cout << "Your oponent is called Kyle."
                        << "" << endl // Enter the Description inside the ""
                        << "He has a total health of " << health2 << "." << endl;
                        name = "Kyle";
                        break;
    }
    
    cout << endl << "Let the battle begin!" << endl
         << "You go first!" << endl << endl;
         
    
    damage = rand()%100;
    damage2 = rand()%100;
    
    
    while ( damage < health2)
    {                 
          if ( 0 < health)
          {   
              //damage = rand()%100;  
              cout << "You have done " << damage << " damage to " << name << "." << endl;
              health2 = health2 - damage;
              cout << endl << name << " now has " << health2 << " health left." << endl;
          }
          
          if ( 0 >= health2)
          {
               cout << name << " has died! You have won the battle!" << endl;
          }

          if ( 0 < health2)
          {    
               //damage2 = rand()%100;
               cout << endl << "It is now " << name << "'s turn to hit." << endl
                    << name << " did " << damage2 << " damage to you.";
               health = health - damage2; 
               cout << endl << "You now have " << health << " health left." << endl;        
          }
    
          if ( 0 >= health)
          {
               cout << "You have died! " << name << " has won the battle!" << endl;
          }
    }   
                                           
    system("pause");
    return 0;
}
Last edited on
Solved it myself! If anyone else is having a similar problem use a for(;;) loop and just break; when the character dies :)
Topic archived. No new replies allowed.