Turn-based battle

UPDATE. I have fixed some of these problems, but others still remain.

I apologize for such a long post...

Hello. I am VERY new to C++ (and programming in general). There is an assignment I'm working on for school. I'm stuck. I feel like I have a foundation for the program. It executes, but there a few holes.

1.) The teacher wants us to be able to choose from hard and easy. Easy mode sets the computer to randomly pick weapons, hard mode has the computer pick all cannons first.

I cannot figure out where to put this! As you can see I have an if statement based on input but I don't know where to put it. The easy mode works fine, however when I tried hard mode it would not turn to the computer's turn?


2.) The program is supposed to count how many of each weapon you have used... For example, the player is only allowed 3 cannons, so the program should say "You are out of cannons!" - on the 4th attempt. I have no idea. As you can see I had totalCannon -= Cannon, that didn't work.

3.) I would like to pause the program in between the player selection output and the computer output. I have tried this:
1
2
std::cout<< "Press ENTER to continue..." << flush;
cin.ignore(cin.rdbuf()->in_avail()+1);

and this:
1
2
cout<<"Press enter to continue";
cin.get();

However, no luck.


4.) I cannot get the damage from each weapon to stay within the range I want it to.

5.) This game is supposed to be turn based, so it is suppose to function where the player takes a turn, then the computer, then the player.. so on. It does that MOST of the time, however in the process of randomizing the weapon damage I think I randomized whether the computer takes a turn at all.

Here is the assignment description.

PLEASE HELP!

Death Battle

You and an opponent will battle each other to the DEATH!! Each time you battle, you will
inflict damage on your opponent, but your opponent will do the same to you.
You will be able to choose from 3 weapons, each inflicting damage (or reducing health) of a
certain range. The damage incurred will be selected randomly. The catch is that you may only
use some of the weapons a limited number of times.
Weapon / Damage Range / Number of rounds
Canon / 10-15 / 3 /
Grenade / 7-12 / 4 /
Rifle / 3-8 / Unlimited /

You and your opponent will both start with a health level of 100. At the beginning of each turn,
the user will select the weapon for the round. The computer will randomly select how much
damage to incur from the range listed, and subtract that from the remaining health. Then, the
computer will select a weapon at random and randomly select how much damage to incur from
the range listed. After each turn, list the remaining health for both teams.

The team whose health reaches 0 first is the loser. The team with health remaining is the victor.

Nice additions:
 Randomly select which team fires first.
 Allow the player to play again after the game ends.

Note:
The computer team shouldn't be able to fire more rounds of each weapon than the maximum, just
like the player.



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Issues...........

// Cannot get system pause to work.
// Cannot get easy or hard mode to work
// The program seems to randomize if the computer gets a turn at all.


#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


