Skip to content

Commit d80fc0d

Browse files
committed
Add IsMineAt, reformat
1 parent 9160be2 commit d80fc0d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

terminal-minesweeper/Program.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,20 @@ public bool Loop() {
157157
// calculate neighbour mine count for every cell
158158
RecalculateCellNumbers();
159159

160-
var gameWon = false;
161-
while (!_gameEnd) {
160+
bool gameWon;
161+
while (true) {
162162
UpdateTerminal();
163163
UserInput();
164164
UncoverCell(CurPos);
165-
if (MineAt(CurPos) != null) {
166-
_gameEnd = true;
165+
if (IsMineAt(CurPos)) {
167166
gameWon = false;
167+
break;
168168
}
169+
// ReSharper disable once InvertIf
169170
if (_uncoveredCellsCoords.Count + _mines.Count == _gridSize.X * _gridSize.Y) {
170-
_gameEnd = true;
171171
gameWon = true;
172-
if (_uncoveredCellsCoords.Any(coords => MineAt(coords) != null)) gameWon = false;
172+
if (_uncoveredCellsCoords.Any(IsMineAt)) gameWon = false;
173+
break;
173174
}
174175
}
175176
UpdateTerminal();
@@ -208,9 +209,9 @@ private void UncoverCell(Coords coords) {
208209
private void RecalculateCellNumbers() {
209210
for (var y = 0; y < _gridSize.Y; y++) {
210211
for (var x = 0; x < _gridSize.X; x++) {
211-
if (MineAt(new(x, y)) != null) continue;
212+
if (IsMineAt(new(x, y))) continue;
212213
var cellsAround = GetCellsAround(new(x, y));
213-
var mineCountAround = cellsAround.Count(cell => MineAt(cell) != null);
214+
var mineCountAround = cellsAround.Count(IsMineAt);
214215
_gameGrid[y, x].Data.Number = mineCountAround;
215216
}
216217

@@ -315,7 +316,7 @@ private void UserInput() {
315316
if (_flaggedCellsCoords.Contains(CurPos) || _uncoveredCellsCoords.Contains(CurPos)) break;
316317

317318
// move/remove the mine if it is the first thing the user reveals
318-
var mineAtCurPos = MineAt(CurPos);
319+
var mineAtCurPos = GetMineAt(CurPos);
319320
if (_uncoveredCellsCoords.Count == 0 && mineAtCurPos != null) {
320321
if (_mines.Count > 1) {
321322
_mines.Remove(mineAtCurPos);
@@ -367,10 +368,13 @@ private void UpdateTerminal() {
367368
PrintColoredStrings(CreateGridString(), defaultBackgroundColor: _defaultBackgroundColor);
368369
}
369370

370-
private Mine? MineAt(Coords coords) {
371+
private Mine? GetMineAt(Coords coords) {
371372
_coordsMinesMap.TryGetValue(coords, out var result);
372373
return result;
373374
}
375+
private bool IsMineAt(Coords coords) {
376+
return GetMineAt(coords) != null;
377+
}
374378

375379
private static void UpdateGrid(ref Grid grid, HashSet<Coords> flagsCoords, HashSet<Coords> uncoveredCoords) {
376380
for (var y = 0; y < grid.GetLength(0); y++) {
@@ -440,7 +444,7 @@ private List<StringColorData> CreateGridString() {
440444
}
441445

442446
// 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)) {
444448
gridItem.Type = GridCellDisplayType.Mine;
445449
}
446450

0 commit comments

Comments
 (0)