@@ -157,19 +157,20 @@ public bool Loop() {
157
157
// calculate neighbour mine count for every cell
158
158
RecalculateCellNumbers ( ) ;
159
159
160
- var gameWon = false ;
161
- while ( ! _gameEnd ) {
160
+ bool gameWon ;
161
+ while ( true ) {
162
162
UpdateTerminal ( ) ;
163
163
UserInput ( ) ;
164
164
UncoverCell ( CurPos ) ;
165
- if ( MineAt ( CurPos ) != null ) {
166
- _gameEnd = true ;
165
+ if ( IsMineAt ( CurPos ) ) {
167
166
gameWon = false ;
167
+ break ;
168
168
}
169
+ // ReSharper disable once InvertIf
169
170
if ( _uncoveredCellsCoords . Count + _mines . Count == _gridSize . X * _gridSize . Y ) {
170
- _gameEnd = true ;
171
171
gameWon = true ;
172
- if ( _uncoveredCellsCoords . Any ( coords => MineAt ( coords ) != null ) ) gameWon = false ;
172
+ if ( _uncoveredCellsCoords . Any ( IsMineAt ) ) gameWon = false ;
173
+ break ;
173
174
}
174
175
}
175
176
UpdateTerminal ( ) ;
@@ -208,9 +209,9 @@ private void UncoverCell(Coords coords) {
208
209
private void RecalculateCellNumbers ( ) {
209
210
for ( var y = 0 ; y < _gridSize . Y ; y ++ ) {
210
211
for ( var x = 0 ; x < _gridSize . X ; x ++ ) {
211
- if ( MineAt ( new ( x , y ) ) != null ) continue ;
212
+ if ( IsMineAt ( new ( x , y ) ) ) continue ;
212
213
var cellsAround = GetCellsAround ( new ( x , y ) ) ;
213
- var mineCountAround = cellsAround . Count ( cell => MineAt ( cell ) != null ) ;
214
+ var mineCountAround = cellsAround . Count ( IsMineAt ) ;
214
215
_gameGrid [ y , x ] . Data . Number = mineCountAround ;
215
216
}
216
217
@@ -315,7 +316,7 @@ private void UserInput() {
315
316
if ( _flaggedCellsCoords . Contains ( CurPos ) || _uncoveredCellsCoords . Contains ( CurPos ) ) break ;
316
317
317
318
// move/remove the mine if it is the first thing the user reveals
318
- var mineAtCurPos = MineAt ( CurPos ) ;
319
+ var mineAtCurPos = GetMineAt ( CurPos ) ;
319
320
if ( _uncoveredCellsCoords . Count == 0 && mineAtCurPos != null ) {
320
321
if ( _mines . Count > 1 ) {
321
322
_mines . Remove ( mineAtCurPos ) ;
@@ -367,10 +368,13 @@ private void UpdateTerminal() {
367
368
PrintColoredStrings ( CreateGridString ( ) , defaultBackgroundColor : _defaultBackgroundColor ) ;
368
369
}
369
370
370
- private Mine ? MineAt ( Coords coords ) {
371
+ private Mine ? GetMineAt ( Coords coords ) {
371
372
_coordsMinesMap . TryGetValue ( coords , out var result ) ;
372
373
return result ;
373
374
}
375
+ private bool IsMineAt ( Coords coords ) {
376
+ return GetMineAt ( coords ) != null ;
377
+ }
374
378
375
379
private static void UpdateGrid ( ref Grid grid , HashSet < Coords > flagsCoords , HashSet < Coords > uncoveredCoords ) {
376
380
for ( var y = 0 ; y < grid . GetLength ( 0 ) ; y ++ ) {
@@ -440,7 +444,7 @@ private List<StringColorData> CreateGridString() {
440
444
}
441
445
442
446
// show the mines if the game has ended
443
- if ( MineAt ( new ( x , y ) ) != null && ( _gameEnd || _uncoveredCellsCoords . Contains ( new ( x , y ) ) || _cheatMode ) ) {
447
+ if ( IsMineAt ( new ( x , y ) ) && ( _gameEnd || _uncoveredCellsCoords . Contains ( new ( x , y ) ) || _cheatMode ) ) {
444
448
gridItem . Type = GridCellDisplayType . Mine ;
445
449
}
446
450
0 commit comments