Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » board, pawn and classes

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
board, pawn and classes
HardTranceFan
Member #7,317
June 2006
avatar

Quote:

I hope it's not a Sams Teach Yourself C++ in 24hr books either.

Why, what's wrong with them? It's what got me started. And no-one's died yet :D

Ok, so my games never work, the PC resets with every recompile, and every time I get to the point where I can execute my program I need to re-install the windows. But otherwise it's fine. :P

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

kosmitek
Member #8,151
December 2006

Durnus - my programm shouldn't use classes, constructor and destructor ?? Why ??

And why I shouldn't use "goto" ??

I change my code
programme and it is compiling and gun, but is it in code everything okey ?

1 
2 
3#include<iostream>
4#include<ctime>
5#include <allegro.h>
6using namespace std;
7 
8 
9 
10 
11class boardboard
12{
13 private:
14 BITMAP *board;
15 BITMAP *pawn;
16 BITMAP *buffer;
17
18public:
19boardboard (BITMAP *a, BITMAP *b, BITMAP *c) //constructor
20{
21board=a;
22pawn=b;
23buffer=c;
24function (a, b, c);
25}
26 
27 
28 
29~boardboard() //destructor
30{
31 destroy_bitmap;
32
33}
34 
35 
36void function (BITMAP *a, BITMAP *b, BITMAP *c)
37{
38
39c=create_bitmap(SCREEN_W,SCREEN_H);
40a = load_bitmap("board.bmp",NULL);
41b = load_bitmap("pawn.bmp",NULL);
42int tab1[2][10]= {
43 {100,220,370,500,700,700,500,350,220,100},
44 {100,100,100,100,100,340,340,340,340,340},
45 };
46int tab2[10]={0,1,2,3,4,5,6,7,8,9};
47 
48int *index=&tab2[0];
49int *zero=&tab2[0];
50int *nine=&tab2[9];
51int module;
52int eyes;
53srand (time(0));
54 
55 
56while(!key[KEY_ESC])
57{
58eyes = 1+rand()%3;
59clear_to_color(c,makecol(255,255,255));
60blit(a,c,0,0,0,0,a->w,a->h);
61blit(b,c,0,0,tab1[0][*index],tab1[1][*index],b->w,b->h);
62textprintf_ex(c, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes);
63textprintf_ex(c, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right");
64textprintf_ex(c, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game");
65blit(c,screen,0,0,0,0,SCREEN_W,SCREEN_H);
66readkey();
67 
68 
69 
70direction:
71if (key[KEY_LEFT])
72{
73if(*index==0||*index==1||*index==2||*index==3||*index==4)
74 {
75 if ((*index-eyes)>=*zero)
76 {
77 index=index-eyes;
78 }
79 else
80 {
81
82 index=nine-(eyes-*index-1);
83 }
84
85 }
86else if(*index==5||*index==6||*index==7||*index==8||*index==9)
87 
88 {
89 if ((*index+eyes)<=*nine)
90 {
91 index=index+eyes;
92 }
93 else
94 {
95 module=eyes-(*nine-*index);
96 if (module<0)
97 {
98 module=-module;
99 }
100 index=zero+module-1;
101 }
102
103 }
104}
105 
106 
107 
108
109else if (key[KEY_RIGHT])
110{
111 if(*index==0||*index==1||*index==2||*index==3||*index==4)
112 {
113 index=index+eyes;
114
115 }
116 else
117 {
118 index=index-eyes;
119 }
120
121
122}
123
124
125
126else
127{
128 if (!key[KEY_ESC])
129 {
130 goto direction;
131 }
132
133}
134 
135}
136 
137 
138 
139}
140};
141 
142
143 
144 
145 
146 
147 
148 
149 
150 
151 
152int main()
153{
154
155allegro_init();
156install_keyboard();
157set_color_depth(32);
158set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
159 
160 
161boardboard bb(NULL, NULL, NULL);
162 
163bb.boardboard::~boardboard();
164allegro_exit();
165return 0;
166}
167END_OF_MAIN()

everything okay ?????

PS. thanks TeamTerradactyl for your advices :)

imaxcs
Member #4,036
November 2003

This is a joke right? This is not even closely valid C++- - code! Did you ever try to compile it? Even if it would, by skimming through it, I can say it would crash like hell... Listen to people's advises or at least post the freaking compiler-errors!! >:(

raccoon
Member #7,478
July 2006

you shouldn't use classes, constructors and destructors because it seems you have no idea how to use them.
you shouldn't use goto because it's dated, makes your code hard to read, and there's is nothing which you can do with goto, which can't be done without goto in a more elegant way.

if you really read a c++-book (which i doubt), you should have come across a thing called while-loop. you could replace your goto with this statement.
also, passing BITMAPs to your function-function (by the way: nice naming), is senseless in your case, but passing NULL-pointers seems like even worse.
there is just so much wrong with your code, it's hard to make a start.

kosmitek
Member #8,151
December 2006

But what is wrong in this code:
(IN attachment you have all my program)

1#include<iostream>
2#include<ctime>
3#include <allegro.h>
4using namespace std;
5 
6 
7 
8 
9class boardboard
10{
11 private:
12 BITMAP *board;
13 BITMAP *pawn;
14 BITMAP *buffer;
15
16public:
17boardboard (BITMAP *a, BITMAP *b, BITMAP *c) //constructor
18{
19board=a;
20pawn=b;
21buffer=c;
22function (a, b, c);
23}
24 
25 
26 
27~boardboard() //destructor
28{
29 destroy_bitmap(board);
30 destroy_bitmap(pawn);
31 destroy_bitmap(buffer);
32
33}
34 
35 
36void function (BITMAP *a, BITMAP *b, BITMAP *c)
37{
38
39c=create_bitmap(SCREEN_W,SCREEN_H);
40a = load_bitmap("board.bmp",NULL);
41b = load_bitmap("pawn.bmp",NULL);
42int tab1[2][10]= {
43 {100,220,370,500,700,700,500,350,220,100},
44 {100,100,100,100,100,340,340,340,340,340},
45 };
46int tab2[10]={0,1,2,3,4,5,6,7,8,9};
47 
48int *index=&tab2[0];
49int *zero=&tab2[0];
50int *nine=&tab2[9];
51int module;
52int eyes;
53srand (time(0));
54 
55 
56while(!key[KEY_ESC])
57{
58eyes = 1+rand()%3;
59clear_to_color(c,makecol(255,255,255));
60blit(a,c,0,0,0,0,a->w,a->h);
61blit(b,c,0,0,tab1[0][*index],tab1[1][*index],b->w,b->h);
62textprintf_ex(c, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d", eyes);
63textprintf_ex(c, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right");
64textprintf_ex(c, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game");
65blit(c,screen,0,0,0,0,SCREEN_W,SCREEN_H);
66readkey();
67 
68 
69 
70direction:
71if (key[KEY_LEFT])
72{
73if(*index==0||*index==1||*index==2||*index==3||*index==4)
74 {
75 if ((*index-eyes)>=*zero)
76 {
77 index=index-eyes;
78 }
79 else
80 {
81
82 index=nine-(eyes-*index-1);
83 }
84
85 }
86else if(*index==5||*index==6||*index==7||*index==8||*index==9)
87 
88 {
89 if ((*index+eyes)<=*nine)
90 {
91 index=index+eyes;
92 }
93 else
94 {
95 module=eyes-(*nine-*index);
96 if (module<0)
97 {
98 module=-module;
99 }
100 index=zero+module-1;
101 }
102
103 }
104}
105 
106 
107 
108
109else if (key[KEY_RIGHT])
110{
111 if(*index==0||*index==1||*index==2||*index==3||*index==4)
112 {
113 index=index+eyes;
114
115 }
116 else
117 {
118 index=index-eyes;
119 }
120
121
122}
123
124
125
126else
127{
128 if (!key[KEY_ESC])
129 {
130 goto direction;
131 }
132
133}
134 
135}
136 
137 
138 
139}
140};
141 
142
143 
144 
145 
146 
147 
148 
149 
150 
151 
152int main()
153{
154
155allegro_init();
156install_keyboard();
157set_color_depth(32);
158set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
159 
160 
161boardboard bb(NULL, NULL, NULL);
162 
163bb.boardboard::~boardboard();
164allegro_exit();
165return 0;
166}
167END_OF_MAIN()

This programm guns very good so what is wrong ???
imaxcs you said:
"I can say it would crash like hell"
but it doesn't crah

X-G
Member #856
December 2000
avatar

All right, guys, let's just stop taking this thread seriously. Either it's a troll, or so blithely ignorant as to be beyond our help. We've already given the only correct response: Get a C++ book and follow it from the beginning to the end.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

kosmitek
Member #8,151
December 2006

X-G I am not troll :(
I use your advices and thanks some of you my programm gun but what is in this programm wrong (without "goto") ?? :(

X-G if you don't want help me you didn't write.

X-G my programm is almost good and you can only criticize me - you do not want even to say what is bad despite that my programme great run.

And "goto" must be because if somebody will press other button than: "->; <-" then program go to "direction" - and player will have press "->; <-" - the number of eyes will not change, if I use "continue" then player also will have press "->; <-; Esc" but the number of eyes will change - and I don't want that the number of eyes change until player press "->" or "<-".

TeamTerradactyl
Member #7,733
September 2006
avatar

kosmitek: Attached is a cleaned-up version of your code.

Since you really don't need to use a class/struct for this program, I have removed them. I also added a bunch of comments so you could read the changes I've made, or else what's going on in the program.

Thank you for posting your source code so I could see what your game was supposed to do.

You may want to convert your .BMP images into .PCX or another format: The filesize will be a LOT smaller (the BMP images are uncompressed, while PCX are).

I also changed this to WINDOWED mode instead of full-screen (my computer crashes a lot whenever it's in full-screen, for some reason), so if you need to change it back, you can do so.

I hope this helps.

I think that if you WANTED to use a class/struct, the best thing to do would be to have something that has a lot of different data: your "pawn" could be an animated tile, and so the pawn could be a class, or if you have several different "boards", you could make each of those with a separate class (like, one could have a wood background with the "tiles" a few inches apart, and another could have a stone background with the "tiles" in more of a circular pattern and different coordinates, etc.).

What you're doing right now doesn't really need a class, since that looks like you're trying to use your class as a substitute for the main body of your game.

Anyway, hope this helps (and others can take a look at the code and make sure I didn't leak a bunch of memory :P )

Paul whoknows
Member #5,081
September 2004
avatar

This site should be enough.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

kosmitek
Member #8,151
December 2006

TeamTerradactyl thank you very much for your help and comments in the code.

I wanted use class, constructor and destructor because I want develop my game and for example: "destroy_bitmap" - it was ideal to use destructor etc.
But if I want to develop my game for example that if player will stand up on same field he will get money - so should I use now class, constructor and destructor in this code (because I want that my final version of game using classes, constructors, destructors etc.) ?

TeamTerradactyl
Member #7,733
September 2006
avatar

kosmitek,

Let's say that that little "pink pawn" that you had in your code was going to become a nice little animated sprite (like a swirling "rock" or a "chess piece" that occasionally moves or whatever).

   +---+---+---+---+---+---+
   | X |   | X |   |   |   |
   +---+---+---+---+---+---+
   |   |   |   | X |   |   |
   +---+---+---+---+---+---+

Say you had three pawns on the screen at once. Declaring the pawn as a class would allow each of the three pawns to have a different look, have different x/y coordinates, be facing different directions, etc. For those, it would be fine to use a class, since each pawn would need to keep track of similar data (direction facing, coordinates, etc) but all the data itself would be different (the top two are facing down, the bottom is facing up, so each would have a different "direction").

As an example of a usage of classes, my games have several bad guys/monsters. Each bad guy/monster keeps track of its current location, the direction is is trying to go, and its health. If I attack one of those monsters, its HP goes down slightly. When the monster dies, I don't need to keep track of that monster anymore and "delete" it. When it gets "deleted", the class destructor frees up all the data it was storing (like the BITMAP it was using, its location, etc.). In that case, it would be useful to have a class.

So kosmitek, if you could describe for us, what do you want to use a "class" for in your game? If you have only 1 pawn and 1 "game board", I don't think you would need to use a class.

X-G
Member #856
December 2000
avatar

I especially love how you all seem to think that "classes" is some kind of feature you "add to your code". THAT'S NOT HOW IT WORKS! Object-oriented programming is a design paradigm that you base your entire architecture on. This is why I'm sure you don't understand OOP.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

kosmitek
Member #8,151
December 2006

TeamTerradactyl you are very very very helpful - thank you a lot.

So I want do this game:
Player throws bone and pawn moves about some number of fields on left or right ( player decides), every time when pawn stands up on field will be drawn event, for example: "you get 100 dollars" or "you lose 50% of your weapons", player can stand up on some field which is shop - there he can buy weapons but if he stand up on field which is shop and then he throw 2 eyes then he lose all his weapons. After 20 movements he will fight with monster so he must have many weapons, if he has too little weapons he will die.

So it is my plan of game - where I can apply here OOP ? ;D

Steve Terry
Member #1,989
March 2002
avatar

Kosmetek: Just out of curiosity how old are you?

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

TeamTerradactyl
Member #7,733
September 2006
avatar

kosmitek:

This sounds slightly similar to Monopoly, where each "field" has a different attribute (like "collect 200 dollars" or "You owe [some person] $50", etc.). If this is the case, then I would suggest making the "field" a class:

1class Field
2{
3 public:
4 Field(const char *filename);
5 ~Field();
6 
7 void setAttribute(const Attribute &newAttribute);
8 Attribute getAttribute(void);
9 
10 void draw(BITMAP *target, const int &x, const int &y, const float &rotate);
11 private:
12 BITMAP *pPicture;
13 Attribute pTheAttribute; // <-- You would have to define the "Attribute" class yourself
14};
15 
16Field::Field(const char *filename)
17{
18 pPicture = load_bitmap(filename, NULL);
19}
20 
21Field::~Field()
22{
23 destroy_bitmap(pPicture);
24 pPicture = NULL;
25}
26 
27void Field::setAttribute(const Attribute &newAttribute)
28{
29 pTheAttribute = newAttribute;
30}
31 
32Attribute Field::getAttribute(void)
33{
34 return pTheAttribute;
35}
36 
37void draw(BITMAP *target, const int &x, const int &y, const float &rotate = 0.0)
38{
39 if (rotate != 0.0)
40 rotate_sprite(target, pPicture, x, y, rotate);
41 else
42 draw_sprite(target, pPicture, x, y);
43}

Then, each "field" you land on can have its own attributes and pictures. This would make sense, since you would use several "field" objects where you would really only use a handful of "pawn" objects.

You would need to define several different "Attributes" that this class would be able to use: one would be a "shop" where you buy stuff, another would be a "thief" where you lose 50% of your weapons, etc. How you implement your "Attributes" would be up to you. But I hope this helps answer your question.

Steve Terry
Member #1,989
March 2002
avatar

What a strange game, throwing bones and eyes...

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

TeamTerradactyl
Member #7,733
September 2006
avatar

Steve:
Bones are dice
Eyes (snake eyes): double 1's

kosmitek
Member #8,151
December 2006

ups - sorry - bones = dice

TeamTerradactyl thank you a lot - you must be very good programmer.

Your idea is too complicated for me. I would like to use in my game some simple class, constructor, destruktor and heirdom but I have no idea - maybe I should do two monsters and when player kill one monster then he heirdom his strength and then he fight with second monster but where can I use here simply class, constructor and destructor..... - class "monster" ??

TeamTerradactyl
Member #7,733
September 2006
avatar

kosmitek: Yes, the "monster" would be an excellent place to use a class. Another would be the "player" because you need to keep track of the player's position, health, strength, etc.

Albin Engström
Member #8,110
December 2006
avatar

TeamTerradactyl has a heart of.. gold, i really want to see you change, kosmitek.

Audric
Member #907
January 2001

(note: It's Christmas)
To kosmitek:
'heirdom'? you probably mean inheritance...
It has nothing to do with "the player inherits the monster's money" :(

In the case of your game, one possible use of inheritance could be if monsters stay on the board:
"The player and the (visible) monsters ARE pawns on the board: They are represented by a BITMAP, and they are positioned on one of the cells. If these two informations (image and position) are "stored" in the parent class (ex: Pawn), you can code a function Pawn::draw() to draw them on the screen, this single function will work well for a Player or a Monster - anything that inherits Pawn.

This is only an example, certainly not THE solution for what you want to do. People here have much experience coding 2D objects moving on a background (I've just described 40% of the games on a.cc), but board games are much, much more difficult to design and code, and frankly there's no evident OO design there. If you've never DESIGNED something in object-oriented design, a board game is the worst choice to start...You will never complete the game, and you will not even learn why your design does not make your game go in the right direction.

If you want to progress in your game, structured programming is the way to go...until at least you completely understand how to use Allegro for a game, with some code for all initializations, a game loop, some code for logic, some other code for display, etc... and no memory messup.

Steve Terry
Member #1,989
March 2002
avatar

I guess even 23yrold3yrold started out like Kosmitek... there may be hope ;D

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

X-G
Member #856
December 2000
avatar

Only if he starts taking the only good advice in this thread and reads a damn book and starts from the beginning, and drops the notions he has ingrained in his head right now. He needs to start over, not be further confused and fed misinformation.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

[iCe]CorTez
Member #1,856
January 2002
avatar

:>

Only The Strong survive

kosmitek
Member #8,151
December 2006

Thank you for you advices, especially thank TeamTerradactyl. I do not thank "X-G", he only discouraged me and he write nothing helpful.

I have fast change my code as you say. Now I have simply class, late I will add here that if player stand on some field then something will done, I will also add class monster etc.

But can you say me that my fragments of code touch OOP are okey ? And why my destructor isn't okey ?

1#include<iostream>
2#include<ctime>
3#include <allegro.h>
4using namespace std;
5 
6 
7 
8 
9 
10class player
11{
12 private:
13 int money;
14 int health;
15 int weapon;
16 int power;
17
18
19 public:
20 player(int a, int b, int c, int d)
21 {
22 money=a;
23 health=b;
24 weapon=c;
25 power=d;
26 }
27
28 int change_money(int x)
29 {
30 money=money+x;
31 return money;
32 }
33
34 int change_health(int x)
35 {
36 health=health+x;
37 return health;
38 }
39
40
41 int change_weapon(int x)
42 {
43 weapon=weapon+x;
44 return weapon;
45 }
46
47
48 int change_power(int x)
49 {
50 power=power+x;
51 return power;
52 }
53
54// ~player(); // destructor - something is wrong ??
55
56};
57 
58 
59 
60 
61 
62void start_game(BITMAP *board, BITMAP *pawn, BITMAP *buffer)
63{
64
65int tab1[2][10]={
66 {100,220,370,500,700,700,500,350,220,100},
67 {100,100,100,100,100,340,340,340,340,340},
68 };
69 
70int tab2[10]={0,1,2,3,4,5,6,7,8,9};
71 
72int *index=&tab2[0];
73int *zero=&tab2[0];
74int *nine=&tab2[9];
75int module;
76int eyes;
77srand (time(0));
78int i=21;
79int y=1;
80 
81player game(99, 99, 99, 99);
82 
83while(!key[KEY_ESC]&&i!=0)
84{
85i--;
86eyes = 1+rand()%3;
87clear_to_color(buffer,makecol(255,255,255));
88blit(board,buffer,0,0,0,0,board->w,board->h);
89blit(pawn,buffer,0,0,tab1[0][*index],tab1[1][*index],pawn->w,pawn->h);
90textprintf_ex(buffer, font, 267, 580, makecol(0, 0, 0), -1, "You have: %d money, %d health, %d weapon and %d power.", game.change_money(y), game.change_money(y), game.change_weapon(y), game.change_power(y));
91textprintf_ex(buffer, font, 267, 600, makecol(0, 0, 0), -1, "You throw: %d, it left yo %d movements", eyes, i);
92textprintf_ex(buffer, font, 267, 620, makecol(0, 0, 0), -1, "-> - left, <- - right");
93textprintf_ex(buffer, font, 267, 640, makecol(0, 0, 0), -1, "ESC - the end of the game");
94blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
95
96 
97 
98 
99direction:
100readkey();
101if (key[KEY_LEFT])
102{
103if(*index>=0&&*index<=4)
104 {
105 if ((*index-eyes)>=*zero)
106 {
107 index=index-eyes;
108 }
109 else
110 {
111
112 index=nine-(eyes-*index-1);
113 }
114
115 }
116else if(*index>=5&&*index<=9)
117 
118 {
119 if ((*index+eyes)<=*nine)
120 {
121 index=index+eyes;
122 }
123 else
124 {
125 module=eyes-(*nine-*index);
126 if (module<0)
127 {
128 module=-module;
129 }
130 index=zero+module-1;
131 }
132
133 }
134}
135 
136 
137 
138
139else if (key[KEY_RIGHT])
140{
141 if(*index>=0&&*index<=4)
142 {
143 index=index+eyes;
144
145 }
146 else
147 {
148 index=index-eyes;
149 }
150
151
152}
153
154
155
156else
157{
158 if (!key[KEY_ESC])
159 {
160 goto direction;
161 }
162
163}
164 
165 
166 
167}
168
169//game.~player(); // destructor - something is wrong
170
171}
172 
173 
174 
175 
176 
177 
178int main()
179{
180BITMAP *board = NULL;
181BITMAP *pawn = NULL;
182BITMAP *buffer = NULL;
183allegro_init();
184install_keyboard();
185set_color_depth(32);
186set_gfx_mode(GFX_AUTODETECT,1024,768,0,0);
187buffer=create_bitmap(SCREEN_W,SCREEN_H);
188 
189 
190board = load_bitmap("board.bmp",NULL);
191pawn = load_bitmap("pawn.bmp",NULL);
192 
193start_game(board, pawn, buffer);
194 
195 
196destroy_bitmap(board);
197destroy_bitmap(pawn);
198destroy_bitmap(buffer);
199allegro_exit();
200return 0;
201}
202END_OF_MAIN()

 1   2   3 


Go to: