Dinosaur Arena game help

I have been working on my actual game now and I am having a real problem, i cant use dinosaurHealth in my main function.

MainGame.h

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#ifndef MAINGAME_H_INCLUDED
#define MAINGAME_H_INCLUDED

#include <iostream>
#include <vector>
#include "player.h"
#include "Dinosaur.h"

using namespace std;

void player::store()
{

    int menuChoice;
    int amountChoice = 0;
    string inv;

    items[0] = 0;
    items[1] = 0;
    items[2] = 0;
    items[3] = 0;

    cout << "1) Health Pack - $5" << endl;
    cout << "2) Pistol Ammo - $2" << endl;
    cout << "3) Shotgun Ammo - $5" << endl;
    cout << "4) Rifle Ammo - $18" << endl;
    cin >> menuChoice;

    while((amountChoice != -1) || (menuChoice != -1))
    {
        if((amountChoice == -1) || (menuChoice == -1))
        {

            cout << "Thank you for shopping at the store\n" << endl;
            MainGame();
            break;
        }
        else
        {

        switch(menuChoice)
        {
            case 1:
                {
                    cout << "How many Health Packs do you want to buy?" << endl;
                    cout << "$5 per health pack" << endl;

                    while(amountChoice != -1)
                    {
                        cin >> amountChoice;

                        items[0] = amountChoice;

                        if(money >= items[0] * 5)
                        {
                            money -= items[0] * 5;
                            healthPacks += amountChoice;
                            cout << "Current money equals $" << money << endl;
                            cout << "Current Health Packs: " << healthPacks << endl;
                            cout << "\n";
                            amountChoice = 0;

                            save();
                            store();
                        }
                        else
                        {
                            cout << "You do not have enough money" << endl;
                        }
                    }

                }break;

            case 2:
                {
                    cout << "How much Pistol Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[1] = amountChoice;

                    if(money >= items[1] * 2)
                    {
                        money -= items[1] * 2;
                        pistolAmmo += amountChoice;
                        cout << "Current money equals $" << money << endl;
                        cout << "Current Pistol Ammo: " << pistolAmmo << endl;
                        cout << "\n";
                        amountChoice = 0;

                        save();
                        store();
                    }
                    else
                    {
                        cout << "You do not have enough money" << endl;
                    }

                }break;

            case 3:
                {
                    cout << "How much Shotgun Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[2] = amountChoice;

                    if(money >= items[2] * 5)
                    {
                        money -= items[2] * 5;
                        shotgunAmmo += amountChoice;
                        cout << "Current money equals $" << money << endl;
                        cout << "Current Shotgun Ammo: " << shotgunAmmo << endl;
                        cout << "\n";
                        amountChoice = 0;

                        save();
                        store();
                    }
                    else
                    {
                        cout << "You do not have enough money" << endl;
                    }

                }break;

            case 4:
                {
                    cout << "How much Rifle Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[3] = amountChoice;

                    if(money >= items[3] * 18)
                    {
                        money -= items[3] * 18;
                        rifleAmmo += amountChoice;
                        cout << "Current money equals $" << money << endl;
                        cout << "Current Rifle Ammo: " << rifleAmmo << endl;
                        cout << "\n";
                        amountChoice = 0;

                        save();
                        store();
                    }
                    else
                    {
                        cout << "You do not have enough money" << endl;
                    }

                }break;
        }//End of switch statement

        }

    }//End of while loop
}

void player::backpack()
{
    cout << "ITEMS\n" << endl;

    cout << "Money $" << money << endl;
    cout << "Health Packs: " << healthPacks << endl;
    cout << "Pistol Ammo: " << pistolAmmo << endl;
    cout << "Shotgun Ammo: " << shotgunAmmo << endl;
    cout << "Rifle Ammo: " << rifleAmmo << endl;

    cout << "\n";

    cout << "WEAPONS\n" << endl;

    cout << "Pistol - Does 3 points of damage against enemies" << endl;
    cout << "Shotgun - Does 9 points of damage against enemies" << endl;
    cout << "Rifle - Does 24 points of damage against enemies" << endl;
}

void player::stats()
{

}


void player::MainGame()
{
    int choice;

    cout << "1) Start Battle" << endl;
    cout << "2) View Stats" << endl;
    cout << "3) Store" << endl;
    cout << "4) View Backpack" << endl;
    cin >> choice;


    if(choice == 1)
    {
        dinoSelect();
    }
    else if(choice == 2)
    {
        stats();
    }
    else if(choice == 3)
    {
        store();
    }
    else if(choice == 4)
    {
        backpack();
    }
}

void player::dinoSelect()
{
    time_t T;
    time(&T);
    srand(T);
    int Time;

    dinosaur D;

    Time = rand() % 2;

    switch(Time)
    {
        case 0:
            {
                cout << "Your opponent is the T-Rex!!\n" << endl;
                arena();
                D.trex();
            }break;
        case 1:
            {
                cout << "Your opponent is the Raptor!!\n" << endl;
                arena();
                D.velociraptor();
            }break;
    }
}

void player::arena()
{
    int choice;

    cout << "Make your move\n" << endl;

    cout << "What do you want to use?" << endl;
    cout << "1) Pistol - 3 DMG" << endl;
    cout << "2) Shotgun - 9 DMG" << endl;
    cout << "3) Rifle - 13 DMG" << endl;
    cout << "4) Use Health Pack\n" << endl;
    cin >> choice;

    if(choice == 1)
    {
        dinosaurHealth -= 3;
    }
}

#endif // MAINGAME_H_INCLUDED 

Dinosaur.h

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
#ifndef DINOSAUR_H_INCLUDED
#define DINOSAUR_H_INCLUDED

#include <iostream>
#include <ctime>
#include <random>
#include "player.h"

using namespace std;

class dinosaur : public player
{
    protected:
        int *dinosaurHealth;
        int *attacks;

    public:
        void trex();
        void velociraptor();
        dinosaur();
        ~dinosaur();

        void set_dinosaurHealth(int DH);
        void set_attacks(int *A);

        int *get_dinosaurHealth();
        int *get_attacks();

};

dinosaur::dinosaur(): dinosaurHealth(100), attacks(new int[0])
{

}

dinosaur::~dinosaur()
{
        if(attacks)
            delete[] attacks;
}

void dinosaur::set_dinosaurHealth(int DH)
{
    dinosaurHealth = DH;
}

void dinosaur::set_attacks(int *A)
{
        if(attacks)
            delete[] attacks;
    attacks = A;
}

//--------------------------------------------

int *dinosaur::get_dinosaurHealth()
{
    return dinosaurHealth;
}

int *dinosaur::get_attacks()
{
    return attacks;
}


void dinosaur::trex()
{
    int TrexHealth = 0;
    int attacks[5] = {5,9,13,19,23};
    time_t T;
    time(&T);
    srand(T);
    int Time;

    Time = rand() % 6;

    TrexHealth = dinosaurHealth;

    switch(Time)
    {
        case 0:
            {
                cout << "T-Rex used Bite!!" << endl;
                playerHealth -= 5;
            }break;
        case 1:
            {
                cout << "T-Rex used Stomp!!" << endl;
                playerHealth -= 9;
            }break;
        case 2:
            {
                cout << "T-Rex used Crunch" << endl;
                playerHealth -= 13;
            }break;
        case 3:
            {
                cout << "T-Rex used Slam" << endl;
                playerHealth -= 19;
            }break;
        case 4:
            {
                cout << "T-Rex used Pulverize" << endl;
                playerHealth -= 23;
            }break;
        default:
            {
                cout << "T-Rex attack missed!!" << endl;
                playerHealth -= 0;
            }
    }
}