int main()
{
    int Player_Health = 100,
    Computer_Health = 100,
    Turn = 0,
    Weapon_Choice,
    Computer_Weapon,
    easy = 1,
    hard = 2,
    difficultyChoice,
    Cannon_Damage = 0,
    Grenade_Damage = 0,
    Rifle_Damage = 0;
    int Player_CannonQty = 3;
    int Player_GrenadeQty = 4;
    int Computer_CannonQty = 3;
    int Computer_GrenadeQty = 4;
    
    
    // cout << "Please select <1> easy or <2> hard:" << endl;
    //cin >> difficultyChoice;
    
    
    //if (difficultyChoice == 1)
    
    
    srand(static_cast<int>(time(0)));
    
    do
    {
        
        if(Turn == 0)// Player Turn
        {
            cout << "\nPick a weapon. <1> <2> or <3>:\n";
            cout << "1. Cannon\n";
            cout << "2. Grenade\n";
            cout << "3. Rifle\n";
            cin >> Weapon_Choice;
            
            //Validate weapon choice
            
            while ((Weapon_Choice < 1 || Weapon_Choice > 3) || (Weapon_Choice == 1
            && Player_CannonQty == 0) || (Weapon_Choice == 2 && Player_GrenadeQty ==0) )
    
            {
                cout << "\nPlease enter a valid option\n";
                cin >> Weapon_Choice;
            }
            

            
            
            
            switch(Weapon_Choice)
            {
                    
                case 1: // player chooses to attack with cannon
                    Cannon_Damage = (10 + rand() % 6);
                    cout << "\nYou chose a cannon"<< endl;
                    Computer_Health = Computer_Health - Cannon_Damage;
                    cout << "You inflicted " << Cannon_Damage << " points on your enemy." << endl;
                    cout <<  "Your health is " << Player_Health << endl;
                    cout << "The computer's health is " << Computer_Health << endl;
                    Player_CannonQty = Player_CannonQty - 1;
                    break;
                    
                    
                case 2:
                    Grenade_Damage = (7 + rand() % 13);
                    cout << "\nYou chose a grenade"<< endl;
                    Computer_Health = Computer_Health - Grenade_Damage;
                    cout << "You inflicted " << Grenade_Damage << " points on your enemy." << endl;
                    cout <<  "Your health is " << Player_Health << endl;
                    cout << "The computer's health is " << Computer_Health << endl;
                    Player_GrenadeQty = Player_GrenadeQty - 1;
                    
                    break;
                    
                case 3:
                    Rifle_Damage = (3 + rand() % 9);
                    cout << "\nYou chose a Rifle"<< endl;
                    Computer_Health = Computer_Health - Rifle_Damage;
                    cout << "You inflicted " << Rifle_Damage << " points on your enemy." << endl;
                    cout <<  "Your health is " << Player_Health << endl;
                    cout << "The computer's health is " << Computer_Health << endl;
                    
                    break;
            }
            
            
            
        }
        
        
        
        Turn == 1; // Computer Turn
        
        Computer_Weapon = rand() % 3;
        
        
        switch(Computer_Weapon)
        {
                
            case 1:
                
                Cannon_Damage = (10 + rand() % 6);
                cout<<"\nYour opponent used a cannon and inflicted " << Cannon_Damage << " points on you." << endl;
                Player_Health = Player_Health - Cannon_Damage;
                cout << "Your health is " << Player_Health << endl;
                cout << "The computer's health is " << Computer_Health << endl;
                Computer_CannonQty = Player_CannonQty - 1;
                break;
                
            case 2:
                Grenade_Damage = (7 + rand() % 6);
                cout<<"\nYour opponent used a grenade and inflicted " << Grenade_Damage << " points on you." << endl;
                Player_Health = Player_Health - Grenade_Damage;
                cout << "Your health is " << Player_Health << endl;
                cout << "The computer's health is " << Computer_Health << endl;
                Computer_GrenadeQty = Computer_GrenadeQty - 1;
                break;
                
            case 3:
                Rifle_Damage = (3 + rand() % 10);
                cout<<"\nYour opponent used a rifle and inflicted " << Rifle_Damage << " points on you." << endl;
                Player_Health = Player_Health - Rifle_Damage;
                cout << "Your health is " << Player_Health << endl;
                cout << "The computer's health is " << Computer_Health << endl;
                break;
                
                
        }
        
        
        
        
    } while(Player_Health >= 0 && Computer_Health >= 0); //loops while both players are alive
    
    
    
    if (Computer_Health < 0)
        cout << "Congratulations! You won!" << endl;
    
    if (Player_Health < 0)
        cout << "YOU HAVE DIED! GAME OVER!" << endl;;
    
    
    
    // std::cout<< "Press ENTER to continue..." << flush;
    
    // cin.ignore(cin.rdbuf()->in_avail()+1);
    
    return 0;
}
 
Last edited on
This srand(static_cast<int>(time(0))); should only ever be called once in your program. Don't put it inside a loop.
Thank you Yay, I will change that.
Are there any other suggestions? I'm losing my mind here.

I thought of changing the switch to a for loop to track the amount of grenades... something like this, but it's not working.. I think it could if it were tweaked..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
    if (Weapon_Choice == 1)
    {
     
        
        
        for (int Cannon = 1; Cannon <= 3; Cannon++)
        {
            Cannon = (1 + rand() % 10 + 15);//attack power can be between 10-15
            cout << "You chose a cannon"<< endl;
            Computer_Health = Computer_Health - Cannon;
            cout << "You inflicted " << Cannon << " points on your enemy." << endl;
            cout <<  "Your health is " << Player_Health << endl;
            cout << "The computer's health is " << Computer_Health << endl;
        
        }
        
            cout << "You are out of cannons!";
        
        
    }
    
}
Line 7: You're declaring your loop variable to be Cannon
Line 9: You're overwriting the value of your loop variable.

(1 + rand() % 10 + 15) will not get you a value between 10 and 15. If you follow C++ Order of Operations: 1 + ( rand() % 10 ) +15 = 16 + ( rand() % 10 ) = a range of 16 to 25.

10 + rand() % 6 will give you a range of 10 to 15.
Last edited on
AbstractionAnon, how could I fix that? I see how that was my problem. I tried working with a variable called totalCannons, but I'm not sure how to implement that into the program because I want to track the number of times 'Cannon' is input.

Topic archived. No new replies allowed.