Looping a certain piece of code?

Ok what I want to know how to do is, How to make a loop so as if the goblin isn't out of hp, You keep attacking the goblin untill it is entirely gone. So...any ideas how? Thanks in advance.

(oh also, for all you pros, Tell me how I'm doing on the code structure, as in, is it neat, organized, and stuff, or do I need to improve on it?)



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
int main()
{
        int Enemyhealth = 5;
        int Attack;
        int Defense;
        int C1, C2, C3, C4, C5;
        int Damage;

        cout << "Welcome to D&D, C++ style" << endl;
        cout << "Please type in '1' to continue" << endl;
                                                            
	cin >> C1;

        if(C1 = 1)
        {
        cout << "Very good,Now we will battle with the Goblin!" << endl;
}

        cout << "please type in '2' to continue" << endl;
        cin >> C2;

        if(C2 = 2)
        {
        cout << "The Goblin has 5 hp, You need to deplete the enemy of it's hp to win " << endl;
}

        cout << "please type in '3' to attack the goblin" << endl;
        cin >> C3;

        if(C3 = 3)
        {
        cout << "You attack the goblin!" << endl;
        srand ( time(NULL) );
        Attack = rand() % 10 + 1;
	Defense = rand() % 10 + 1;
        cout << "Your attack roll " << Attack << endl;
        cout << "Goblin's defense roll " << Defense << endl;
}
        if(Attack > Defense)
        {
        cout << "you strike the goblin!" << endl;
        Damage = rand() % 5 + 1;
        cout << "The goblin has this much health left " << Enemyhealth - Damage <<endl;
}

        if(Defense > Attack)
        {
        cout << "you missed the goblin!" << endl;
}
 

Last edited on
Sorry, I don't have enough time, just I advice a tutorial:

http://www.dreamincode.net/forums/showtopic13919.htm
1
2
cin >> C1;
if (C1 = 1)


Why? Why do you need the user to enter 1, 2 and 3 for no real particular reason to attack, or enter battle? Anyway.. if you REALLY need them, what happens if the user enters something like "cows" for C1? The program will go haywire so you'll need to make sure the input is valid (see: http://www.cplusplus.com/forum/articles/6046/).

Also.. look at your own code: if (C1 = 1) = means to assign a value to something
== means to compare a value to something else
That means you should've used == instead since you aren't comparing C1, C2 or C3 to anything - you are just assigning a value to them.

Also for your code:
if(Attack > Defense) if(Defence > Attack)
What would happen if Attack == Defence?

I also recommend the ending bracers for a code block is on the same line as the starting bracers to make it neater and a tad easier to read.
1
2
3
4
5
6
if(Attack > Defense)
        {
        cout << "you strike the goblin!" << endl;
        Damage = rand() % 5 + 1;
        cout << "The goblin has this much health left " << Enemyhealth - Damage <<endl;
} //move this up to the line where the first braces is 


To answer your first question -
I think you can use a boolean to check if the goblin is dead or not. So something like -
1
2
boolean goblin_isDead = false;
//..... etc 

Then inside a loop in the goblin combat -
1
2
3
4
while (goblin_isDead == false)
{
   //.....
}

Then later -
1
2
3
4
if (EnemyHealth == 0)
{
   goblin_isDead = true; //assigns goblin_isDead true so that it'll break out of the loop
}


EDIT: In fact, you don't need a boolean. You can just do something like -
1
2
3
4
while (EnemyHealth > 0) //while the enemy still has more than 0 health
{
   //... attack... defend.. etc
}
Last edited on
Ah thanks for the explaination, I'll give it a go, And see what comes up.

And, Entering the numbers and stuff, I'm just seeing what can I actually do with c++, I know it's probably not neccessary and could be wrote a much better way, Just me screwing around with it :P
Topic archived. No new replies allowed.