]> git.saurik.com Git - wxWidgets.git/blame - demos/bombs/game.cpp
Baked files for 2.6.1
[wxWidgets.git] / demos / bombs / game.cpp
CommitLineData
025e88c5
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: bombs1.cpp
3// Purpose: Implementation of the class BombsGame
4// Author: P. Foggia 1996
c8059953 5// Modified by: Wlodzimierz Skiba (ABX) since 2003
025e88c5
JS
6// Created: 1996
7// RCS-ID: $Id$
8// Copyright: (c) 1996 P. Foggia
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
0c65afdb 13# pragma implementation
025e88c5
JS
14#endif
15
16#include "wx/wxprec.h"
17
0c65afdb
DS
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
025e88c5 22#ifndef WX_PRECOMP
0c65afdb
DS
23# include "wx/wx.h"
24#endif
025e88c5
JS
25
26#include "game.h"
27#include <stdlib.h>
28#include <limits.h>
29
30#define PROB 0.2
31
32#ifndef RAND_MAX
0c65afdb 33# define RAND_MAX INT_MAX
025e88c5
JS
34#endif
35
36
025e88c5 37BombsGame::~BombsGame()
0c65afdb
DS
38{
39 if (m_field)
40 {
41 delete[] m_field;
42 }
43}
44
45// Initialize the play field. Returns false on failure
43c3922b 46bool BombsGame::Init(int aWidth, int aHeight, bool easyCorner)
0c65afdb
DS
47{
48 m_gridFocusX = m_gridFocusY = -1;
49
50 int x, y;
025e88c5
JS
51 int xx, yy;
52
0c65afdb
DS
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;
025e88c5 75 }
0c65afdb
DS
76 }
77
43c3922b
WS
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
0c65afdb
DS
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;
709d0081 102 m_numMarkedCells = 0;
0c65afdb
DS
103
104 return true;
105}
025e88c5 106
025e88c5 107void BombsGame::Mark(int x, int y)
0c65afdb
DS
108{
109 m_field[x+y*m_width] ^= BG_MARKED;
709d0081
WS
110 if (IsMarked(x, y))
111 m_numMarkedCells++;
112 else
113 m_numMarkedCells--;
0c65afdb 114}
025e88c5 115
709d0081 116void BombsGame::Unhide(int x, int y, bool b_selected)
0c65afdb
DS
117{
118 if (!IsHidden(x,y))
119 {
120 return;
121 }
122
709d0081
WS
123 if (b_selected)
124 m_field[x+y*m_width] |= BG_SELECTED;
125
0c65afdb
DS
126 m_field[x+y*m_width] &= ~BG_HIDDEN;
127
025e88c5 128 if (!IsBomb(x,y))
0c65afdb
DS
129 {
130 m_numRemainingCells--;
131 }
132}
025e88c5 133
025e88c5 134
0c65afdb
DS
135void BombsGame::Explode(int x, int y)
136{
137 m_field[x+y*m_width] |= BG_EXPLODED;
138}