1998-12-31 05:54:41 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: bombs1.cpp
|
|
|
|
// Purpose: Bombs game
|
|
|
|
// 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
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*
|
|
|
|
* implementation of the methods DrawField and OnEvent of the
|
|
|
|
* class BombsCanvas
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
1998-12-31 05:54:41 -05:00
|
|
|
#endif //precompiled headers
|
|
|
|
|
|
|
|
#include "bombs.h"
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
// Draws the field on the device context dc
|
|
|
|
// xc1,yc1 etc. are the (inclusive) limits of the area to be drawn,
|
|
|
|
// expressed in cells.
|
|
|
|
void BombsCanvas::DrawField(wxDC *dc, int xc1, int yc1, int xc2, int yc2)
|
|
|
|
{
|
|
|
|
wxString buf;
|
|
|
|
long chw, chh;
|
|
|
|
|
2005-05-16 11:32:04 -04:00
|
|
|
wxColour wxYellow = wxTheColourDatabase->Find(wxT("YELLOW"));
|
2003-12-18 20:34:40 -05:00
|
|
|
wxColour wxFocused = wxTheColourDatabase->Find(wxT("GREY"));
|
|
|
|
|
2005-05-16 11:32:04 -04:00
|
|
|
wxPen *bluePen = wxThePenList->FindOrCreatePen(*wxBLUE, 1, wxSOLID);
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
wxBrush *focusedBrush = wxTheBrushList->FindOrCreateBrush(wxFocused, wxSOLID);
|
2005-05-16 11:32:04 -04:00
|
|
|
wxBrush *yellowBrush = wxTheBrushList->FindOrCreateBrush(wxYellow, wxSOLID);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
int x, y;
|
|
|
|
int xMax = this->GetGridSizeInPixels().GetWidth();
|
|
|
|
int yMax = this->GetGridSizeInPixels().GetHeight();
|
|
|
|
for(x=xc1; x<=xc2; x++)
|
|
|
|
dc->DrawLine(x*m_cellWidth*X_UNIT, 0, x*m_cellWidth*X_UNIT, yMax);
|
|
|
|
for(y=xc1; y<=yc2; y++)
|
|
|
|
dc->DrawLine(0, y*m_cellHeight*Y_UNIT, xMax, y*m_cellHeight*Y_UNIT);
|
|
|
|
|
|
|
|
|
|
|
|
wxFont font= BOMBS_FONT;
|
2005-01-31 13:02:36 -05:00
|
|
|
dc->SetFont(font);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
for(x=xc1; x<=xc2; x++)
|
|
|
|
for(y=yc1; y<=yc2; y++)
|
|
|
|
{
|
|
|
|
if (m_game->IsMarked(x,y))
|
|
|
|
{
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
if (m_game->IsFocussed(x, y))
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetBrush(*focusedBrush);
|
2003-12-18 20:34:40 -05:00
|
|
|
else
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetBrush(*wxLIGHT_GREY_BRUSH);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT,
|
|
|
|
m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1);
|
|
|
|
buf = wxT("M");
|
|
|
|
if (!m_game->IsHidden(x,y) && m_game->IsBomb(x,y))
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextForeground(*wxBLUE);
|
2003-12-18 20:34:40 -05:00
|
|
|
else
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextForeground(*wxRED);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextBackground(*wxLIGHT_GREY);
|
2003-12-18 20:34:40 -05:00
|
|
|
dc->GetTextExtent(buf, &chw, &chh);
|
|
|
|
dc->DrawText( buf,
|
|
|
|
x*m_cellWidth*X_UNIT + (m_cellWidth*X_UNIT-chw)/2,
|
|
|
|
y*m_cellHeight*Y_UNIT + (m_cellHeight*Y_UNIT-chh)/2 );
|
|
|
|
|
|
|
|
if (!m_game->IsHidden(x,y) && m_game->IsBomb(x,y))
|
|
|
|
{
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetPen(*wxRED_PEN);
|
2003-12-18 20:34:40 -05:00
|
|
|
dc->DrawLine(x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT,
|
|
|
|
(x+1)*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT);
|
|
|
|
dc->DrawLine(x*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT,
|
|
|
|
(x+1)*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_game->IsHidden(x,y))
|
|
|
|
{
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
2003-12-18 20:34:40 -05:00
|
|
|
if (m_game->IsFocussed(x, y))
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetBrush(*focusedBrush);
|
2003-12-18 20:34:40 -05:00
|
|
|
else
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetBrush(*wxLIGHT_GREY_BRUSH);
|
2003-12-18 20:34:40 -05:00
|
|
|
|
|
|
|
dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT,
|
|
|
|
m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1);
|
|
|
|
}
|
|
|
|
else if (m_game->IsBomb(x,y))
|
|
|
|
{
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
|
|
|
dc->SetBrush(*wxRED_BRUSH);
|
2003-12-18 20:34:40 -05:00
|
|
|
dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT,
|
|
|
|
m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1);
|
|
|
|
buf = wxT("B");
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextForeground(*wxBLACK);
|
|
|
|
dc->SetTextBackground(*wxRED);
|
2003-12-18 20:34:40 -05:00
|
|
|
dc->GetTextExtent(buf, &chw, &chh);
|
|
|
|
dc->DrawText( buf,
|
|
|
|
x*m_cellWidth*X_UNIT + (m_cellWidth*X_UNIT-chw)/2,
|
|
|
|
y*m_cellHeight*Y_UNIT + (m_cellHeight*Y_UNIT-chh)/2);
|
|
|
|
if (m_game->IsExploded(x,y))
|
|
|
|
{
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetPen(*bluePen);
|
2003-12-18 20:34:40 -05:00
|
|
|
dc->DrawLine(x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT,
|
|
|
|
(x+1)*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT);
|
|
|
|
dc->DrawLine(x*m_cellWidth*X_UNIT, (y+1)*m_cellHeight*Y_UNIT,
|
|
|
|
(x+1)*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Display a digit
|
|
|
|
{
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetPen(*wxBLACK_PEN);
|
2003-12-18 20:34:40 -05:00
|
|
|
if (m_game->IsFocussed(x, y))
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetBrush(*focusedBrush);
|
|
|
|
else if (m_game->IsSelected(x,y))
|
|
|
|
dc->SetBrush(*wxWHITE_BRUSH);
|
|
|
|
else
|
|
|
|
dc->SetBrush(*yellowBrush);
|
2003-12-18 20:34:40 -05:00
|
|
|
dc->DrawRectangle( x*m_cellWidth*X_UNIT, y*m_cellHeight*Y_UNIT,
|
|
|
|
m_cellWidth*X_UNIT+1, m_cellHeight*Y_UNIT+1);
|
|
|
|
|
|
|
|
int digit_value = m_game->Get(x,y) & BG_MASK;
|
|
|
|
switch(digit_value)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
buf = wxT("0");
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextForeground(*wxGREEN);
|
2003-12-18 20:34:40 -05:00
|
|
|
break;
|
2005-01-31 13:02:36 -05:00
|
|
|
case 1:
|
2003-12-18 20:34:40 -05:00
|
|
|
buf = wxT("1");
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextForeground(*wxBLUE);
|
2003-12-18 20:34:40 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buf.Printf(wxT("%d"),digit_value);
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextForeground(*wxBLACK);
|
2003-12-18 20:34:40 -05:00
|
|
|
break;
|
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
dc->GetTextExtent(buf, &chw, &chh);
|
2005-05-16 11:32:04 -04:00
|
|
|
dc->SetTextBackground(*wxWHITE);
|
1998-12-31 05:54:41 -05:00
|
|
|
dc->DrawText( buf,
|
2003-12-18 20:34:40 -05:00
|
|
|
x*m_cellWidth*X_UNIT + (m_cellWidth*X_UNIT-chw)/2,
|
|
|
|
y*m_cellHeight*Y_UNIT + (m_cellHeight*Y_UNIT-chh)/2);
|
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
dc->SetFont(wxNullFont);
|
|
|
|
|
2004-07-20 15:17:52 -04:00
|
|
|
wxString msg;
|
2005-05-16 11:32:04 -04:00
|
|
|
msg.Printf(wxT("%d bombs, %u marked, %d remaining cells"),
|
|
|
|
m_game->GetNumBombs(), m_game->GetNumMarkedCells(),
|
|
|
|
m_game->GetNumRemainingCells() );
|
2004-07-20 15:17:52 -04:00
|
|
|
|
|
|
|
#if wxUSE_LOG && wxUSE_STATUSBAR
|
|
|
|
wxLogStatus(msg);
|
|
|
|
#else
|
|
|
|
this->GetParent()->SetTitle(msg);
|
2003-12-18 20:34:40 -05:00
|
|
|
#endif
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
// Refreshes the field image
|
|
|
|
// xc1,yc1 etc. are the (inclusive) limits of the area to be drawn,
|
|
|
|
// expressed in cells.
|
|
|
|
void BombsCanvas::RefreshField(int xc1, int yc1, int xc2, int yc2)
|
|
|
|
{
|
1998-12-31 05:54:41 -05:00
|
|
|
wxClientDC dc(this);
|
|
|
|
DrawField(& dc, xc1, yc1, xc2, yc2);
|
2003-12-18 20:34:40 -05:00
|
|
|
if (m_bmp)
|
|
|
|
{
|
|
|
|
wxMemoryDC memDC;
|
|
|
|
memDC.SelectObject(*m_bmp);
|
1998-12-31 05:54:41 -05:00
|
|
|
DrawField(&memDC, xc1, yc1, xc2, yc2);
|
|
|
|
memDC.SelectObject(wxNullBitmap);
|
2003-12-18 20:34:40 -05:00
|
|
|
}
|
|
|
|
}
|
1998-12-31 05:54:41 -05:00
|
|
|
|
2000-10-30 12:06:43 -05:00
|
|
|
// Called when uncovering a cell.
|
2003-12-18 20:34:40 -05:00
|
|
|
void BombsCanvas::Uncover(int x, int y)
|
2000-10-30 12:06:43 -05:00
|
|
|
{
|
2005-05-16 11:32:04 -04:00
|
|
|
m_game->Unhide(x,y,true);
|
2003-12-18 20:34:40 -05:00
|
|
|
RefreshField(x, y, x, y);
|
|
|
|
|
|
|
|
const int gridWidth = m_game->GetWidth();
|
|
|
|
const int gridHeight = m_game->GetHeight();
|
|
|
|
|
|
|
|
const bool hasWon = m_game->GetNumRemainingCells() == 0;
|
|
|
|
if (m_game->IsBomb(x,y) || hasWon)
|
|
|
|
{
|
|
|
|
wxBell();
|
|
|
|
if (hasWon)
|
|
|
|
{
|
|
|
|
wxMessageBox(wxT("Nice! You found all the bombs!"),
|
|
|
|
wxT("wxWin Bombs"), wxOK|wxCENTRE);
|
|
|
|
}
|
|
|
|
else // x,y is a bomb
|
|
|
|
{
|
|
|
|
m_game->Explode(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(x=0; x<gridWidth; x++)
|
|
|
|
for(y=0; y<gridHeight; y++)
|
2005-05-16 11:32:04 -04:00
|
|
|
m_game->Unhide(x,y,false);
|
|
|
|
|
|
|
|
RefreshField(0, 0, gridWidth-1, gridHeight-1);
|
2000-10-30 12:06:43 -05:00
|
|
|
}
|
2005-05-16 11:32:04 -04:00
|
|
|
else if (0 == (m_game->Get(x, y) & BG_MASK))
|
2003-12-18 20:34:40 -05:00
|
|
|
{
|
|
|
|
int left = ( x > 0 ) ? x-1 : 0;
|
|
|
|
int right = ( x < gridWidth - 1 )
|
|
|
|
? x+1
|
|
|
|
: gridWidth - 1;
|
|
|
|
int top = ( y > 0 ) ? y-1 : 0;
|
|
|
|
int bottom = ( y < gridHeight - 1 )
|
|
|
|
? y+1
|
|
|
|
: gridHeight - 1;
|
|
|
|
|
|
|
|
int i, j;
|
|
|
|
for (j=top; j<=bottom; j++)
|
|
|
|
for (i=left; i<=right; i++)
|
|
|
|
if ( (i != x || j != y) && m_game->IsHidden(i, j)
|
|
|
|
&& !m_game->IsMarked(i, j) )
|
|
|
|
{
|
|
|
|
Uncover(i, j);
|
|
|
|
}
|
2000-10-30 12:06:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-31 05:54:41 -05:00
|
|
|
// Called when the canvas receives a mouse event.
|
2003-12-18 20:34:40 -05:00
|
|
|
void BombsCanvas::OnMouseEvent(wxMouseEvent& event)
|
1999-02-05 11:44:52 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
const int gridWidth = m_game->GetWidth();
|
|
|
|
const int gridHeight = m_game->GetHeight();
|
|
|
|
|
|
|
|
wxCoord fx, fy;
|
|
|
|
event.GetPosition(&fx, &fy);
|
|
|
|
int x = fx/(m_cellWidth*X_UNIT);
|
|
|
|
int y = fy/(m_cellHeight*Y_UNIT);
|
|
|
|
if (x<gridWidth && y<gridHeight)
|
|
|
|
{
|
|
|
|
if ( (event.RightDown() || (event.LeftDown() && event.ShiftDown()))
|
|
|
|
&& (m_game->IsHidden(x,y)
|
|
|
|
|| !m_game->GetNumRemainingCells() ) )
|
2005-01-31 13:02:36 -05:00
|
|
|
{
|
2003-12-18 20:34:40 -05:00
|
|
|
// store previous and current field
|
|
|
|
int prevFocusX = m_game->m_gridFocusX;
|
|
|
|
int prevFocusY = m_game->m_gridFocusY;
|
|
|
|
m_game->m_gridFocusX = x;
|
|
|
|
m_game->m_gridFocusY = y;
|
|
|
|
RefreshField(prevFocusX, prevFocusY, prevFocusX, prevFocusY);
|
|
|
|
m_game->Mark(x, y);
|
|
|
|
RefreshField(x, y, x, y);
|
|
|
|
return;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
2003-12-18 20:34:40 -05:00
|
|
|
else if (event.LeftDown() && m_game->IsHidden(x,y)
|
|
|
|
&& !m_game->IsMarked(x,y))
|
|
|
|
{
|
|
|
|
// store previous and current field
|
|
|
|
int prevGridFocusX = m_game->m_gridFocusX;
|
|
|
|
int prevGridFocusY = m_game->m_gridFocusY;
|
|
|
|
m_game->m_gridFocusX = x;
|
|
|
|
m_game->m_gridFocusY = y;
|
|
|
|
RefreshField(prevGridFocusX, prevGridFocusY,
|
|
|
|
prevGridFocusX, prevGridFocusY);
|
|
|
|
Uncover(x, y);
|
|
|
|
return;
|
1998-12-31 05:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
void BombsCanvas::OnChar(wxKeyEvent& event)
|
|
|
|
{
|
|
|
|
int keyCode = event.GetKeyCode();
|
|
|
|
int prevGridFocusX = m_game->m_gridFocusX;
|
|
|
|
int prevGridFocusY = m_game->m_gridFocusY;
|
|
|
|
|
|
|
|
const int gridWidth = m_game->GetWidth();
|
|
|
|
const int gridHeight = m_game->GetHeight();
|
|
|
|
|
|
|
|
switch(keyCode)
|
|
|
|
{
|
|
|
|
|
|
|
|
case WXK_RIGHT:
|
|
|
|
m_game->m_gridFocusX++;
|
|
|
|
if (m_game->m_gridFocusX >= gridWidth) m_game->m_gridFocusX = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WXK_LEFT:
|
|
|
|
m_game->m_gridFocusX--;
|
|
|
|
if (m_game->m_gridFocusX<0) m_game->m_gridFocusX = gridWidth-1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WXK_DOWN:
|
|
|
|
m_game->m_gridFocusY++;
|
|
|
|
if (m_game->m_gridFocusY >= gridHeight) m_game->m_gridFocusY = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WXK_UP:
|
|
|
|
m_game->m_gridFocusY--;
|
|
|
|
if (m_game->m_gridFocusY<0) m_game->m_gridFocusY = gridHeight-1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WXK_RETURN:
|
|
|
|
if ( (prevGridFocusX == m_game->m_gridFocusX)
|
|
|
|
&& (prevGridFocusY == m_game->m_gridFocusY)
|
|
|
|
&& (m_game->IsHidden(m_game->m_gridFocusX, m_game->m_gridFocusY)) )
|
|
|
|
{
|
|
|
|
m_game->Mark(m_game->m_gridFocusX, m_game->m_gridFocusY);
|
|
|
|
if (!m_game->IsMarked(m_game->m_gridFocusX, m_game->m_gridFocusY))
|
|
|
|
{
|
|
|
|
Uncover(m_game->m_gridFocusX, m_game->m_gridFocusY);
|
|
|
|
}
|
|
|
|
RefreshField(m_game->m_gridFocusX, m_game->m_gridFocusY,
|
|
|
|
m_game->m_gridFocusX, m_game->m_gridFocusY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
event.Skip();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((prevGridFocusX != m_game->m_gridFocusX)
|
|
|
|
|| (prevGridFocusY != m_game->m_gridFocusY))
|
|
|
|
{
|
2003-12-23 05:45:28 -05:00
|
|
|
// cause focused field to be visible after first key hit after launching new game
|
|
|
|
if( m_game->m_gridFocusX < 0 ) m_game->m_gridFocusX = 0;
|
|
|
|
if( m_game->m_gridFocusY < 0 ) m_game->m_gridFocusY = 0;
|
|
|
|
|
2003-12-18 20:34:40 -05:00
|
|
|
// refresh previous field and focused field
|
|
|
|
RefreshField(prevGridFocusX, prevGridFocusY,
|
|
|
|
prevGridFocusX, prevGridFocusY);
|
|
|
|
RefreshField(m_game->m_gridFocusX, m_game->m_gridFocusY,
|
|
|
|
m_game->m_gridFocusX, m_game->m_gridFocusY);
|
|
|
|
}
|
|
|
|
}
|