Skip to content

Commit

Permalink
Merge pull request #60 from Q42/bug/win-by-uncovering
Browse files Browse the repository at this point in the history
Changed win condition to uncovered tiles
  • Loading branch information
mbernson authored Jan 12, 2024
2 parents 156d942 + 47db46a commit 145a5b0
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions Shared/MinesweeperGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,15 @@ extension Grid2D where Tile == MinesweeperTile {
}

// Check win condition

// Check if all mines have a flag
let mines = memory.filter { tile in
tile.content == .mine
}
let flaggedMines = mines.filter { tile in
tile.state == .flagged
}
let allMinesAreFlagged = mines.count == flaggedMines.count

// Check if all empty tiles *don't* have a flag
let emptyTiles = memory.filter { tile in
tile.content == .empty
}
let nonMineTilesDontHaveFlags = emptyTiles.allSatisfy { tile in
tile.state != .flagged

let uncoveredTiles = memory.filter { tile in
tile.state == .exposed
}

if allMinesAreFlagged && nonMineTilesDontHaveFlags {
if emptyTiles.count == uncoveredTiles.count {
return .won
} else {
return .running
Expand Down

0 comments on commit 145a5b0

Please sign in to comment.