need help with battle system

im working on a little text battle game.
what i have so far seems to be ok.
im just learning so please be gentle if criticizing me.
what im having trouble with is ENDING the program when hp or enemhp = 0.

i try to put just a simple return 0;
but the compiler throws an error.

also everytime the user/enemy attacks its always = to 4?? on both sides even though i have it set to random?

can anyone help??

thanks in advance.



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
  

#include <iostream>
#include <stdlib.h>










int attackenemy(int enemyhp, int attack)
    {
        enemyhp = enemyhp - attack;
        return enemyhp;
    }



int attacked(int hp, int enemyattack)
    {
        hp = hp - enemyattack;
        return hp;
    }


int block(int hp, int attack)
    {
        attack = 0;
        hp = hp - attack;
        return hp;
    }






void battle()
{

    int hp = 100;
    int enemhp = 100;
    int attack;
    int enemyattack;
    int fight;

    attack = rand() % 10;
    enemyattack = rand() & 10;

    do
    {
        int n;
        std::cout << "\n\n\n\n\n1)Attack\n2) Block\n   >   ";
        std::cin >> fight;
        for (n = 0; n < 10; n++)
            {
                std::cout <<  "\n\n\n\n\n\n";
            }



        if(fight == 1)
            {


                enemhp = attackenemy(enemhp, attack);
                hp = attacked(hp, enemyattack);
                std::cout << "####################################\n# PLayer HP:  " << hp <<"                   #\n#                                  #\n# Enemy HP:  " << enemhp << "                    #\n####################################\n" ;
            }


         if (fight == 2)
            {
                block(hp, enemyattack);
                std::cout << "Player HP:  " << hp;
                std::cout << "\nEnemy hp:  " << enemhp << "\n";
            }

         if (hp < 0 )
            {
                std::cout << "Gameover!\n";

            }

         if (enemhp < 0)
            {
                std::cout << "Winner!\n";

            }





    } while(hp > 0 && enemhp > 0);


}





int main()
{
    int n;
    std::cout << "*****************************\n*       FIGHT!!!            *\n*****************************\n\n1) Begin\n2) Exit\n\n\n>    ";
    int mainquestion;
    std::cin >> mainquestion;

    while(mainquestion == 1)
    {
        battle();
    }





    if (mainquestion == 2)
    {
        std::cout << "\n\nBye bye\n\n";
        return 0;
    }


    return 0;
}


rand() needs to be seeded, otherwise it wont work. Use srand() for that
http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand

read that article though, in particular read the example.


then add this to the beginning of main()...
srand(time(NULL));

the battle does end, but when it returns to main() you just call it again without asking theuser so it looks like it didnt.

you need to ask mainquestion inside the loop for it to be asked every time round.
Last edited on
im having a hard time with the rand() thing here. i have the program end when hp = 0. fixed that still cant get the random to work
all Random Number Generators needs "seeding" before they will work properly, think of this as initialising the RNG, so i'd put the following line as the first line of main().
srand(time(NULL));

you may also need #include <time.h> at the top of your file.

enemyattack = rand() & 10;
should be:
enemyattack = rand() % 10;
adding

srand(time(NULL));

seemed to help.

it seems to be on a pattern though.

id like it to be completely random.
but thenaks for the help yall
i was also going to ask.

in the python language you can assign variables to print something. say i did

banner = "This is my banner\n And i want to assign this\n so i dont have to copy this\nWhole code over and over.'

print banner




is there a way i can do it in c++?????

i try

1
2
3
4
5
6
7
8
9
10
11

#include <iostream>

int main()
{
      int x = "text here";
      std::cout << x;
      return 0;
}




but i get rhown errors and im not sure what to type into google to search it.
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>

int main()
{
    std::string x = "text here";
    std::cout << x << '\n' ;
}
thank you so much!!!!
Topic archived. No new replies allowed.