Certainly! Computing the probability that each unrevealed cell in a Minesweeper board contains a mine is a classic constraint satisfaction problem. The general approach is:
- Parse the board to identify revealed numbers and unrevealed cells.
- For each revealed number, set up constraints: the number of mines in its adjacent unrevealed cells must equal the number shown minus the number of already-flagged mines.
- Enumerate all possible mine placements that satisfy all constraints.
- Count, for each unrevealed cell, in how many valid placements it contains a mine.
- Probability = (number of valid placements with a mine in the cell) / (total number of valid placements).
This is computationally feasible for small boards or local regions, but can be slow for large boards.
Below is a Python implementation for a small board or a local region (e.g., the "frontier" of unrevealed cells adjacent to revealed numbers):
import itertools
import numpy as np
def get_neighbors(board, r, c):
rows, cols = len(board), len(board[0])
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
if dr == 0 and dc == 0:
continue
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
yield (nr, nc)
def compute_mine_probabilities(board, total_mines=None):
rows, cols = len(board), len(board[0])
revealed = []
unrevealed = set()
for r in range(rows):
for c in range(cols):
if board[r][c] == -1:
unrevealed.add((r, c))
elif 0 <= board[r][c] <= 8:
revealed.append((r, c))
# Only consider unrevealed cells adjacent to revealed numbers (the "frontier")
frontier = set()
for r, c in revealed:
for nr, nc in get_neighbors(board, r, c):
if board[nr][nc] == -1:
frontier.add((nr, nc))
frontier = list(frontier)
n = len(frontier)
if n == 0:
# No constraints, assign uniform probability if total_mines is known
if total_mines is not None:
remaining_mines = total_mines - sum(1 for r in range(rows) for c in range(cols) if board[r][c] == 9)
prob = remaining_mines / len(unrevealed)
return {cell: prob for cell in unrevealed}
else:
return {cell: None for cell in unrevealed}
# Build constraints
constraints = []
for r, c in revealed:
neighbors = [(nr, nc) for nr, nc in get_neighbors(board, r, c) if (nr, nc) in frontier]
if neighbors:
constraints.append((neighbors, board[r][c] - sum(1 for nr, nc in get_neighbors(board, r, c) if board[nr][nc] == 9)))
# Enumerate all possible mine placements in the frontier
valid_placements = []
for mines in itertools.product([0, 1], repeat=n):
placement = dict(zip(frontier, mines))
satisfied = True
for cells, count in constraints:
if sum(placement[cell] for cell in cells) != count:
satisfied = False
break
if satisfied:
valid_placements.append(placement)
# Count probabilities
counts = {cell: 0 for cell in frontier}
for placement in valid_placements:
for cell in frontier:
if placement[cell]:
counts[cell] += 1
total = len(valid_placements)
if total == 0:
# No valid placements, return None
return {cell: None for cell in frontier}
probabilities = {cell: counts[cell] / total for cell in frontier}
return probabilities
# Example usage:
board = [
[1, 1, 1, -1],
[1, -1, 2, 1],
[1, 2, -1, 1],
[0, 1, 1, 1]
]
probs = compute_mine_probabilities(board)
for cell, prob in probs.items():
print(f"Cell {cell}: {prob:.2f}")
Notes:
- This code only computes probabilities for the "frontier" (unrevealed cells adjacent to revealed numbers).
- If you want to include all unrevealed cells, you need to know the total number of mines and adjust the code accordingly.
- For large boards, this approach is not efficient; more advanced techniques (e.g., SAT solvers, belief propagation) are used in practice.
Let me know if you want a version that handles the entire board or a specific scenario!