/************************************************************************ Handle click on a mine button ************************************************************************/ bool MinesweeperSample::handleMineButtonClicked(const CEGUI::EventArgs& event) { const CEGUI::WindowEventArgs* evt = static_cast<const CEGUI::WindowEventArgs*>(&event); CEGUI::PushButton* button = static_cast<CEGUI::PushButton*>(evt->window); Location* buttonLoc = static_cast<Location*>(button->getUserData()); if (button->getID() > 0) { // dont touch flagged buttons return true; } if (boardDiscover(*buttonLoc)) { // We did not find a mine button->setText(CEGUI::PropertyHelper<CEGUI::uint>::toString(d_board[buttonLoc->d_row][buttonLoc->d_col])); if (isGameWin()) gameEnd(true); } else { for(size_t i = 0 ; i < MinesweeperSize ; ++i) { for (size_t j = 0 ; j < MinesweeperSize ; ++j) { if (! d_buttons[i][j]->isDisabled()) { if (d_board[i][j] > 8) { d_buttons[i][j]->setText("B"); d_buttons[i][j]->setProperty("DisabledTextColour", "FFFF1010"); } else { d_buttons[i][j]->setText(CEGUI::PropertyHelper<CEGUI::uint>::toString(d_board[i][j])); } } d_buttons[i][j]->setEnabled(false); } } gameEnd(false); } return true; }
bool MinesweeperSample::boardDiscover(const Location& loc) { CEGUI::PushButton* btn = d_buttons[loc.d_row][loc.d_col]; if (btn->isDisabled() || btn->getID() > 0) return true; if (d_board[loc.d_row][loc.d_col] > 8) return false; d_buttons[loc.d_row][loc.d_col]->setText(CEGUI::PropertyHelper<CEGUI::uint>::toString(d_board[loc.d_row][loc.d_col])); d_buttons[loc.d_row][loc.d_col]->setEnabled(false); ++d_boardCellDiscovered; // Discover surrounding case if (d_board[loc.d_row][loc.d_col] == 0) { Location l; if (loc.d_row > 0) { l.d_row = loc.d_row - 1; if ( loc.d_col > 0) { l.d_col = loc.d_col - 1; boardDiscover(l); } l.d_col = loc.d_col; boardDiscover(l); if ( loc.d_col < MinesweeperSize - 1) { l.d_col = loc.d_col + 1; boardDiscover(l); } } l.d_row = loc.d_row; if ( loc.d_col > 0) { l.d_col = loc.d_col - 1; boardDiscover(l); } if ( loc.d_col < MinesweeperSize - 1) { l.d_col = loc.d_col + 1; boardDiscover(l); } if (loc.d_row < MinesweeperSize - 1) { l.d_row = loc.d_row + 1; if ( loc.d_col > 0) { l.d_col = loc.d_col - 1; boardDiscover(l); } l.d_col = loc.d_col; boardDiscover(l); if ( loc.d_col < MinesweeperSize - 1) { l.d_col = loc.d_col + 1; boardDiscover(l); } } } return true; }