1998-12-31 05:54:41 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: bombs1.cpp
|
|
|
|
// Purpose: Implementation of the class BombsGame
|
|
|
|
// Author: P. Foggia 1996
|
2005-05-10 15:22:06 -04:00
|
|
|
// Modified by: Wlodzimierz Skiba (ABX) since 2003
|
1998-12-31 05:54:41 -05:00
|
|
|
// Created: 1996
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1996 P. Foggia
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
#ifndef WX_PRECOMP
|
2003-12-18 20:34:40 -05:00
|
|
|
# include "wx/wx.h"
|
|
|
|
#endif
|
1998-12-31 05:54:41 -05:00
|
|
|
|
|
|
|
#include "game.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#define PROB 0.2
|
|
|
|
|
|
|
|
#ifndef RAND_MAX
|
2003-12-18 20:34:40 -05:00
|
|
|
# define RAND_MAX INT_MAX
|
1998-12-31 05:54:41 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
BombsGame::~BombsGame()
|
2003-12-18 20:34:40 -05:00
|
|
|
{
|
|
|
|
if (m_field)
|
|
|
|
{
|
|
|
|
delete[] m_field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the play field. Returns false on failure
|
2005-05-06 14:07:14 -04:00
|
|
|
bool BombsGame::Init(int aWidth, int aHeight, bool easyCorner)
|
2003-12-18 20:34:40 -05:00
|
|
|
{
|
|
|
|
m_gridFocusX = m_gridFocusY = -1;
|
|
|
|
|
|
|
|
int x, y;
|
1998-12-31 05:54:41 -05:00
|
|
|
int xx, yy;
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
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;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
}
|
|
|
|
|
2005-05-06 14:07:14 -04:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
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;
|
2005-05-16 11:32:04 -04:00
|
|
|
m_numMarkedCells = 0;
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
|
|
|
|
void BombsGame::Mark(int x, int y)
|
2003-12-18 20:34:40 -05:00
|
|
|
{
|
|
|
|
m_field[x+y*m_width] ^= BG_MARKED;
|
2005-05-16 11:32:04 -04:00
|
|
|
if (IsMarked(x, y))
|
|
|
|
m_numMarkedCells++;
|
|
|
|
else
|
|
|
|
m_numMarkedCells--;
|
2003-12-18 20:34:40 -05:00
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2005-05-16 11:32:04 -04:00
|
|
|
void BombsGame::Unhide(int x, int y, bool b_selected)
|
2003-12-18 20:34:40 -05:00
|
|
|
{
|
|
|
|
if (!IsHidden(x,y))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-05-16 11:32:04 -04:00
|
|
|
if (b_selected)
|
|
|
|
m_field[x+y*m_width] |= BG_SELECTED;
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
m_field[x+y*m_width] &= ~BG_HIDDEN;
|
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
if (!IsBomb(x,y))
|
2003-12-18 20:34:40 -05:00
|
|
|
{
|
|
|
|
m_numRemainingCells--;
|
|
|
|
}
|
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
void BombsGame::Explode(int x, int y)
|
|
|
|
{
|
|
|
|
m_field[x+y*m_width] |= BG_EXPLODED;
|
|
|
|
}
|