Skip to content

Commit 9734b4d

Browse files
Merge pull request #26 from DeepCodingInTuringAcademy/release-1.0.1
Release 1.0.1
2 parents e61d858 + 0c45cce commit 9734b4d

File tree

4 files changed

+60
-63
lines changed

4 files changed

+60
-63
lines changed

include/game.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Game
3333

3434
public:
3535
// 十七
36-
Game(int, int, int);
36+
Game(int width, int height, int mineCount);
3737
static std::string version();
3838
void reset(); /// 重置游戏数据
3939
[[nodiscard]] bool Over() const; /// 游戏是否结束

include/gameboard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class GameBoard
1919
int width_; ///< 地图宽度
2020
int height_; ///< 地图高度
2121
int mineCount_; ///< 地雷总数
22+
int mineRemainsCount_;
2223
std::vector<std::vector<Cell>> board_; ///< 地图格子
2324

2425
// dearling
@@ -49,14 +50,13 @@ class GameBoard
4950
[[nodiscard]] bool cellIsRevealed(int x, int y) const;
5051
// 各自是否被插旗
5152
[[nodiscard]] bool cellIsFlagged(int x, int y) const;
52-
// 获取格子周围地雷数
53-
[[nodiscard]] int getAdjacentMines(int x, int y) const;
5453
// 地图尺寸
5554
[[nodiscard]] int getWidth() const;
5655
[[nodiscard]] int getHeight() const;
5756

5857
// 获取地图所有地雷数
5958
[[nodiscard]] int getMineCount() const;
59+
[[nodiscard]] int getMinesRemainCount() const;
6060
};
6161

6262
#endif // GAMEBOARD_H

lib/game.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ void Game::handleInput(const std::string &input)
5555
{
5656
board_.generateMines(cur_x, cur_y); // 先埋雷
5757
first_ = false;
58-
board_.revealCell(cur_x, cur_y);
5958
}
6059
processReveal(cur_x, cur_y);
6160
}
@@ -131,13 +130,9 @@ void Game::processFlag(const int x, const int y)
131130
board_.display();
132131
}
133132

134-
Game::Game(const int width, const int height, const int mineCount) : board_(width, height, mineCount)
135-
{
136-
gameOver_ = false;
137-
win_ = false;
138-
begin_ = false;
139-
first_ = true;
140-
}
133+
Game::Game(const int width, const int height, const int mineCount)
134+
: board_(width, height, mineCount), gameOver_(false),
135+
win_(false), begin_(false), first_(true) {}
141136

142137
std::string Game::version()
143138
{
@@ -166,6 +161,7 @@ void Game::run()
166161
GameUI::printMessage(usageInfo());
167162
error.clear();
168163
}
164+
GameUI::printMessage("Remaining mines: " + std::to_string(board_.getMinesRemainCount()));
169165
GameUI::promptInput(); // 提示用户输入
170166

171167
std::string input;

lib/gameboard.cpp

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,79 +83,81 @@ bool GameBoard::inBounds(const int x, const int y) const
8383
}
8484

8585
GameBoard::GameBoard(const int width, const int height, const int mineCount)
86-
{
87-
this->width_ = width;
88-
this->height_ = height;
89-
this->mineCount_ = mineCount;
90-
this->board_ = std::vector(height_, std::vector(width_, Cell()));
91-
}
86+
: width_(width), height_(height),
87+
mineCount_(mineCount), mineRemainsCount_(mineCount),
88+
board_(height_, std::vector(width_, Cell())) {}
9289

9390
void GameBoard::revealCell(const int x, const int y)
9491
{
95-
if (this->board_[x][y].isRevealed())
96-
{
92+
if (this->board_[x][y].isRevealed())
93+
{
9794
return;
98-
}
99-
this->board_[x][y].reveal();
95+
}
96+
this->board_[x][y].reveal();
10097
}
10198

10299
void GameBoard::toggleFlag(const int x, const int y)
103100
{
104-
this->board_[x][y].toggleFlag();
101+
if (this->board_[x][y].isFlagged()){
102+
this->mineRemainsCount_ += 1;
103+
} else {
104+
this->mineRemainsCount_ -= 1;
105+
}
106+
this->board_[x][y].toggleFlag();
105107
}
106108

