Attack + health system!

Pages: 12
This is my second day of teaching myself C++.
I'm trying to make an easy text based game.
There are no errors, but the attack sequence does not work no damage is given or taken. I think it between lines 71 and 101... Also how would I make it so monsters are random?
Simple is good, and please explain carefully so I can learn!

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
//  My First Game

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(int argc, char** argv)
{  //  beggining
    using std::cout;
    using std::cin;
    using std::endl;
    
    cout  << "Welcome to you life!\n";
    char userName[25];
    cout << "Please enter your name- ";
    cin >> userName;
    cout << "\n";
    cout << "Your journey begins " << userName << "!\n";
    system("pause");
    cout << "\n";
    
    cout << "You are an average human.\n";
    system("pause");
    cout << "\n";
    
    cout << "You have never been on an adventure before.\n";
    system("pause");
    cout << "\n";
    
    cout << "You did not even plan to ever go on an adventure.\n";
    system("pause"); 
    cout << "\n";
    
    cout << "As you step out of your door for the first time in your life...";
    Sleep(5000);
    
    
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    
    system("color 07");  //  white
    std::cout << "...a sword falls from above!\n";
    cout << "\n";
    
    cout << "     0 \n";  //  sword picture
    cout << "     | \n";
    cout << "    -O-\n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    system("pause");
    cout << "\n";
    
    
    cout << "You pick up the sword.\n";
    system("pause");
    cout << "\n";
    
    cout << "You notice a monster out side of your house...\n";
    system("pause");
    cout << "\n";
    
    int health;  //  player health
    health = 20;
    
    int slimeHealth;  //  slime health
    slimeHealth = 5;
    
    int slimeAttack;
    int max;
    max = 5;
    int min;
    min = 1;
    slimeAttack = rand()%(max-min + 1) + min;
    
    
    cout << "You approach a slime...\n";
    system("pause");
    cout << "\n";
    
    
    cout << "You have " << health << " health\n";
    system("pause");
    cout << "\n";

    cout << "You attack it...\n";
    system("pause");
    cout << "\n";
    
    health - slimeAttack == health;
    cout << "You have " << health << " health\n";
    
    system("pause");
    return 0;
}  //  end
    


Thank you!
Last edited on
health - slimeAttack == health;
Is this where you are trying to reduce the amount of health on you, or the creature?

If it is reducing your health:
health = health - slimeAttack

If you were taking health away from the creature, just take the above code and change the variable names to match the health you are taking away from.

Another thing I would do is do an if statement to see if the creature, or you are dead
1
2
3
4
if(health <= 0)
{
     //do some action saying you are dead or something
}


Or you could go with a while loop that constantly checks to see if your health does not equal or go below 0
1
2
3
4
while(health > 0)
{
     //continue fighting
}

Just be sure to do this for the creature as well
1
2
3
4
while(health > 0 || creatureHealth > 0) //|| means logical or
{
   //continue fighting
}


Another thing I will point out is using structures. Structures are similar to data type that can hold multiple pieces of data ex: ints, floats, doubles, etc...
I would use this to make up your multiple creature
1
2
3
4
struct Enemy{
int health;
//any other stats like maybe attack, defense, etc...
}; //be sure to add the semicolon 


Then make the creature by doing this:
1
2
3
Enemy Slime;
Slime.health = 5;
//any other data that is in the struct like attack: Slime.attack = 2; etc... 


This way you can make multiple creatures that can be called later.
1
2
3
4
Enemy Slime;
Enemy Goblin;
Enemy Bat;
//etc... 


Here is a link for structures: http://www.cplusplus.com/doc/tutorial/structures/

