im trying to use pointers to get one object to take damage from another

so in my mind i wanted something like playerobject.takehealthfrom->baddy object i could only make pointers point to class however and i got something runnning which would crash, i think i was taking health from the constructor and not the object...heres my code...the problem part is in the function fight at the bottom, objects are in main and naturaly class is at the top...i proly need to learn propper indentation...i will incoperate good habbits in my next code...thanks for looking <3

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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>


using namespace std;
















class baddy
{
public:


int givehit ()
{
    bealth=bealth-10;
    if (bealth<10)
    {
        cout << "you got the wanker"<<endl;
        return 2;
    }
}
 int bealth;
 string bame;
 int battackpower;
 int bosx;
 int bosy;
 char bad;
 int bice;
};

class player
{
public:

void attack (baddy *e)
{
    e->givehit();
}

int gethit ()
{
    health=health-10;
    if (health<10)
    {
        cout << "your  dead innit"<<endl;
        return 1;
    }
}
 int health;
 string name;
 int attackpower;
 int dice;
 char you;
};


int getrandom ();
void fight ();

char z = '*';
char x = ' ';

char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };

int bosx=2;
int bosy=2;
char them = '!';
char you = 'O';

void start ();
void getbrd ();
void bdymove ();
void move ();

int posx = 5;
int posy = 5;








string enter;

int main ()
{
cout << "enter your name contender 1" <<endl;
cin >> enter;
system ("cls");
baddy estateagent;
estateagent.bame="barry";

estateagent.bealth=20;
player playa;
playa.name=enter;

playa.health=100;



start();
while (1)
{

getbrd();
move();
bdymove();
fight();
system ("CLS");
}
return 0;
}












void getbrd ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    brdray [posx] [posy] = x;
    int chekposx = posx;
    int chekposy = posy;
    int chukposx=posx;
    int chukposy=posy;
chekposx=chekposx-1;
chekposy=chekposy-1;
chukposx=chukposx+1;
chukposy=chukposy+1;
    switch (mv)
    {


    case 1:
    if (brdray[chekposx] [chekposy]!=z)
    {posx=posx-1;}
    break;
    case 2:
    if (brdray[chukposx] [chukposy]!=z)
    {posx=posx+1;}
     break;
    case 3:
    if (brdray[chekposx] [chekposy]!=z)
    {posy=posy-1;}
     break;
    case 4:
    if (brdray[chukposx] [chukposy]!=z)
    {posy=posy+1;}
     break;
    }

    brdray [posx] [posy] =you;
}


void start()
{
brdray [posx] [posy]=you;
brdray [bosx] [bosy]=them;
}

void bdymove ()
{
brdray [bosx] [bosy] = x;

if (posx>bosx)
{
    bosx++;
}
if (posx<bosx)
{
    bosx--;
}
if (posy>bosy)
{
    bosy++;
}
if (posy<bosy)
{
    bosy--;
}


brdray [bosx] [bosy]=them;
}

void fight ()
{
int hilo;
int randomnum;
randomnum=getrandom();
player *playpointer;
baddy  *badpointer;
if (brdray [posx] [posy]==brdray [bosx] [bosy])
{
    cout << "you must FIGHT!! 1 to block 2 to attack"<<endl;
    cin >> hilo;
    switch (hilo)
    {
        case 1:
        cout << "you block his attack...";
        if (hilo < randomnum)
        cout <<"and hit him";
        player.attack(&baddy);// i know each of these are different just i wanted to show my trains of thought
        if  (hilo > randomnum)
        cout << "only to get hit when open";
       baddy->givehit(player);//though i dont know what i was thinking here
        break;
        case 2:
        cout << "you swing...";
        if (hilo > randomnum)
        cout << "and get the bastard";
        playa.attack(&baddy);  //i dont see why this doesnt work
        if (hilo < randomnum)
        cout << "you misss and he kicks you up the arse";
        baddy->givehit(); //and here proves i have still not grasped the -> 
        break;
    }

    }
}


int getrandom ()
{
int number;
 srand(time_t(0));
 number=1+(rand()%6);
 return number;
}



i want to say i had a post simlar to this but they are different subjectts, this one i wanted to get the objects to hit each other the other i need a pointer explanation...
your problem is that you don't check the limits in your move() functions. For instance chekposx or bosx must not be negative
the givehit/gethit deal is the fing i struggle wiv

its all right they wont be bdymove move follows move and move wont cross the borders because chechpos is one step ahead at all times

Yes, givehit()/gethit() both have the problem that they don't return anything usefull if health/bealth are greater or equal 10. But since you do not check the result, it doesn't matter.

The names of your variables are terrible and hence hard to follow.

This

if (brdray [posx] [posy]==brdray [bosx] [bosy])

does not tell whether the opponents are at the same place. It rather checks whether the same symbol is at different places.


On line 242/243 you're using uninitialized pointer. Which certainly crashes. if you want to use pointers you have to pass the objects created in main() to fight():
1
2
3
4
void fight (player *playpointer, baddy  *badpointer)
{
...
}


Like i said (later) another crash occurs in move() (if the user enters the wrong direction) or more likely in bdymove (). Trust me, it does
Last edited on
kay ive changed it a bit but i still havnt mastered the pointing stuff...god its messy...i popped system pause in just to see whats happening.

i think i should start ALL over again and tidier but first i would like to get the health checking working, ive recently discovered new and as you can see dont properly understand it im not even sure if its even working in checkmove like i made it look like i do :)

im also aware that im changing the class itself like some kinda weird function rather than the class object like ive been trying to suss out---i think thats the very element of what i need to know here----see then i can start again tidier

because i havnt sussed out how to check 'you's location i have kind of bodged the answer by having their locations managed instead..all adding to a huge messy mess messingson and sons

i suppose its a brave man with aspergers like concetration to suss out my code through the mess and an elite headshrinker to suss out how to explain how what im thinking is wrong through my mind mess :/

its the greatest challenge yet.

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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
 <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>


using namespace std;










class baddy
{
public:
baddy ()
{
    bealth=20;
}

int givehit ()
{
    bealth=bealth-10;
    if (bealth<10)
    {
        cout << "you got the wanker"<<endl;
        return 2;
    }
}
    void getbaddydam ()
    {
        cout << "baddy has " << bealth << "health"<<endl;
    }

 int bealth;
 string bame;
 int battackpower;
 int bosx;
 int bosy;
 char bad;
 int bice;
};

class player
{
    public:
    player ()
    {
        health = 90;
    }


void attack (baddy *e)
{
    e->givehit();
}
void gethealth ()
{
    cout << health;
}
int gethit ()
{
    health=health-10;
    if (health<10)
    {
        cout << "your  dead innit"<<endl;
        return 1;
    }
}
 int health;
 string name;
 int attackpower;
 int dice;
 char you;
};


int getrandom ();
void fight ();

char z = '*';
char x = ' ';

char brdray [10] [10] = {  {z,z,z,z,z,z,z,z,z,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z},
{z,x,x,x,x,x,x,x,x,z}, {z,z,z,z,z,z,z,z,z,z} };

int bosx=2;
int bosy=2;
char them = '!';
char you = 'O';

void start ();
void getbrd ();
void bdymove ();
void move ();

int checkhealth ();


int posx = 5;
int posy = 5;








string enter;

int main ()
{
cout << "enter your name contender 1" <<endl;
cin >> enter;
system ("cls");
baddy estateagent;
estateagent.bame="barry";

estateagent.bealth=20;

player playa;
playa.name=enter;

playa.health=100;
int q=1;


start();
while (q)
{

getbrd();
move();
bdymove();
fight();
checkhealth();
system ("CLS");
}

cout <<playa.name<< "dies horribly...you lose" <<endl;
return 0;
}












void getbrd ()
{
for (int a = 0;a<10;a++)
 {
     for (int b =0;b<10;b++)
        cout << brdray [a][b];
        cout<<endl;
 }

}

void move ()
{
    int mv;
    cout << "1 is up, 2 is down, 3 is left and 4 is right"<< endl;
    cin >> mv;
    char w;
    char a,x,d,n;

    brdray [posx] [posy] = x;
    int chekposx = posx;
    int chekposy = posy;
    int chukposx=posx;
    int chukposy=posy;
chekposx=chekposx-1;
chekposy=chekposy-1;
chukposx=chukposx+1;
chukposy=chukposy+1;
    switch (mv)
    {


    case 1:
    if (brdray[chekposx] [chekposy]!=z)
    {posx=posx-1;}
    break;
    case 2:
    if (brdray[chukposx] [chukposy]!=z)
    {posx=posx+1;}
     break;
    case 3:
    if (brdray[chekposx] [chekposy]!=z)
    {posy=posy-1;}
     break;
    case 4:
    if (brdray[chukposx] [chukposy]!=z)
    {posy=posy+1;}
     break;
    }

    brdray [posx] [posy] =you;
}


