Zork Clone

Hello folks!

I got a problem with my code. How would i generate 2 'random' integers?

1
2
3
4
5
6
  do{
srand(time(NULL)); 
int Attack = rand() % 10 + 1; 
int Heal = rand() % 10 + 1;
int dragonAttack = rand() % 20 + 1; // How would i make this integer different from the other 2?
cout << "Attack - Heal" << endl;    


I'd appreciate any help.
You should avoid at all cost putting srand(time(NULL)); inside a loop. Put it outside the loop.

For example:
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL)); //srand outside the loop
    
    int i = 1;
    do
    {
        int attack = rand() % 10+1; //Set the random number as usual
        int heal = rand() % 10+1; //Set the random number as usual
        int dragAtk = rand() % 20 + 1; //Set the random number as usual
        
        cout << "Attack: " << attack << endl;
        cout << "Heal: " << heal << endl;
        cout << "Dragon Attack: " << dragAtk << endl;
        
        i++;
        
        cout << "\n" << endl;
    
    }while(i < 3);

    return 0;
}
Last edited on
I tried to do that however, it gives me this error:

C:\Users\User\Desktop\C++ Projects\Basic\main.cpp|11|error: expected constructor, destructor, or type conversion before '(' token|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|

Can you post your full code?
Here it is.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
using namespace std;



string name;
int playerHP = 20;
int dragonHP = 30;
string choice;
srand(time(NULL));



int main(){

cout << "Hello wanderer, what is your name?" << endl;
cin >> name;
cout << "Welcome " << name <<". Kill the fucking dragon." << endl;
do{
int Attack = rand() % 10 + 1;
int Heal = rand() % 10 + 1;
int dragonAttack = rand() % 20 + 1;
cout << "Attack - Heal" << endl;
cin >> choice;

if(choice == "Attack"){
    dragonHP = dragonHP - Attack;
    cout << "You attacked the dragon! It has " << dragonHP << " left!" <<  endl;
}
else{
    playerHP = playerHP + Heal;
    cout << "You healed yourself for " << Heal << " ! Your current HP is" << playerHP << endl;

}

playerHP = playerHP - dragonAttack;
cout << "The dragon attacked you for " << Attack << " damage! You have " << playerHP << " left!" << endl;


}while(dragonHP != 0 && playerHP != 0);

cout << "Game over" << endl;

return 0;
}
The problem is that the srand should be inside the main() function. It can't be outside. Call it at the beginning of your main() function. Also for the condition of the while loop it should be:
while(dragonHP > 0 || playerHP > 0);
This is because you want to end the loop when ONE of the HP reaches zero. If you put while(dragonHP != 0 && playerHP != 0); then it will keep looping until BOTH of them are EXACTLY zero. Say for example you have 5 HP left and the dragon hits you with a 10. You will have -5 HP and the loop won't end.

One more thing, it should be if(choice == "Attack" || choice == "attack"). You never know if the user types in ALL lower case.

Here is a fixed version:

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

string name;
int playerHP = 20;
int dragonHP = 30;
string choice;


int main(){
    srand(time(NULL));
    cout << "Hello wanderer, what is your name?" << endl;
    cin >> name;
    cout << "Welcome " << name <<". Kill the fucking dragon." << endl;
    do{
    int Attack = rand() % 10 + 1;
    int Heal = rand() % 10 + 1;
    int dragonAttack = rand() % 20 + 1;
    cout << "Attack - Heal" << endl;
    cin >> choice;

    if(choice == "Attack" || choice == "attack"){
        dragonHP = dragonHP - Attack;
        cout << "You attacked the dragon! It has " << dragonHP << " left!" <<  endl;
    }
    else{
        playerHP = playerHP + Heal;
        cout << "You healed yourself for " << Heal << " ! Your current HP is" << playerHP << endl;

    }

    playerHP = playerHP - dragonAttack;
    cout << "The dragon attacked you for " << Attack << " damage! You have " << playerHP << " left!" << endl;


    }while(dragonHP > 0 || playerHP > 0);

    cout << "Game over" << endl;

    return 0;
}
Last edited on
Thank you! I totally forgot about || and the lowercase. However.

When i attack the dragon, the dragon will redirect the same damage at me and i'll end up with something like this http://prntscr.com/2e2xkk. I want the dragons attack to be randomized seperately. How would i do that?

It does attack with a different attack. You are printing it wrong. So its actually a printing mistake.

You have the line:
cout << "The dragon attacked you for " << Attack << " damage! You have " << playerHP << " left!" << endl;

Which should actually be:
cout << "The dragon attacked you for " << dragonAttack << " damage! You have " << playerHP << " left!" << endl;
Last edited on
How stupid of me! Thanks!
Topic archived. No new replies allowed.