trying to make a pokemon like in c++

So i am still learning c++ (by myself) and i thought that making something like this might help me move forward. here's what i got so far :

#ifndef POKEMON_H
#define POKEMON_H
#include <iostream>
#include <cstdlib>

using namespace std;

class squirtl
{public :
int life = 500;
int stg = 50;
int def = 20;
};
class pikachu
{public :
int life = 600;
int stg = 80;
int def = 10;
};
class charmender
{public :
int life = 600;
int stg = 80;
int def = 10;
};

class pidgey
{public :
int life = 600;
int stg = 80;
int def = 10;
};

#endif // POKEMON_H

i have created a few pokemon classes.

#include <iostream>
#include <cstdlib>
#include "pokemon.h"

using namespace std;

int main() {

squirtl pokemon_y;
pikachu pokemon_x;
do {
void attack();
{
pokemon_y.life = pokemon_y.life - (pokemon_x.stg/pokemon_x.stg);
}
cout << "presse 1 to attack" << endl;
cout << "presse 2 to heal" << endl;
cout << "presse 3 to throw pokeball" << endl;
cin >> x ;
if (x == 1)
void attack();
cout << pokemon_x.life << endl;
cout << pokemon_y.life <<endl;
}
while (pokemon_x.life && pokemon_y.life > 1);
}

what i wanted to do now was creat a player class or array that contains a certain number of pokemons and i need help with doing that, i was thinking of doing somthing like an array player[3]={squirtl,pikachu,charmender} (of course this wouldn't work)
Last edited on
you are making a bit of a mess :)
you should have 1 class, with life, stg, def in it, and the name of the thing as well, not a unique class for each one. Classes are types, like int, and the data changes not the type.

What you have, in terms of int:
typedef int three_t;
typedef int five_t;

three_t three = 3;
five_t five = 5;

instead of just saying
int three = 3;
int five = 5;

class pokey
{public :
string name;
int life;
int stg;
int def;
};

pokey charmender;
charmender.name = "Charmender";
charmender.life = 600;
... etc

and if you make a constructor for it, you can allocate your array of 3 of them simply -- its similar to what you said, but do this stuff first, then we can look at the array.
Last edited on
i actually did try this before but it did not workout, this time i managed to make it work (with your help) and i got my player array. thanks
I'll update this post if i get stuck again (which I'm sure will happen again).
hi again,so now i have a player vector and multiple pokemons and multiple moves like this (this is not all of it just an 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
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
struct attacks : public Parent
{
string mv_name;
int power;
int pp;
int accuracy;

struct Thunderbolt : public attacks {
Thunderbolt(){
mv_name= "Thunderbolt";
power =60;
pp=15;
accuracy=90;
}
}Thunderbolt;

struct ember : public attacks {
ember(){
mv_name= "ember";
power =60;
pp=15;
accuracy=90;
}
}ember;

struct pokemon : public Parent
{
string name;
int life;
float stg;
int def;
};



struct charmander : public pokemon
{
charmander (){
name="charmander";
life=600;
stg=4.5;
def=3;}
vector<attacks> charmender{{ember, quick_attack}};
}charmander;


struct pikachu : public pokemon
{
pikachu (){
name="pikachu";
life=600;
stg=4.7;
def=3;}
vector<attacks> pikachu{{thunderbolt, quick_attack}};
}pikachu;
};


this is all in a header file
 
vector<pokemon> player{{charmander, pikachu }};

then in main i have my player vector.


my question is how can i use an element of the vector charmender (for example) by using the player vector (somthing like player [0][0]), i tried this
 
vector<vector<Parent>> allData = {player,charmander};

with parent being an empty class that both "pokemon"and"attack"classes inherit from but it did work
You've defined your vector to be a vector of Parent objects. This means that you are only storing base class objects in there, not the derived classes. Even though you're initialising the vector using derived class objects, the copies that are stored in the vector are just base class objects.

This is known as object slicing.

If you're looking to use polymorphism here, you need to store a vector of pointers to the objects.
Last edited on
In my opinion, you're still going about this wrong.

You don't need a different class for every single Pokemon.
You just need the Pokemon class, and each Pokemon has a name, life, strength defense, other stats, and a vector of attacks.

Likewise, you don't need a different class for every single move. Depending on the logic, you might want an Attack to derive from a Move, but you shouldn't need to make individual classes for every single move (Stomp, Tackle, Surf, etc.). You only need a class to define a category of classes.
e.g. a Move has-a type, strength, accuracy, side-effects.
The side-effects is where it's going to get complicated, and where you might want to use polymorphism.
So i changed up my code a bit hope it's a little cleaner now,managed to get my player and attacks array working
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
#ifndef POKEMON_H
#define POKEMON_H
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

using namespace std;

int x,y,a,b,z,i,j,p_p,p_e,r_e,r_p,m,g;


struct attacks
{
string mv_name;
int power;
int pp;
int accuracy;
int side_effect;//1 for paralysis , 2 for burn
};


struct pokemon : public attacks
{
string name;
int life;
int stg;
int def;
attacks moves[2];
};

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "pokemon.h"

int main() {

attacks ember;
ember.mv_name="Ember";
ember.power=80;
ember.pp=25;
ember.accuracy=90;
ember.side_effect = 2;

attacks thunderbolt;
thunderbolt.mv_name="Thunderbolt";
thunderbolt.power=80;
thunderbolt.pp=25;
thunderbolt.accuracy=90;

attacks quick_attack;
quick_attack.mv_name="Quick attack";
quick_attack.power=70;
quick_attack.pp=30;
quick_attack.accuracy=95;

attacks bubble;
bubble.mv_name="Bubble";
bubble.power=70;
bubble.pp=30;
bubble.accuracy=95;

attacks razor_leaf;
razor_leaf.mv_name="Razor Leaf";
razor_leaf.power=70;
razor_leaf.pp=30;
razor_leaf.accuracy=95;

attacks tail_whip;
tail_whip.mv_name="Tail Whip";
tail_whip.power=70;
tail_whip.pp=30;
tail_whip.accuracy=95;

attacks tackle;
tackle.mv_name="Tackle";
tackle.power=70;
tackle.pp=30;
tackle.accuracy=95;
tackle.side_effect = 1;

pokemon charmander;
charmander.name="Charmander";
charmander.life=500;
charmander.stg=5;
charmander.def=10;
charmander.moves[0]=ember;
charmander.moves[1]=tackle;

pokemon evee;
evee.name="Evee";
evee.life=500;
evee.stg=5;
evee.def=10;
evee.moves[0]=tail_whip;
evee.moves[1]=quick_attack;

pokemon pikachu;
pikachu.name="Pikachu";
pikachu.life=500;
pikachu.stg=5;
pikachu.def=10;
pikachu.moves[0]=thunderbolt;
pikachu.moves[1]=quick_attack;

pokemon bulbasaur;
bulbasaur.name="Bulbasaur";
bulbasaur.life=500;
bulbasaur.stg=5;
bulbasaur.def=10;
bulbasaur.moves[0]=razor_leaf;
bulbasaur.moves[1]=tackle;

pokemon squirtl;
squirtl.name="Squirtl";
squirtl.life=500;
squirtl.stg=5;
squirtl.def=10;
squirtl.moves[0]=bubble;
squirtl.moves[1]=tackle;

pokemon pidgey;
pidgey.name="Pidgey";
pidgey.life=500;
pidgey.stg=5;
pidgey.def=10;
pidgey.moves[0]=tackle;
pidgey.moves[1]=quick_attack;

 vector<pokemon> player{{charmander, pikachu, squirtl}};
 vector<pokemon> enemy{{bulbasaur, pidgey, evee}};
j= 0+rand()% 3;
cout << enemy[j].name << " has appeared\n\n";
cout << "press 1 to send out " << player[0].name << "\n";
cout << "press 2 to send out " << player[1].name << "\n";
cout << "press 3 to send out " << player[2].name << "\n";
 cin >> z ;
 if (z==1){i=0;}
 else if (z==2){i=1;}
 else if (z==3){i=2;};

do {
cout<<p_e;"\n";
    cout << player[i].name << "'s turn\n";
    cout << "press 1 to attack\n";
    cout << "press 2 to use item\n";
    cout << "press 3 to throw pokeball\n";
    cin >> x ;
    if (x==1){
            if (p_e>-1){break;
      cout << "press 1 to use " << player[i].moves[0].mv_name << "\n";
      cout << "press 2 to use " << player[i].moves[1].mv_name << "\n";
      cout << p_e;"\n";
      cin >> y ;
if (y==1){m=0;}
 else if (y==2){m=1;};
      if (y==1){
       enemy[j].life = enemy[j].life - ((player[i].stg*player[i].moves[m].power)/enemy[j].def);
       cout << enemy[j].name << "'s life :" << enemy[j].life << "\n";}
       else if (y==2){
        enemy[j].life = enemy[j].life - ((player[i].stg*player[i].moves[m].power)/enemy[j].def);
        cout << enemy[j].name << "'s life :" << enemy[j].life << "\n";}
        if(player[i].moves[m].side_effect = 1){
          g= 0+rand()% 100;
          if (g>65){
            p_p=0+rand()%2;}
        else if (player[i].moves[m].side_effect = 2){
          g= 1+rand()% 100;
          if (g>65){
            r_p=0+rand()%2;}}
}}}
    else if (x==2){
     cout << "press 1 to use heal\n";
     cout << "press 2 to use cure paralysis\n";
     cout << "press 3 to use heal burn\n";
     cin >> a ;
     if (a==1){
        player[i].life = player[i].life + 60;
        cout << player[i].name << "'s life :" << player[i].life << "\n";}
     else if (a==2){p_e=0;}
     else if (a==3){r_e=0;}}
    else if (x==3){
           if (enemy[j].life >= 260){
        b= 1+rand()% 30;
        if (b<5){
        cout << enemy[j].life << "caught\n";}
        else if (b>5){
            cout << "missed\n";}
           }
           else if (enemy[j].life < 260){
            b= 1+rand()% 10;
            if (b<5){
            cout << enemy[j].life << "caught\n";}
           else if (b>=5){
            cout << "missed\n";}
           }
           };
// PHASE 2
    cout << enemy[j].name << "'s turn\n\n";
    cout << "press 1 to attack\n";
    cout << "press 2 to use item\n" ;
    cin >> x ;
    if (x==1){
            if (p_p>-1){break;
      cout << "press 1 to use " << enemy[j].moves[0].mv_name << "\n";
      cout << "press 2 to use " << enemy[j].moves[1].mv_name << "\n";
      cin >> y ;
      if (y==1){
       player[i].life = player[i].life - ((enemy[j].stg*enemy[j].moves[0].power)/player[i].def);
       cout << player[i].name << "'s life :" << player[i].life << "\n";}
       else if (y==2){
        player[i].life = player[i].life - ((enemy[j].stg*enemy[j].moves[1].power)/player[i].def);
        cout << player[i].name << "'s life :" << player[i].life << "\n";}
            if(player[i].moves[m].side_effect = 1){
          g= 1+rand()% 100;
          if (g>70){
            p_e=0+rand()%2;}
        else if (player[i].moves[m].side_effect = 2){
          g= 1+rand()% 100;
          if (g>65){
            r_e=0+rand()%2;}}
    }}
    else if (x==2){
     cout << "press 1 to use heal\n";
     cout << "press 2 to use cure paralysis\n";
     cout << "press 3 to use heal burn\n";
     cin >> a ;
     if (a==1){
        enemy[j].life = enemy[j].life + 70;
        cout << enemy[j].name << "'s life :" << enemy[j].life << "\n";}
     else if (a==2){
        void cure_paralysis();}
     else if (a==3){
        void cure_burn ();}}}}
    while (player[i].life && enemy[j].life > 1);
}

i wanted some help with the paralysis and burn status (specially paralysis)
for paralysis when it's present in an attack theres a chance of paralysis the problem is i wanted to put it as if paralysis > 0 then (break) skip the enemy's attack turn but it just keeps skipping no matter what. any advice or better way to do it would be appreciated
Topic archived. No new replies allowed.