]> git.saurik.com Git - wxWidgets.git/blob - demos/bombs/game.cpp
Switch to using wxArtProvider::GetBitmap instead of GetIcon so if the
[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) since 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, bool easyCorner)
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 /* Force (0,0) not to have a bomb for those that don't want to have
79 to guess on the first move. Better would be to for the MS rule that
80 whatever is picked first isn't a bomb.
81 */
82 if(easyCorner)
83 {
84 m_field[0] = BG_HIDDEN;
85 }
86
87 m_numBombCells = 0;
88 for(x=0; x<m_width; x++)
89 for(y=0; y<m_height; y++)
90 if (m_field[x+y*m_width] & BG_BOMB)
91 {
92 m_numBombCells++;
93
94 for(xx=x-1; xx<=x+1; xx++)
95 if (xx>=0 && xx<m_width)
96 for(yy=y-1; yy<=y+1; yy++)
97 if (yy>=0 && yy<m_height && (yy!=y || xx!=x))
98 m_field[xx+yy*m_width]++;
99 }
100
101 m_numRemainingCells = m_height*m_width-m_numBombCells;
102 m_numMarkedCells = 0;
103
104 return true;
105 }
106
107 void BombsGame::Mark(int x, int y)
108 {
109 m_field[x+y*m_width] ^= BG_MARKED;
110 if (IsMarked(x, y))
111 m_numMarkedCells++;
112 else
113 m_numMarkedCells--;
114 }
115
116 void BombsGame::Unhide(int x, int y, bool b_selected)
117 {
118 if (!IsHidden(x,y))
119 {
120 return;
121 }
122
123 if (b_selected)
124 m_field[x+y*m_width] |= BG_SELECTED;
125
126 m_field[x+y*m_width] &= ~BG_HIDDEN;
127
128 if (!IsBomb(x,y))
129 {
130 m_numRemainingCells--;
131 }
132 }
133
134
135 void BombsGame::Explode(int x, int y)
136 {
137 m_field[x+y*m_width] |= BG_EXPLODED;
138 }