If any of this confuses you, just say so and I or someone else will try to clarify the information. I hope this helps you a little bit. Sorry I wouldn't know much about random generating numbers, but I believe someone else on here will help with that.
Last edited on
Thank you you were very help full.
I think I did some thing wrong. The slimes health is the same as mine...
I still need help with random generated numbers between two set numbers for attacks and enemy health...

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
//  My First Game

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(int argc, char** argv)
{  //  beggining
    using std::cout;
    using std::cin;
    using std::endl;
    
    cout  << "Welcome to you life!\n";
    char userName[25];
    cout << "Please enter your name- ";
    cin >> userName;
    cout << "\n";
    cout << "Your journey begins " << userName << "!\n";
    system("pause");
    cout << "\n";
    
    cout << "You are an average human.\n";
    system("pause");
    cout << "\n";
    
    cout << "You have never been on an adventure before.\n";
    system("pause");
    cout << "\n";
    
    cout << "You did not even plan to ever go on an adventure.\n";
    system("pause"); 
    cout << "\n";
    
    cout << "As you step out of your door for the first time in your life...";
    Sleep(5000);
    
    
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    
    system("color 07");  //  white
    std::cout << "...a sword falls from above!\n";
    cout << "\n";
    
    cout << "     0 \n";  //  sword picture
    cout << "     | \n";
    cout << "    -O-\n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    system("pause");
    cout << "\n";
    
    
    cout << "You pick up the sword.\n";
    system("pause");
    cout << "\n";
    
    cout << "You notice a monster out side of your house...\n";
    system("pause");
    cout << "\n";
    
    int health;  //  player health
    health = 20;
    int attack;  //  player attack
    attack = 2;
    
    
    struct Enemy  //  enemy stats
{
    int health;
    int attack;
};  
    
    cout << "You approach a slime...\n";
    system("pause");
    cout << "\n";
    
    Enemy Slime;  //  enemy slime stats
    Slime.health = 5;  //  later change to a variable between 10 and 5
    Slime.attack = 2;  //  later change to a variable between 5 and 0
    
    while(health > 0 || Slime.health > 0) //  || means logical or
{  //  battle sequence

    cout << "You have " << health << " health\n";
    system("pause");

    cout << "You attack it...\n";
    system("pause");
    
    Slime.health = Slime.health - attack;
    cout << "The slime has " << health << " health\n";
    
    cout << "The slime attacks...\n";
    health = health - Slime.attack;
    
}  //  end battle sequence

    if ( health >= 0 );  //  lose
{
    cout << "You died...\n";
    //  command to restart
}  //  end

    if ( Slime.health >= 0 );  //  win
{
    cout << "You defeated the slime!\n";
}  //  end

    cout << "You continue on your journey.\n";
    cout << "\n";

    system("pause");
    return 0;
}  //  end 


Also if you could point out anything else that could be improved/ changed/ fixed that would be very helpful...
Last edited on
jxkl5 wrote:
...anything else that could be improved/ changed/ fixed...


1
2
3
4
5
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h> 


*.h for standard lib is deprecated I think, use this instead:

1
2
3
4
5
#include <iostream>
#include <windows.h> // Not a standard library file I think
#include <cstdlib>
#include <cstdio>
#include <ctime> 


----

 
Enemy Slime;  //  enemy slime stats 


Personally I use non-capital letters when declaring an object, and capital letters when defining a class/struct/union. I think it makes it easier to distinguish classes from objects.

Enemy slime; // enemy slime stats

----

char userName[25];

Maybe you should get known with std::string, which has a variadic size and works nicely with <algorithm>.

std::string userName;

----

1
2
3
4
5
    if ( health >= 0 );  //  lose
{
    cout << "You died...\n";
    //  command to restart
}  //  end 


I would indent the { and }. More of a style issue, do whatever you want, but I'm just suggesting...

1
2
3
4
5
6
7
8
9
    //...
    if ( health >= 0 );  //  lose
    {
        cout << "You died...\n";
        //  command to restart
    }  //  end
    //...
    return 0;
}


Some people use this style:

1
2
3
4
5
6
7
8
    //...
    if ( health >= 0 ); {
        cout << "You died...\n";
        //  command to restart
    }  //  end
    //...
    return 0;
}


----


Writing about that I see your if statements have a ";" at the end, thus: your {} will always be executed:

1
2
3
4
    if ( health >= 0 );
    {
        cout << "You died...\n";
    }


translates to

1
2
3
4
5
6
7
    if ( health >= 0 )
    {
        // empty block due to ;
    }
    {
        cout << "You died...\n";
    }


the correct version would be:

1
2
3
4
    if ( health >= 0 ) // <--- removed the ;.
    {
        cout << "You died...\n";
    }
Thank you again, I still need help though...
How would I set a variable between two numbers for attack, health, ect?
How would I set the program to restart after death?
The slime should have 5 health but it gets 20...
And any other notes would be helpfull...

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
//  My First Game

#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <ctime>

