|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from unittest.mock import patch |
| 4 | + |
3 | 5 | from sudoku_dlx.strategies import ( |
4 | 6 | apply_box_line_claiming, |
5 | 7 | apply_hidden_pair, |
| 8 | + apply_hidden_single, |
6 | 9 | apply_hidden_triple, |
7 | 10 | apply_hidden_single, |
8 | 11 | apply_locked_candidates_pointing, |
|
11 | 14 | apply_naked_triple, |
12 | 15 | apply_x_wing, |
13 | 16 | candidates, |
| 17 | + step_once, |
14 | 18 | ) |
15 | 19 |
|
16 | 20 |
|
@@ -320,3 +324,79 @@ def test_x_wing_rows_then_cols() -> None: |
320 | 324 | assert move_col["strategy"] == "x_wing_col" |
321 | 325 | assert move_col["digit"] == digit_col |
322 | 326 | assert digit_col not in cand2[3][0] |
| 327 | + |
| 328 | + |
| 329 | +def test_step_once_prefers_naked_single_over_hidden_single() -> None: |
| 330 | + grid = _empty_grid() |
| 331 | + cand = candidates(grid) |
| 332 | + |
| 333 | + _set_candidates(cand, 0, 0, {1}) |
| 334 | + _set_candidates(cand, 0, 1, {2, 3}) |
| 335 | + for c in range(9): |
| 336 | + if c != 1: |
| 337 | + cand[0][c].discard(2) |
| 338 | + |
| 339 | + grid_hidden = [row[:] for row in grid] |
| 340 | + cand_hidden = [[cell.copy() for cell in row] for row in cand] |
| 341 | + hidden_move = apply_hidden_single(grid_hidden, cand_hidden) |
| 342 | + |
| 343 | + assert hidden_move is not None |
| 344 | + assert hidden_move["strategy"] == "hidden_single" |
| 345 | + |
| 346 | + with patch("sudoku_dlx.strategies.candidates", return_value=cand): |
| 347 | + move = step_once(grid) |
| 348 | + |
| 349 | + assert move is not None |
| 350 | + assert move["strategy"] == "naked_single" |
| 351 | + assert move["r"] == 0 and move["c"] == 0 |
| 352 | + assert grid[0][0] == 1 |
| 353 | + |
| 354 | + |
| 355 | +def test_step_once_prioritizes_locked_pointing_before_box_line() -> None: |
| 356 | + grid = _empty_grid() |
| 357 | + cand = candidates(grid) |
| 358 | + |
| 359 | + for r in range(3): |
| 360 | + for c in range(3): |
| 361 | + cand[r][c].discard(1) |
| 362 | + _set_candidates(cand, 0, 0, {1, 2}) |
| 363 | + _set_candidates(cand, 0, 1, {1, 3}) |
| 364 | + _set_candidates(cand, 0, 5, {1, 4, 5}) |
| 365 | + |
| 366 | + for c in range(9): |
| 367 | + if c not in (3, 4): |
| 368 | + cand[1][c].discard(4) |
| 369 | + _set_candidates(cand, 1, 3, {4, 5}) |
| 370 | + _set_candidates(cand, 1, 4, {4, 6}) |
| 371 | + _set_candidates(cand, 0, 3, {2, 4, 7}) |
| 372 | + |
| 373 | + grid_locked = [row[:] for row in grid] |
| 374 | + cand_locked = [[cell.copy() for cell in row] for row in cand] |
| 375 | + locked_move = apply_locked_candidates_pointing(grid_locked, cand_locked) |
| 376 | + |
| 377 | + assert locked_move is not None |
| 378 | + assert locked_move["strategy"].startswith("locked_pointing") |
| 379 | + |
| 380 | + grid_box_line = [row[:] for row in grid] |
| 381 | + cand_box_line = [[cell.copy() for cell in row] for row in cand] |
| 382 | + box_line_move = apply_box_line_claiming(grid_box_line, cand_box_line) |
| 383 | + |
| 384 | + assert box_line_move is not None |
| 385 | + assert box_line_move["strategy"].startswith("box_line") |
| 386 | + |
| 387 | + with patch("sudoku_dlx.strategies.candidates", return_value=cand): |
| 388 | + move = step_once(grid) |
| 389 | + |
| 390 | + assert move is not None |
| 391 | + assert move["strategy"].startswith("locked_pointing") |
| 392 | + assert grid == _empty_grid() |
| 393 | + |
| 394 | + |
| 395 | +def test_step_once_returns_none_when_no_moves_available() -> None: |
| 396 | + grid = _empty_grid() |
| 397 | + snapshot = [row[:] for row in grid] |
| 398 | + |
| 399 | + move = step_once(grid) |
| 400 | + |
| 401 | + assert move is None |
| 402 | + assert grid == snapshot |
0 commit comments