-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
416 lines (362 loc) · 11.7 KB
/
main.cpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
#include <string>
#include <time.h>
#include <SDL_ttf.h>
#define images_number 14
class ccampo {
private:
int **mine; //array che contiene le mine
int **local_campo; //array che contiene le informazioni grafiche/di gioco
int height,width; //altezza e larghezza in caselle
int numero_mine;
int caselle_libere;
int gameover; //gameover
//mousedown variables
int mousedown; //mouse cliccato
int mousedown_x; //coordinate di click in caselle
int mousedown_y;
int mousedown_old_value; //vecchio valore (local_campo) della casella cliccata
Uint8 mousedown_button; //bottone del mouse
//graphic variables
SDL_Surface* images_bmps[images_number]; //array che contiene le immagini caricate
SDL_Surface* screen; //form
//private functions
int mine_count(int x, int y); //conta le mine
int isMine(int x, int y); //check mina
void markMines(int x, int y); //segna tutte le mine dopo un gameover
void freeCell(int x, int y); //libera una cella che non contiene una mina
//se ha 0 mine adiacenti avvia la pulizia ricorsiva
SDL_Surface* load_image(char *filename); //carica e ottimizza un immagine
public:
ccampo(int height,int width,int numero_mine); //inizializzazione
~ccampo(); //deallocazione
void draw_campo(SDL_Surface* screen, int x, int y); //funzione di disegno del campo di gioco
void mousedown_event(int x,int y,Uint8 button); //eventi per i click del mouse
void mouseup_event(int x,int y,Uint8 button);
int get_width(); //larghezza
int get_height(); //altezza
int isGameover(); //il gioco è in gmaeover?
};
ccampo::ccampo(int height,int width,int numero_mine)
{
//inizializzazione del seme casuale
srand(time(NULL));
//inizializzazione delle variabili interne
(*this).height = height;
(*this).width = width;
(*this).numero_mine = numero_mine;
//inizializzazione degli array per il campo
(*this).local_campo = (int**) malloc(sizeof(int*) * width);
(*this).mine = (int**) malloc(sizeof(int*) * width);
if (((*this).local_campo == NULL) || ((*this).mine == NULL)) //controllo del risultato del malloc
{
printf("errore di allocazione!");
exit(1);
}
for (int i=0;i<width;i++)
{
(*this).local_campo[i] = (int*) malloc(sizeof(int) * height);
(*this).mine[i] = (int*) malloc(sizeof(int) * height);
if (((*this).local_campo[i] == NULL) || ((*this).mine[i] == NULL)) //controllo del risultato del malloc
{
printf("errore di allocazione!");
exit(1);
}
for(int j=0;j<height;j++) //inizializzazione della griglia e azzeramento delle mine
{
(*this).local_campo[i][j] = 9;
(*this).mine[i][j] = 0;
}
}
//inizializzazione delle mine
if(numero_mine >= (width*height))
{
printf("errore: numero di mine fuori dai limiti!");
exit(2);
}
while(numero_mine > 0)
{
int x,y;
x = rand() % width;
y = rand() % height;
if((*this).mine[x][y] != 1)
{
(*this).mine[x][y] = 1;
numero_mine--;
}
}
//inizializzazione delle immagini
//SDL_Surface* images_bmps[images_number];
char* images_names[images_number] = {"0","1","2","3","4","5","6","7","8","void","flag","qmark","mine","exploded"};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13
char *tmp_images_name_ptr = (char*)malloc(200);
for(int i=0;i<images_number;i++)
{
sprintf(tmp_images_name_ptr,"tiles/%s.bmp",images_names[i]);
(*this).images_bmps[i] = SDL_LoadBMP(tmp_images_name_ptr);
if (!(*this).images_bmps[i])
{
printf("Unable to load bitmap %s.bmp: %s\n", images_names[i], SDL_GetError());
exit(3);
}
}
free(tmp_images_name_ptr);
//inizializzazione del video delle SDL
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 4;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(width * 16, height * 16, 16, SDL_HWSURFACE|SDL_DOUBLEBUF);wwwwwwww
if ( !screen )
{
printf("Errore di inizializzazione del form: %s\n", SDL_GetError());
return 1;
}
(*this).mousedown = 0;
(*this).gameover = 0;
}
ccampo::~ccampo()
{
//free allocated arrays
for(int i=0;i<width;i++)
{
free((*this).mine[i]);
free((*this).local_campo[i]);
}
free((*this).mine);
free((*this).local_campo);
// free loaded bitmaps
for(int i=0;i<images_number;i++)
{
SDL_FreeSurface((*this).images_bmps[i]);
}
}
void ccampo::draw_campo(SDL_Surface* screen, int x, int y)
{
SDL_Rect dstrect;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 200, 190, 165));
// draw bitmaps
dstrect.x = x;
dstrect.y = y;
for(int i = 0;i<height;i++)
{
for(int j = 0;j<width;j++)
{
SDL_BlitSurface(images_bmps[(*this).local_campo[j][i]], 0, screen, &dstrect);
dstrect.x = dstrect.x + 16;
}
dstrect.y = dstrect.y + 16;
dstrect.x = 0;
}
// finally, update the screen :)
SDL_Flip(screen);
}
void ccampo::mousedown_event(int x,int y,Uint8 button)
{
if (((*this).local_campo[x][y] == 9) || ((*this).local_campo[x][y] == 10) || ((*this).local_campo[x][y] == 11))
{
(*this).mousedown = 1;
(*this).mousedown_x = x;
(*this).mousedown_y = y;
(*this).mousedown_old_value = (*this).local_campo[x][y];
(*this).mousedown_button = button;
(*this).local_campo[x][y] = 0;
}
}
void ccampo::mouseup_event(int x, int y, Uint8 button)
{
if((*this).mousedown)
{
if(((*this).mousedown_x == x) && ((*this).mousedown_y == y))
{
if((*this).gameover)
{
(*this).local_campo[x][y] = (*this).mousedown_old_value;
}
else if((button == SDL_BUTTON_LEFT) && (((*this).mousedown_old_value == 9) || ((*this).mousedown_old_value == 11)))
{
if((*this).isMine(x,y))
{
(*this).gameover = 1;
(*this).markMines(x, y);
}
else
(*this).freeCell(x,y);
}
else if((button == SDL_BUTTON_LEFT) && ((*this).mousedown_old_value == 10))
{
(*this).local_campo[x][y] = 10;
}
else if((button == SDL_BUTTON_RIGHT) && (((*this).mousedown_old_value == 9) || ((*this).mousedown_old_value == 10) || ((*this).mousedown_old_value == 11)))
{
(*this).local_campo[x][y] = (((*this).mousedown_old_value - 8) % 3) + 9; //cicla le tre modalita' (vuoto,bandiera,punto interrogativo)
}
}
else
{
(*this).local_campo[mousedown_x][mousedown_y] = (*this).mousedown_old_value;
}
(*this).mousedown = 0;
}
}
int ccampo::get_width()
{
return (*this).width;
}
int ccampo::get_height()
{
return (*this).height;
}
int ccampo::mine_count(int x, int y)
{
int count = 0;
int xmin,xmax,ymin,ymax;
if (x == 0) xmin = 0;
else xmin = x - 1;
if (x == ((*this).width - 1)) xmax = x;
else xmax = x + 1;
if (y == 0) ymin = 0;
else ymin = y - 1;
if (y == ((*this).height - 1)) ymax = y;
else ymax = y + 1;
for(int i = xmin; i <= xmax; i++)
for(int j = ymin; j <= ymax; j++)
count += (*this).mine[i][j];
return count;
}
int ccampo::isMine(int x, int y)
{
return (*this).mine[x][y];
}
void ccampo::markMines(int x, int y)
{
for(int i = 0; i < (*this).width; i++)
for(int j = 0; j < (*this).height; j++)
if((*this).mine[i][j])
(*this).local_campo[i][j] = 12;
(*this).local_campo[x][y] = 13;
}
void ccampo::freeCell(int x, int y)
{
int xmin,xmax,ymin,ymax;
(*this).local_campo[x][y] = (*this).mine_count(x,y);
if((*this).local_campo[x][y] == 0)
{
if (x == 0)
xmin = 0;
else
xmin = x - 1;
if (x == ((*this).width - 1))
xmax = x;
else
xmax = x + 1;
if (y == 0)
ymin = 0;
else
ymin = y - 1;
if (y == ((*this).height - 1))
ymax = y;
else
ymax = y + 1;
for(int i = xmin; i <= xmax; i++)
for(int j = ymin; j <= ymax; j++)
if((x != i) || (y != j))
{
if((*this).local_campo[i][j] > 8)
(*this).freeCell(i,j);
}
}
}
SDL_Surface* ccampo::load_image(std::string filename)
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = SDL_LoadBMP( filename.c_str() );
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
int main ( int argc, char** argv )
{
ccampo campominato(10,20,20);
// // initialize SDL video
// if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
// {
// printf( "Unable to init SDL: %s\n", SDL_GetError() );
// return 4;
// }
// // make sure SDL cleans up before exit
// atexit(SDL_Quit);
// create a new window
// SDL_Surface* screen = SDL_SetVideoMode(campominato.get_width() * 16, campominato.get_height() * 16, 16,
// SDL_HWSURFACE|SDL_DOUBLEBUF);
// if ( !screen )
// {
// printf("Errore di inizializzazione del form: %s\n", SDL_GetError());
// return 1;
// }
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
case SDL_MOUSEBUTTONDOWN:
if ((event.button.x <= (campominato.get_width() * 16)) && (event.button.y <= (campominato.get_height() * 16)))
campominato.mousedown_event(event.button.x / 16,event.button.y / 16,event.button.button);
break;
case SDL_MOUSEBUTTONUP:
if ((event.button.x <= (campominato.get_width() * 16)) && (event.button.y <= (campominato.get_height() * 16)))
campominato.mouseup_event(event.button.x / 16,event.button.y / 16,event.button.button);
break;
} // end switch
} // end of message processing
// // DRAWING STARTS HERE
//
campominato.draw_campo(screen,0,0);
} // end main loop
// all is well ;)
//printf("Exited cleanly\n");
return 0;
}