void start()
{
brdray [posx] [posy]=you;
brdray [bosx] [bosy]=them;
}

void bdymove ()
{
brdray [bosx] [bosy] = x;

if (posx>bosx)
{
    bosx++;
}
if (posx<bosx)
{
    bosx--;
}
if (posy>bosy)
{
    bosy++;
}
if (posy<bosy)
{
    bosy--;
}


brdray [bosx] [bosy]=them;
}

void fight ()
{
int hilo;
int randomnum;
randomnum=getrandom();
player *playpointer;
baddy  *badpointer;
playpointer = new player;
badpointer = new baddy;
if (brdray [posx] [posy]==brdray [bosx] [bosy])
{
    cout << "you must FIGHT!! 1 to block 2 to attack"<<endl;
    cin >> hilo;
    switch (hilo)
    {
        case 1:
        cout << "you block his attack...";
        if (hilo < randomnum)
        cout <<"and hit him";
        badpointer->givehit();
        badpointer->getbaddydam();
        break;
        if  (hilo > randomnum)
        cout << "only to get hit when open";
       playpointer->gethit();
       playpointer->gethealth();
        break;
        case 2:
        cout << "you swing...";
        if (hilo > randomnum)
        cout << "and get the bastard";
        badpointer->givehit();
        badpointer->getbaddydam ();
          break;
        if (hilo < randomnum)
        cout << "you miss and he kicks you up the arse";
       playpointer->gethit();
       playpointer->gethealth();
        break;
    }

    }

}


int getrandom ()
{
int number;
 srand(time_t(0));
 number=1+(rand()%6);
 return number;
}

int checkhealth()
{
player *playpointer;
baddy  *badpointer;
playpointer = new player;
badpointer = new baddy;


system ("pause");
if ((badpointer->bealth)==10)
{
 bosx=1;
 bosy=1;
 brdray [bosx] [bosy] = them;
}
if ((playpointer->health)==20)
{
int q=0;
return q;
}


}
Last edited on
The first thing I would suggest is making a multi-document project. Say the name of the file that includes the code you just posted is main.cpp. Find that file in your folders and add two more files, player.h and player.cpp (both of these are text files). You can then do something like this:

player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>

class Player
{
public:
  Player(void);  // putting in void is a safer way to say No Arguments. 
  ~Player(void);

  void speak(void);

#endif 


player.cpp
1
2
3
4
5
6
7
8
9
#include player.h  //(player.h needs to be in quotes, a button broken on this keyboard) 

Player::Player(void){}
Player::~Player(void) {} 

void Player::speak(void)
{
  std::cout << I am a player\n;  // Again, quotes
}


main.cpp
1
2
3
4
5
6
7
8
#include player.h  // quotes

int main(void)
{
  Player me;
  me.speak();
  return 0;
}


If you want to use pointers:
1
2
3
4
5
6
7
8
int main(void)
{
  Player *me;
  me = new Player();
  me->speak();
  delete me;  // for every new, you need a delete, very important 
  return 0;
}


You have a lot of issues with scope. The player and baddie you make in main() are not the same as in fight(). Such is also the reason that checkhealth() doesnt work. There also seem to be a lot of things youve put into the classes that you dont use (such as the pos variables).

I would recommend starting all over. My reason for this is that it looks like you have a bunch of half working things, and my opinion is that it would be better to get one thing working at a time.

Also, when you find something you don't understand, start a new project. Figure out everything you need to know in that project, and then implement what you have learned into your game. I think that will help a lot. Especially if you start to use more than one file in your projects. A file that works one place will work in another, so once you get something simple working all you have to do is copy/paste the file into your main project.
must...grasp....pointers...yeah kay i will do as you say you are right i will start fresh i might put off using headers and such for now or while i review lots of small programs you are right i will start again... :)


Topic archived. No new replies allowed.