]>
git.saurik.com Git - wxWidgets.git/blob - demos/bombs/game.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implementation of the class BombsGame
4 // Author: P. Foggia 1996
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
16 #include "wx/wxprec.h"
20 #endif //precompiled headers
29 #define RAND_MAX INT_MAX
33 /*-------------------- BombsGame::~BombsGame() ---------------------*/
34 /*--------------------------------------------------------------------*/
35 BombsGame::~BombsGame()
40 /*------------------ int BombsGame::Init(width,height) -------------------*/
41 /* Initialize the play field. Returns 0 on failure */
42 /*--------------------------------------------------------------------------*/
43 int BombsGame::Init(int aWidth
, int aHeight
)
49 field
=(short *)malloc(aWidth
*aHeight
*sizeof(short));
57 for(x
=0; x
<width
; x
++)
58 for(y
=0; y
<height
; y
++)
59 { field
[x
+y
*width
] = ((float)rand()/RAND_MAX
<PROB
)?
65 for(x
=0; x
<width
; x
++)
66 for(y
=0; y
<height
; y
++)
67 if (field
[x
+y
*width
] & BG_BOMB
)
69 for(xx
=x
-1; xx
<=x
+1; xx
++)
70 if (xx
>=0 && xx
<width
)
71 for(yy
=y
-1; yy
<=y
+1; yy
++)
72 if (yy
>=0 && yy
<height
&& (yy
!=y
|| xx
!=x
))
75 normal_cells
=height
*width
-bombs
;
79 /*---------------------- BombsGame::Mark(x,y) -------------------------*/
80 /* Marks/unmarks a cell */
81 /*-----------------------------------------------------------------------*/
82 void BombsGame::Mark(int x
, int y
)
84 field
[x
+y
*width
] ^= BG_MARKED
;
87 /*------------------- BombsGame::Unhide(x,y) ------------------------*/
89 /*---------------------------------------------------------------------*/
90 void BombsGame::Unhide(int x
, int y
)
93 field
[x
+y
*width
] &= ~BG_HIDDEN
;
98 /*------------------- BombsGame::Explode(x,y) ------------------------*/
99 /* Makes a cell exploded */
100 /*----------------------------------------------------------------------*/
101 void BombsGame::Explode(int x
, int y
)
103 field
[x
+y
*width
] |= BG_EXPLODED
;