1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
///////////////////////////////////////////////////////////////////////////////
// Name: bombs1.cpp
// Purpose: Implementation of the class BombsGame
// Author: P. Foggia 1996
// Modified by: Wlodzimierz Skiba (ABX) since 2003
// Created: 1996
// Copyright: (c) 1996 P. Foggia
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
#include "game.h"
#include <stdlib.h>
#include <limits.h>
#define PROB 0.2
#ifndef RAND_MAX
# define RAND_MAX INT_MAX
#endif
BombsGame::~BombsGame()
{
if (m_field)
{
delete[] m_field;
}
}
// Initialize the play field. Returns false on failure
bool BombsGame::Init(int aWidth, int aHeight, bool easyCorner)
{
m_gridFocusX = m_gridFocusY = -1;
int x, y;
int xx, yy;
if (m_field)
{
delete[] m_field;
}
m_field = new short[aWidth*aHeight];
if (!m_field)
{
m_width = m_height = 0;
return false;
}
m_width = aWidth;
m_height = aHeight;
for(x=0; x<m_width; x++)
{
for(y=0; y<m_height; y++)
{
m_field[x+y*m_width] = ((float)rand()/RAND_MAX <PROB)
? BG_HIDDEN | BG_BOMB
: BG_HIDDEN;
}
}
/* Force (0,0) not to have a bomb for those that don't want to have
to guess on the first move. Better would be to for the MS rule that
whatever is picked first isn't a bomb.
*/
if(easyCorner)
{
m_field[0] = BG_HIDDEN;
}
m_numBombCells = 0;
for(x=0; x<m_width; x++)
for(y=0; y<m_height; y++)
if (m_field[x+y*m_width] & BG_BOMB)
{
m_numBombCells++;
for(xx=x-1; xx<=x+1; xx++)
if (xx>=0 && xx<m_width)
for(yy=y-1; yy<=y+1; yy++)
if (yy>=0 && yy<m_height && (yy!=y || xx!=x))
m_field[xx+yy*m_width]++;
}
m_numRemainingCells = m_height*m_width-m_numBombCells;
m_numMarkedCells = 0;
return true;
}
void BombsGame::Mark(int x, int y)
{
m_field[x+y*m_width] ^= BG_MARKED;
if (IsMarked(x, y))
m_numMarkedCells++;
else
m_numMarkedCells--;
}
void BombsGame::Unhide(int x, int y, bool b_selected)
{
if (!IsHidden(x,y))
{
return;
}
if (b_selected)
m_field[x+y*m_width] |= BG_SELECTED;
m_field[x+y*m_width] &= ~BG_HIDDEN;
if (!IsBomb(x,y))
{
m_numRemainingCells--;
}
}
void BombsGame::Explode(int x, int y)
{
m_field[x+y*m_width] |= BG_EXPLODED;
}
|