@@ -83,79 +83,81 @@ bool GameBoard::inBounds(const int x, const int y) const
8383}
8484
8585GameBoard::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
9390void 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
10299void 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
107109bool 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
122124bool 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
137139void 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
193195bool GameBoard::cellHasMine (const int x, const int y) const
194196{
195- return board_[x][y].hasMine ();
197+ return board_[x][y].hasMine ();
196198}
197199
198200bool 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
204206bool 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
209211bool 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
219216int GameBoard::getWidth () const
220217{
221- return this ->width_ ;
218+ return this ->width_ ;
222219}
223220
224221int GameBoard::getHeight () const
225222{
226- return this ->height_ ;
223+ return this ->height_ ;
227224}
228225
229226int 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