Skip to content

Commit

Permalink
重覆利用guiBoard
Browse files Browse the repository at this point in the history
避免不必要的delete
  • Loading branch information
jeang-bo-yuan committed Apr 26, 2023
1 parent 0818419 commit 3de9519
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
29 changes: 10 additions & 19 deletions minesweeper/GUIGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <QLabel>
#include <QMessageBox>
#include <QFont>
#include <iostream>
#include <string>
#include <cstring>

Expand Down Expand Up @@ -218,10 +217,8 @@ void StandbyWidget::replay() {
// PlayingWidget的實作 //////////////////////////////////////////////////////////////////////////////////////////////

PlayingWidget::PlayingWidget(std::shared_ptr<GameBoard> p, QWidget* parent)
: GeneralGameWidget(p, "Playing", parent), guiBoard(nullptr)
: GeneralGameWidget(p, "Playing", parent), guiBoard(new QGridLayout)
{
vLayout->addStretch(1);

// infobox
{
infoBox->addWidget(new QLabel("Bomb Count : "), 1, 0);
Expand All @@ -245,14 +242,17 @@ PlayingWidget::PlayingWidget(std::shared_ptr<GameBoard> p, QWidget* parent)
infoBox->addWidget(remainBlank, 2, 3);
}

// GUI Minesweeper Board
vLayout->addStretch(1);
vLayout->addLayout(guiBoard);
vLayout->addStretch(1);
}

void PlayingWidget::initGameBoard() {
updateInfoBox();

guiBoard = new QGridLayout;
for (size_t r = 0; r < board_p->rowSize(); ++r) {
for (size_t c = 0; c < board_p->colSize(); ++c) {
for (unsigned r = 0; r < board_p->rowSize(); ++r) {
for (unsigned c = 0; c < board_p->colSize(); ++c) {
MineButton* button = new MineButton((int)r, (int)c);
QObject::connect(button, &MineButton::leftClicked, this, &PlayingWidget::openBlock);
QObject::connect(button, &MineButton::rightClicked, this, &PlayingWidget::flagBlock);
Expand All @@ -264,9 +264,6 @@ void PlayingWidget::initGameBoard() {
guiBoard->setContentsMargins(0, 0, 0, 0);
guiBoard->setAlignment(Qt::AlignCenter);

vLayout->addLayout(guiBoard);
vLayout->addStretch(1);

this->show();
}

Expand Down Expand Up @@ -361,22 +358,16 @@ void PlayingWidget::checkIfGameOver() {
for (int r = 0; r < (int)board_p->rowSize(); ++r) {
for (int c = 0; c < (int)board_p->colSize(); ++c) {
MineButton* button = qobject_cast<MineButton*>(guiBoard->itemAtPosition(r, c)->widget());
guiBoard->removeWidget(button);
delete button;
}
}

QLayoutItem* stretchToDelete = vLayout->itemAt(vLayout->count() - 1);
vLayout->removeItem(guiBoard);
vLayout->removeItem(stretchToDelete); // 刪掉最下面的stretch(在initGameBoard被加入)
delete guiBoard;
guiBoard = nullptr;
delete stretchToDelete;

std::cout << "<Replay> : Success" << std::endl;
printCommandSuccessOrNot("Replay", true);
emit replay();
}
else {
std::cout << "<Quit> : Success" << std::endl;
printCommandSuccessOrNot("Quit", true);
qApp->closeAllWindows();
}
}
11 changes: 10 additions & 1 deletion minesweeper/GUIGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,17 @@ class PlayingWidget : public GeneralGameWidget {
Q_OBJECT

//! 包含按鈕
QGridLayout* guiBoard;
QGridLayout* const guiBoard;

/**
* @brief check if game is over
* @post 若遊戲結束,顯示對話框詢問是否要重新開始(Replay)。
* - Yes ->
* 1. 將 PlayingWidget::guiBoard上原本的按鈕清掉
* 2. emit replay();
* - No ->
* 結束程式
*/
void checkIfGameOver();

public:
Expand Down
Binary file modified minesweeper/doc/Document.chm
Binary file not shown.

0 comments on commit 3de9519

Please sign in to comment.