int main(int argc, char** argv)
{  //  beggining
    using std::cout;
    using std::cin;
    using std::endl;
    
    cout  << "Welcome to you life!\n";
    std::string userName;
    cout << "Please enter your name- ";
    cin >> userName;
    cout << "\n";
    cout << "Your journey begins " << userName << "!\n";
    system("pause");
    cout << "\n";
    
    cout << "You are an average human.\n";
    system("pause");
    cout << "\n";
    
    cout << "You have never been on an adventure before.\n";
    system("pause");
    cout << "\n";
    
    cout << "You did not even plan to ever go on an adventure.\n";
    system("pause"); 
    cout << "\n";
    
    cout << "As you step out of your door for the first time in your life...";
    Sleep(5000);
    
    
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    
    system("color 07");  //  white
    std::cout << "...a sword falls from above!\n";
    cout << "\n";
    
    cout << "     0 \n";  //  sword picture
    cout << "     | \n";
    cout << "    -O-\n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    system("pause");
    cout << "\n";
    
    
    cout << "You pick up the sword.\n";
    system("pause");
    cout << "\n";
    
    cout << "You notice a monster out side of your house...\n";
    system("pause");
    cout << "\n";
    
    int health;  //  player health
    health = 20;
    int attack;  //  player attack
    attack = 2;
    
    
    struct Enemy  //  enemy stats
    {
    int health;
    int attack;
    };  
    
    cout << "You approach a slime...\n";
    system("pause");
    cout << "\n";
    
    Enemy slime;  //  enemy slime stats
    slime.health = 5;  //  later change to a variable between 10 and 5
    slime.attack = 2;  //  later change to a variable between 5 and 0
    
    while(health > 0 || slime.health > 0) //  || means logical or
    {  //  battle sequence

    cout << "You have " << health << " health\n";
    system("pause");

    cout << "You attack it...\n";
    system("pause");
    
    slime.health = slime.health - attack;
    cout << "The slime has " << health << " health\n";
    
    cout << "The slime attacks...\n";
    health = health - slime.attack;
    }  //  end battle sequence

    if ( health >= 0 )  //  lose
    {
    cout << "You died...\n";
    system("pause");
    return 0;
    }  //  end

    if ( slime.health >= 0 )  //  win
    {
    cout << "You defeated the slime!\n";
    cout << "You continue on your journey.\n";
    cout << "\n";
    }  //  end


    system("pause");
    return 0;
}  //  end
    


Please Help!
cout << "The slime has " << health << " health\n";

Line 102, do you see what is wrong here?
Thank you, next problem=
The slime is getting negative health how do I fix that?
and the other unanswered questions?
Few problems that can see
 
while(health > 0 || slime.health > 0)

Should be && as both conditions must be true to continue the fight.
Otherwise both must be false to finish it.

Next
 
if ( slime.health >= 0 ) 

and
 
 if ( health >= 0 )

should use
 
<=0

to ensure the health is above zero

also what Bourgond Aries said.

Hope this helps.
Last edited on
Thank you, now all i need is help with the variables and anything else any one notices!
How would I set a variable between two numbers for attack, health, ect?

Dont quite understand what you mean.

How would I set the program to restart after death?

Put it in a function.
Call the function when you die.
When the monster or player attacks, I wast it to vary between a set range of numbers (like 0 and 5)
or:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    bool go_on = true;

    do
    {
        //... game code here... edit go_on from in here if you wanna quit. break allows early exit.
    }
    while (go_on);

    return 0;
}
use srand and rand
call this to set the seed.
Only call it once at the start of the program.
srand( time( NULL ) );
Then use rand for a random number
int attackvalue = (rand() % 5)
The % 5 ensures the number will be between 0 and 5.
Last edited on
@jidder I think the number will be ∈[0, 4], not ∈[0, 5]
@Bourgond Aries yeah my bad.
I saw this everywhere... but I dont understand how to use it...
Here is my updated code:

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
//  My First Game

#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <ctime>

