📄 sample_minesweeper.cpp
字号:
d_result->setVisible(false); boardReset(); boardPositionMines(); for (size_t i = 0 ; i < MinesweeperSize ; ++i) { for(size_t j = 0 ; j < MinesweeperSize ; ++j) { d_buttons[i][j]->setText(""); d_buttons[i][j]->setEnabled(true); } } d_counter->setText(CEGUI::PropertyHelper::uintToString(MineCount)); // Handle timer d_timerStartTime = ::clock(); d_timerValue = 0; d_timer->setText("0"); d_gameStarted = true; d_alarm->start(); return true;}/************************************************************************ 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::uintToString(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::uintToString(d_board[i][j])); } } d_buttons[i][j]->setEnabled(false); } } gameEnd(false); } return true; }/************************************************************************ Handle click on a mine button (any mouse button)************************************************************************/bool MinesweeperSample::handleMineButtonDown(const CEGUI::EventArgs& event){ const CEGUI::MouseEventArgs& me = static_cast<const CEGUI::MouseEventArgs&>(event); if (me.button == CEGUI::RightButton) { CEGUI::Window* button = me.window; if (!button->isDisabled()) { Location* buttonLoc = static_cast<Location*>(button->getUserData()); size_t x = buttonLoc->d_col; size_t y = buttonLoc->d_row; if (button->getID() == 0) { button->setID(1); button->setText("F"); } else { button->setID(0); button->setText(""); } return true; } } return false;}/*********************************************************************** Handle timer refresh ***********************************************************************/bool MinesweeperSample::handleUpdateTimer(const CEGUI::EventArgs& event){ if (d_gameStarted) { clock_t time = ::clock(); time -= d_timerStartTime; if (time != d_timerValue) { d_timer->setText(CEGUI::PropertyHelper::uintToString(time / CLOCKS_PER_SEC)); d_timerValue = time; } } return true;}/************************************************************************ Create the board ************************************************************************/void MinesweeperSample::boardReset(){ d_boardCellDiscovered = 0; for(size_t i = 0 ; i < MinesweeperSize ; ++i) { for(size_t j = 0 ; j < MinesweeperSize ; ++j) { d_board[i][j] = 0; } }}/************************************************************************ Position mine on the board ************************************************************************/void MinesweeperSample::boardPositionMines(){ size_t x = 0 ; size_t y = 0 ; ::srand(::clock()); for(size_t i = 0 ; i < MineCount ; ++i) { do { x = (size_t) ((float)MinesweeperSize * (::rand() / (RAND_MAX + 1.0))); y = (size_t) ((float)MinesweeperSize * (::rand() / (RAND_MAX + 1.0))); } while(d_board[x][y] > 8); d_board[x][y] += 10; if (x > 0) { if (y > 0) ++ d_board[x - 1][y - 1]; ++ d_board[x - 1][y ]; if (y < MinesweeperSize - 1) ++ d_board[x - 1][y + 1]; } if (y > 0) ++ d_board[x ][y - 1]; if (y < MinesweeperSize - 1) ++ d_board[x ][y + 1]; if (x < MinesweeperSize - 1) { if (y > 0) ++ d_board[x + 1][y - 1]; ++ d_board[x + 1][y ]; if (y < MinesweeperSize - 1) ++ d_board[x + 1][y + 1]; } }}/************************************************************************ Check wether the game is won or not ************************************************************************/bool MinesweeperSample::isGameWin(){ return d_boardCellDiscovered + MineCount == MinesweeperSize * MinesweeperSize;}void MinesweeperSample::gameEnd(bool victory){ d_gameStarted = false; d_alarm->stop(); CEGUI::String message; if (victory) { message = CEGUI::String((CEGUI::utf8*)"You win"); } else { message = CEGUI::String((CEGUI::utf8*)"You lose"); } // Display a message to the user d_result->setText(message); d_result->setVisible(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::uintToString(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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -