]> git.saurik.com Git - wxWidgets.git/blame - demos/bombs/game.cpp
Spelling fixes [#1017001], source cleaning.
[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
0c65afdb 5// Modified by: Wlodzimierz Skiba (ABX) 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
46bool BombsGame::Init(int aWidth, int aHeight)
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
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}
025e88c5 96
025e88c5 97void BombsGame::Mark(int x, int y)
0c65afdb
DS
98{
99 m_field[x+y*m_width] ^= BG_MARKED;
100}
025e88c5 101
025e88c5 102void BombsGame::Unhide(int x, int y)
0c65afdb
DS
103{
104 if (!IsHidden(x,y))
105 {
106 return;
107 }
108
109 m_field[x+y*m_width] &= ~BG_HIDDEN;
110
025e88c5 111 if (!IsBomb(x,y))
0c65afdb
DS
112 {
113 m_numRemainingCells--;
114 }
115}
025e88c5 116
025e88c5 117
0c65afdb
DS
118void BombsGame::Explode(int x, int y)
119{
120 m_field[x+y*m_width] |= BG_EXPLODED;
121}