]> git.saurik.com Git - wxWidgets.git/blob - demos/bombs/game.cpp
XTI has own wxObjectEventFunction and has to follow event.h
[wxWidgets.git] / demos / bombs / game.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: bombs1.cpp
3 // Purpose: Implementation of the class BombsGame
4 // Author: P. Foggia 1996
5 // Modified by: Wlodzimierz Skiba (ABX) 2003
6 // Created: 1996
7 // RCS-ID: $Id$
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 # pragma implementation
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 # include "wx/wx.h"
24 #endif
25
26 #include "game.h"
27 #include <stdlib.h>
28 #include <limits.h>
29
30 #define PROB 0.2
31
32 #ifndef RAND_MAX
33 # define RAND_MAX INT_MAX
34 #endif
35
36
37 BombsGame::~BombsGame()
38 {
39 if (m_field)
40 {
41 delete[] m_field;
42 }
43 }
44
45 // Initialize the play field. Returns false on failure
46 bool BombsGame::Init(int aWidth, int aHeight)
47 {
48 m_gridFocusX = m_gridFocusY = -1;
49
50 int x, y;
51 int xx, yy;
52
53 if (m_field)
54 {
55 delete[] m_field;
56 }
57
58 m_field = new short[aWidth*aHeight];
59 if (!m_field)
60 {
61 m_width = m_height = 0;
62 return false;
63 }
64
65 m_width = aWidth;
66 m_height = aHeight;
67
68 for(x=0; x<m_width; x++)
69 {
70 for(y=0; y<m_height; y++)
71 {
72 m_field[x+y*m_width] = ((float)rand()/RAND_MAX <PROB)
73 ? BG_HIDDEN | BG_BOMB
74 : BG_HIDDEN;
75 }
76 }
77
78 m_numBombCells = 0;
79 for(x=0; x<m_width; x++)
80 for(y=0; y<m_height; y++)
81 if (m_field[x+y*m_width] & BG_BOMB)
82 {
83 m_numBombCells++;
84
85 for(xx=x-1; xx<=x+1; xx++)
86 if (xx>=0 && xx<m_width)
87 for(yy=y-1; yy<=y+1; yy++)
88 if (yy>=0 && yy<m_height && (yy!=y || xx!=x))
89 m_field[xx+yy*m_width]++;
90 }
91
92 m_numRemainingCells = m_height*m_width-m_numBombCells;
93
94 return true;
95 }
96
97 void BombsGame::Mark(int x, int y)
98 {
99 m_field[x+y*m_width] ^= BG_MARKED;
100 }
101
102 void BombsGame::Unhide(int x, int y)
103 {
104 if (!IsHidden(x,y))
105 {
106 return;
107 }
108
109 m_field[x+y*m_width] &= ~BG_HIDDEN;
110
111 if (!IsBomb(x,y))
112 {
113 m_numRemainingCells--;
114 }
115 }
116
117
118 void BombsGame::Explode(int x, int y)
119 {
120 m_field[x+y*m_width] |= BG_EXPLODED;
121 }