void dinosaur::velociraptor()
{
    int RaptorHealth = 0;
    int attacks[5] = {5,9,13,19,23};
    time_t T;
    time(&T);
    srand(T);
    int Time;

    Time = rand() % 6;

    RaptorHealth = dinosaurHealth;

    switch(Time)
    {
        case 0:
            {
                cout << "Raptor used Bite!!" << endl;
                playerHealth -= 5;
            }break;
        case 1:
            {
                cout << "Raptor used Slash!!" << endl;
                playerHealth -= 9;
            }break;
        case 2:
            {
                cout << "Raptor used Leap Attack" << endl;
                playerHealth -= 13;
            }break;
        case 3:
            {
                cout << "Raptor used Slam" << endl;
                playerHealth -= 19;
            }break;
        case 4:
            {
                cout << "Raptor used Shred" << endl;
                playerHealth -= 23;
            }break;
        default:
            {
                cout << "Raptors attack missed!!" << endl;
                playerHealth -= 0;
            }
    }
}

#endif // DINOSAUR_H_INCLUDED 
bump
Just an organisational point - do not put implementation code (the function definitions) into header files.

Put your class declaration into the header file, and your code for the function definitions into a .cpp file.

When I use my IDE, it automatically puts the class declaration into the header file and I can get it to create function stubs in the .cpp file.


Also Dinosaur should be a class of it's own with TRex & other classes inheriting from it. That way a Trex and other dinosaurs can be an objects of their own, rather than being a function inside a dinosaur object.

If you do this you can start to take advantage of OOP ideas - like inheritance & polymorphism.

Finally, by now I think you should get out of the habit of using namespace std;. Either put std:: before each std namespace thing or have using std::cout; as example, for each std thing. I do a mixture - I use the first one for things that appear infrequently, and the second for things that are used all the time.

If you can make all these changes - then we will see if you still have problems.

HTH - have fun !!!!
ok so i only use .h files for classes?
Ok im sure the errors are obvious but ive only had 5 hours of sleep so if you could help me fix the errors that would be most appreciated.

The code is too big to post here so i had to put it on paste bin.

main.cpp

http://pastebin.com/x43abmPP

here are my classes

player class

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
#ifndef PLAYER_H
#define PLAYER_H

#include <vector>
#include <string>


class player
{
    protected:
        int playerHealth;
        int pistolAmmo;
        int shotgunAmmo;
        int rifleAmmo;
        int score;
        int money;
        int healthPacks;
        int *items;

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void timer();
        void backpack();
        void stats();
        void dinoSelect();
        void arena();
        void conditionals();
        player();
        ~player();

        void set_health(int H);
        void set_pistolAmmo(int PA);
        void set_shotgunAmmo(int SA);
        void set_rifleAmmo(int RA);
        void set_score(int S);
        void set_money(int M);
        void set_items(int *I);
        void set_healthPacks(int HP);

        int get_health();
        int get_pistolAmmo();
        int get_shotgunAmmo();
        int get_rifleAmmo();
        int get_score();
        int get_money();
        int *get_items();
        int get_healthPacks();
};

player::player(): playerHealth(100), pistolAmmo(0), shotgunAmmo(0),
                  rifleAmmo(0), score(0), money(600), healthPacks(0),
                  items(new int[4])
{
    for(int i = 0; i < 5; ++i)
    {
        items[i] = 0;
    }
}

player::~player()
{
        if(items)
                delete[] items;
}

void player::set_health(int H)
{
    playerHealth = H;
}

void player::set_pistolAmmo(int PA)
{
    pistolAmmo = PA;
}

void player::set_shotgunAmmo(int SA)
{
    shotgunAmmo = SA;
}

void player::set_rifleAmmo(int RA)
{
    rifleAmmo = RA;
}

void player::set_score(int S)
{
    score = S;
}

void player::set_money(int M)
{
    money = M;
}

void player::set_items(int *I)
{
    items = I;
}

void player::set_healthPacks(int HP)
{
    healthPacks = HP;
}