int main(int argc, char** argv)
{  //  beggining
    using std::cout;
    using std::cin;
    using std::endl;
    srand( time( NULL ) );
    
    cout  << "Welcome to you life!\n";
    std::string userName;
    cout << "Please enter your name- ";
    cin >> userName;
    cout << "\n";
    cout << "Your journey begins " << userName << "!\n";
    system("pause");
    cout << "\n";
    
    cout << "You are an average human.\n";
    system("pause");
    cout << "\n";
    
    cout << "You have never been on an adventure before.\n";
    system("pause");
    cout << "\n";
    
    cout << "You did not even plan to ever go on an adventure.\n";
    system("pause"); 
    cout << "\n";
    
    cout << "As you step out of your door for the first time in your life...";
    Sleep(5000);
    
    
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    
    system("color 07");  //  white
    std::cout << "...a sword falls from above!\n";
    cout << "\n";
    
    cout << "     0 \n";  //  sword picture
    cout << "     | \n";
    cout << "    -O-\n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    system("pause");
    cout << "\n";
    
    
    cout << "You pick up the sword.\n";
    system("pause");
    cout << "\n";
    
    cout << "You notice a monster out side of your house...\n";
    system("pause");
    cout << "\n";
    
    int health;  //  player health
    health = 20;
    int attack;  //  player attack
    attack = int attackvalue = (rand() % 5);
    
    
    struct Enemy  //  enemy stats
    {
    int health;
    int attack;
    };  
    
    cout << "You approach a slime...\n";
    system("pause");
    cout << "\n";
    
    Enemy slime;  //  enemy slime stats
    slime.health = 6;  //  later change to a variable between 10 and 5
    slime.attack = int attackvalue = (rand() % 5);  //  later change to a variable between 5 and 0
    
    while(health > 0 && slime.health > 0) //  || means logical or
    {  //  battle sequence

    cout << "You have " << health << " health\n";
    system("pause");

    cout << "You attack it...\n";
    system("pause");
    
    slime.health = slime.health - attack;
    cout << "The slime has " << slime.health << " health\n";
    
    cout << "The slime attacks...\n";
    health = health - slime.attack;
    }  //  end battle sequence

    if ( health <= 0 )  //  lose
    {
    cout << "You died...\n";
    system("pause");
    return 0;
    }  //  end

    if ( slime.health <= 0 )  //  win
    {
    cout << "You defeated the slime!\n";
    cout << "You continue on your journey.\n";
    cout << "\n";
    }  //  end


    system("pause");
    return 0;
}  //  end
    
    
Please help!
Based on what everyone has said, for the health of the enemy I would try this:
slime.health = (rand() % 6 ) + 5; // generates a random number between 0-5 then add 5

And attack to be:
slime.attack = (rand() % 6); // generates a random number between 0-5

But to understand the question, what is it that you don't understand that you see everywhere?
Last edited on
The players health goes down by 4 each turn while the n=monsters goes down by 2... every turn... The attack is not varying... neither is the health...

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
    cout << "You pick up the sword.\n";
    system("pause");
    cout << "\n";
    
    cout << "You notice a monster out side of your house...\n";
    system("pause");
    cout << "\n";
    
    int health;  //  player health
    health = 20;
    int attack;  //  player attack
    attack = (rand() % 6);
    
    
    struct Enemy  //  enemy stats
    {
    int health;
    int attack;
    };  
    
    cout << "You approach a slime...\n";
    system("pause");
    cout << "\n";
    
    Enemy slime;  //  enemy slime stats
    slime.health = (rand() % 6 ) + 5;  
    slime.attack = (rand() % 6);  
    
    while(health > 0 && slime.health > 0) //  || means logical or
    {  //  battle sequence

    cout << "You have " << health << " health\n";
    system("pause");

    cout << "You attack it...\n";
    system("pause");
    
    slime.health = slime.health - attack;
    cout << "The slime has " << slime.health << " health\n";
    
    cout << "The slime attacks...\n";
    health = health - slime.attack;
    }  //  end battle sequence

    if ( health <= 0 )  //  lose
    {
    cout << "You died...\n";
    system("pause");
    return 0;
    }  //  end

    if ( slime.health <= 0 )  //  win
    {
    cout << "You defeated the slime!\n";
    cout << "You continue on your journey.\n";
    cout << "\n";
    }  //  end


    system("pause");
    return 0;
}  //  end 
call your attack = random number function inside the while loop not before it. Cheers
ex:
1
2
3
4
5
    srand( ( unsigned ) time( NULL ) );
    for( unsigned int i = 0; i < 100; i++ )
    {
        std::cout << rand() % 6 << std::endl;
    }

output will vary....
5
4
2
1
2
4
0
4
0
2
1
4
5
3
5
3
1
0
4
1
1
2
1
4
4
2
3
4
5
3
3
2
4
3
4
2
2
2
0
3
2
5
3
5
5
0
4
5
4
3
4
3
3
3
3
3
1
3
0
5
4
5
4
4
0
2
3
0
3
0
0
5
4
2
1
4
5
3
4
4
5
3
3
3
3
1
4
2
3
2
4
4
1
2
1
3
1
0
0
5
Last edited on
Thank you the separate code is very helpful...
I just do not have any idea how to insert it into my code...
Please help and explain in the simplest terms possible.
Pages: 12