commit 5febf45346dbdf88d44c5900cf0e80c3702c690a
parent 953cda455938f746af709935e648623f8c2cce2c
Author: William Lindholm <william@skovde.combitech.se>
Date: Wed, 17 Dec 2025 14:17:41 +0100
Enabled debug output
Diffstat:
3 files changed, 85 insertions(+), 7 deletions(-)
diff --git a/ms_bot/board.py b/ms_bot/board.py
@@ -93,3 +93,37 @@ def parse_square_class(class_name: str) -> object:
if "bomb" in class_name:
return "mine"
return "unknown"
+
+
+def render_board_for_llm(board: BoardSnapshot) -> str:
+ """
+ Render a board snapshot as a compact ASCII grid for debugging.
+
+ Legend:
+ # = covered, F = flagged, M = mine, ? = unknown, 0-8 = open numbers
+ """
+
+ def cell_char(v: object) -> str:
+ if v == "covered":
+ return "#"
+ if v == "flagged":
+ return "F"
+ if v == "mine":
+ return "M"
+ if v == "unknown":
+ return "?"
+ if isinstance(v, int) and 0 <= v <= 8:
+ return str(v)
+ return "?"
+
+ xs = list(range(board.min_x, board.max_x + 1))
+ ys = list(range(board.min_y, board.max_y + 1))
+ header = " " + " ".join(f"{x:>2}" for x in xs)
+ lines = [
+ f"origin=({board.min_x},{board.min_y}) size={board.width}x{board.height}",
+ header,
+ ]
+ for y in ys:
+ row = [cell_char(board.get(Cell(x, y))) for x in xs]
+ lines.append(f"{y:>3} " + " ".join(f"{c:>2}" for c in row))
+ return "\n".join(lines)
diff --git a/ms_bot/bot.py b/ms_bot/bot.py
@@ -8,7 +8,7 @@ from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from playwright.async_api import Page
-from .board import BoardSnapshot, Cell, parse_square_class
+from .board import BoardSnapshot, Cell, parse_square_class, render_board_for_llm
from .solver import best_guess, choose_first_click, iter_in_order, solve_step
@@ -316,6 +316,14 @@ async def run_bot(
for line in history:
print(line)
print("---- end history ----")
+ if state == "dead":
+ try:
+ final_board = await read_board(page, width=width, height=height)
+ print("---- final board ----")
+ print(render_board_for_llm(final_board))
+ print("---- end final board ----")
+ except Exception as e:
+ print(f"Failed to dump final board: {e}")
if leave_open and headful:
print("Leaving browser open. Close the browser window to exit.")
# Wait until the user closes the window; Ctrl+C should exit cleanly.
diff --git a/ms_bot/solver.py b/ms_bot/solver.py
@@ -241,6 +241,8 @@ def _enumerate_component(
var_to_cons[vi].append(ci)
assignment = [0] * len(variables)
+ # Heuristic ordering: assign most constrained variables first for better pruning.
+ order = sorted(range(len(variables)), key=lambda vi: len(var_to_cons[vi]), reverse=True)
def feasible() -> bool:
for ci in range(len(constraints)):
@@ -265,10 +267,11 @@ def _enumerate_component(
mines_per_cell[variables[vi]] += 1
return
+ vi = order[i]
# Try safe then mine to bias towards fewer mines for early pruning.
for val in (0, 1):
- assignment[i] = val
- touched = var_to_cons[i]
+ assignment[vi] = val
+ touched = var_to_cons[vi]
for ci in touched:
cons_remaining[ci] -= 1
cons_assigned[ci] += val
@@ -277,7 +280,7 @@ def _enumerate_component(
for ci in touched:
cons_assigned[ci] -= val
cons_remaining[ci] += 1
- assignment[i] = 0
+ assignment[vi] = 0
backtrack(0)
return total, mines_per_cell
@@ -286,7 +289,7 @@ def _enumerate_component(
def _probability_guess(
board: BoardSnapshot,
total_mines: int,
- component_limit: int = 18,
+ component_limit: int = 24,
) -> tuple[Cell | None, float | None, set[Cell], set[Cell]]:
covered_all = {c for c in board.iter_cells() if board.get(c) == "covered"}
flagged = board.flagged_cells()
@@ -299,6 +302,27 @@ def _probability_guess(
probs: dict[Cell, float] = {}
unconstrained = covered_all - frontier - flagged
+ def heuristic_prob_for_frontier_cell(cell: Cell) -> float | None:
+ # Cheap local estimate for cells in large frontier components that we
+ # don't enumerate: look at each adjacent revealed number constraint and
+ # estimate mine chance as (mines_needed / covered_neighbors_count).
+ estimates: list[float] = []
+ for n in board.neighbors(cell):
+ v = board.get(n)
+ if not isinstance(v, int) or v < 0:
+ continue
+ covered, _flagged, flagged_count = _adjacent_sets(board, n)
+ if cell not in covered:
+ continue
+ need = v - flagged_count
+ if need < 0 or len(covered) == 0:
+ continue
+ estimates.append(need / len(covered))
+ if not estimates:
+ return None
+ # Conservative-ish: use the maximum local density among adjacent constraints.
+ return max(estimates)
+
for variables, cons in _components(frontier, constraints):
if len(variables) == 0:
continue
@@ -320,8 +344,20 @@ def _probability_guess(
base_prob = (mines_left / remaining_covered) if remaining_covered else 1.0
for c in unconstrained:
probs[c] = base_prob
-
- candidates = [c for c in covered_all if c not in flagged]
+ # Frontier cells in skipped components: fill with local heuristic estimate.
+ for c in frontier:
+ if c in flagged or c in probs:
+ continue
+ hp = heuristic_prob_for_frontier_cell(c)
+ if hp is not None:
+ probs[c] = hp
+
+ # If we have any frontier, prefer guessing *on* the frontier instead of
+ # clicking in an unrelated "sea" of covered tiles.
+ if frontier:
+ candidates = [c for c in frontier if c not in flagged and board.get(c) == "covered"]
+ else:
+ candidates = [c for c in covered_all if c not in flagged]
if not candidates:
return None, None, safe, mines