107109
bool GameBoard::isGameWon() const
108110
{
109-
for (const auto &row : board_)
110-
{
111+
for (const auto &row : board_)
112+
{
111113
for (const auto &cell : row)
112114
{
113115
if (!cell.hasMine() && !cell.isRevealed())
114116
{
115117
return false; // 非地雷格子没翻开,未胜利
116118
}
117119
}
118-
}
119-
return true; // 所有非雷格子都翻开了,胜利
120+
}
121+
return true; // 所有非雷格子都翻开了,胜利
120122
}
121123

122124
bool GameBoard::isGameLose() const
123125
{
124-
for (auto &row : this->board_)
125-
{
126+
for (auto &row : this->board_)
127+
{
126128
for (auto cell : row)
127129
{
128130
if (cell.isRevealed() && cell.hasMine())
129131
{
130132
return true;
131133
}
132134
}
133-
}
134-
return false;
135+
}
136+
return false;
135137
}
136138

137139
void GameBoard::display(const bool revealAll) const
138140
{
139-
const int height = this->getHeight();
140-
const int width = this->getWidth();
141+
const int height = this->getHeight();
142+
const int width = this->getWidth();
141143

142-
// 打印列号
143-
std::cout << " "; // 行号空白占位
144-
for (int col = 0; col < width; ++col)
145-
{
144+
// 打印列号
145+
std::cout << " "; // 行号空白占位
146+
for (int col = 0; col < width; ++col)
147+
{
146148
std::cout << std::setw(3) << col;
147-
}
148-
std::cout << std::endl;
149+
}
150+
std::cout << std::endl;
149151

150-
std::cout << " +";
151-
for (int col = 0; col < width; ++col)
152-
{
152+
std::cout << " +";
153+
for (int col = 0; col < width; ++col)
154+
{
153155
std::cout << "---";
154-
}
155-
std::cout << "+" << std::endl;
156+
}
157+
std::cout << "+" << std::endl;
156158

157-
for (int row = 0; row < height; ++row)
158-
{
159+
for (int row = 0; row < height; ++row)
160+
{
159161
std::cout << std::setw(3) << row << "|";
160162
for (int col = 0; col < width; ++col)
161163
{
@@ -180,53 +182,52 @@ void GameBoard::display(const bool revealAll) const
180182
}
181183
}
182184
std::cout << "|" << std::endl;
183-
}
185+
}
184186

185-
std::cout << " +";
186-
for (int col = 0; col < width; ++col)
187-
{
187+
std::cout << " +";
188+
for (int col = 0; col < width; ++col)
189+
{
188190
std::cout << "---";
189-
}
190-
std::cout << "+" << std::endl;
191+
}
192+
std::cout << "+" << std::endl;
191193
}
192194

193195
bool GameBoard::cellHasMine(const int x, const int y) const
194196
{
195-
return board_[x][y].hasMine();
197+
return board_[x][y].hasMine();
196198
}
197199

198200
bool GameBoard::cellHasAdjacentMines(const int x, const int y) const
199201
{
200-
return board_[x][y].hasAdjacentMines();
202+
return board_[x][y].hasAdjacentMines();
201203
}
202204

203205
// Check if the cell is revealed
204206
bool GameBoard::cellIsRevealed(const int x, const int y) const
205207
{
206-
return board_[x][y].isRevealed(); // Return if the cell is revealed
208+
return board_[x][y].isRevealed(); // Return if the cell is revealed
207209
}
208210

209211
bool GameBoard::cellIsFlagged(const int x, const int y) const
210212
{
211-
return board_[x][y].isFlagged();
212-
}
213-
// Get the number of adjacent mines to the cell
214-
int GameBoard::getAdjacentMines(const int x, const int y) const
215-
{
216-
return board_[x][y].getAdjacentMines(); // Return the number of adjacent mines
213+
return board_[x][y].isFlagged();
217214
}
218215

219216
int GameBoard::getWidth() const
220217
{
221-
return this->width_;
218+
return this->width_;
222219
}
223220

224221
int GameBoard::getHeight() const
225222
{
226-
return this->height_;
223+
return this->height_;
227224
}
228225

229226
int GameBoard::getMineCount() const
230227
{
231-
return this->mineCount_;
228+
return this->mineCount_;
229+
}
230+
231+
int GameBoard::getMinesRemainCount() const {
232+
return this->mineRemainsCount_;
232233
}

0 commit comments

Comments
 (0)