int player::get_health()
{
    return playerHealth;
}

int player::get_pistolAmmo()
{
    return pistolAmmo;
}

int player::get_shotgunAmmo()
{
    return shotgunAmmo;
}

int player::get_rifleAmmo()
{
    return rifleAmmo;
}

int player::get_score()
{
    return score;
}

int player::get_money()
{
    return money;
}

int *player::get_items()
{
    return items;
}

int player::get_healthPacks()
{
    return healthPacks;
}

#endif // PLAYER_H


dinosaur class

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
#ifndef DINOSAUR_H_INCLUDED
#define DINOSAUR_H_INCLUDED

#include <iostream>
#include <ctime>
#include <random>
#include "player.h"

using namespace std;

class dinosaur
{
    protected:
        int dinosaurHealth;
        int *attacks;

    public:
        void trex();
        void velociraptor();
        dinosaur();
        ~dinosaur();

        void set_dinosaurHealth(int DH);
        void set_attacks(int *A);

        int get_dinosaurHealth();
        int *get_attacks();

};

dinosaur::dinosaur(): dinosaurHealth(100), attacks(new int[0])
{

}

dinosaur::~dinosaur()
{
        if(attacks)
            delete[] attacks;
}

void dinosaur::set_dinosaurHealth(int DH)
{
    dinosaurHealth = DH;
}

void dinosaur::set_attacks(int *A)
{
        if(attacks)
            delete[] attacks;
    attacks = A;
}

//--------------------------------------------

int dinosaur::get_dinosaurHealth()
{
    return dinosaurHealth;
}

int *dinosaur::get_attacks()
{
    return attacks;
}

#endif // DINOSAUR_H_INCLUDED 


errors

C:\Users\Chay\Desktop\Dinosaur Arena\main.cpp||In member function 'void dinosaur::trex()':|
C:\Users\Chay\Desktop\Dinosaur Arena\main.cpp|390|warning: unused variable 'attacks'|
C:\Users\Chay\Desktop\Dinosaur Arena\main.cpp||In member function 'void dinosaur::velociraptor()':|
C:\Users\Chay\Desktop\Dinosaur Arena\main.cpp|444|warning: unused variable 'attacks'|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h||In member function 'void player::conditionals()':|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|11|error: 'int dinosaur::dinosaurHealth' is protected|
C:\Users\Chay\Desktop\Dinosaur Arena\main.cpp|504|error: within this context|
||=== Build finished: 2 errors, 2 warnings ===|
Last edited on
OK, about the files:

Lines 53 onwards should be in player.cpp. You have player function definitions in main.cpp - maybe these should be in there own class named after the application - maybe Arena or something? Same story class declaration inn arena.h and function definitions in arena.cpp. The player class (and any other class you want to save stuff) should have a WriteToFile function. The Arena class could have a save function that calls the WriteToFile function for each class you want to save. See below for how the WriteToFile function might work.

Similarly, Lines 31 onwards should be in dinosaur.cpp.

As I mentioned earlier, trex & velociraptor should be classes of their own and inherit from dinosaur. Put common functions & variables into the dinosaur class, then only have specialised things in trex & velociraptor. Make the dinosaur constructor protected thereby preventing you from creating a dinosaur object, meaning you can only create trexes and velociraptors - or any other class derived from dinosaur.

Now for your member functions. Try to avoid having a get & set function for each member variable. If you provide a public get / set function for each member variable - you may as well make all the member variables public !!!

Instead, try to think about how things would work realistically. You can make use of the constructor to set values initially when the object is created. Provide functions that output a number of things. Remember that class functions have direct access to member variables, so player::WriteToFile can do it's stuff without the need for all the get functions.

The function name store could be construed as confusing - does it store something? Really you should have a Shop class.

HTH
Thanks for the reply and the feedback, i fixed the problem almost right after i made the post but im going to look into everything you mentioned and fix them but for right now the game is functional :D
Topic archived. No new replies allowed.