demine 0.1.0

A minesweeper solver.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
mod base;
mod checker;
mod set_solver;

/// Minesweeper solver instance.
pub struct Solver {
    inner: base::Solver,
    utils: base::SolverUtils,
    checker: checker::CheckerData,
}

/// This is returned once known numbers have been set.
pub struct SolverWithKnown<'a> {
    solver: &'a mut Solver,
}

pub use base::Failure as SolverFailure;

pub use crate::solver::base::counters::print as print_dbg_counters;

/// Minesweeper solver error.
#[derive(Debug, Clone)]
pub enum Error {
    InvalidGridSize,
    RowColumnOutOfRange,
    SolverFailure(SolverFailure),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::InvalidGridSize => {
                f.write_str("Invalid size of new grid, expected width * height")
            }
            Error::RowColumnOutOfRange => f.write_str("Row or column is out of range"),
            Error::SolverFailure(SolverFailure::Empty) => {
                f.write_str("No solution; grid is completely blank")
            }
            Error::SolverFailure(SolverFailure::MustGuess) => {
                f.write_str("No cell is guaranteed to be safe")
            }
            Error::SolverFailure(SolverFailure::Solved) => {
                f.write_str("The board is already solved")
            }
            Error::SolverFailure(SolverFailure::Unsolvable) => {
                f.write_str("The board is unsolvable")
            }
        }
    }
}

impl Solver {
    pub fn new(w: usize, h: usize, seed: u64) -> Self {
        let inner = base::Solver::new(w, h, seed);
        let utils = base::SolverUtils::new(&inner);
        let checker = checker::CheckerData::new(&inner);
        Self {
            inner,
            utils,
            checker,
        }
    }

    pub fn with_known(
        &mut self,
        grid: &[impl Into<Option<usize>> + Clone],
        n_mines: Option<usize>,
    ) -> Result<SolverWithKnown<'_>, Error> {
        if grid.len() != self.inner.w * self.inner.h {
            Err(Error::InvalidGridSize)
        } else {
            self.checker_state().update(grid, n_mines);
            Ok(SolverWithKnown { solver: self })
        }
    }

    pub fn size(&self) -> (usize, usize) {
        (self.inner.w, self.inner.h)
    }

    fn checker_state(&mut self) -> checker::CheckerState<'_> {
        checker::CheckerState::new(&self.inner, &mut self.checker, &mut self.utils)
    }
}

impl<'a> SolverWithKnown<'a> {
    pub fn solver(&self) -> &'_ Solver {
        self.solver
    }

    /// Finds a guaranteed safe cell indexed (row, col) or returns a solver
    /// failure.
    pub fn find_safe_cell(&mut self) -> Result<(usize, usize), Error> {
        self.solver
            .checker_state()
            .find_safe_cell(|_| true)
            .map_err(Error::SolverFailure)
    }

    /// Finds a safe cell if you already know some cells are mines / want to
    /// filter out potential safe cells. Note this might return a cell for which
    /// the filter function returns false if it is safe; the function is merely
    /// a guide.
    pub fn find_safe_cell_filtering(
        &mut self,
        filter_cells: impl FnMut(usize) -> bool,
    ) -> Result<(usize, usize), Error> {
        self.solver
            .checker_state()
            .find_safe_cell(filter_cells)
            .map_err(Error::SolverFailure)
    }

    /// Returns the probability that the cell at (row, col) is a mine. If this
    /// solver has a fixed number of mines, mines are assumed to be randomly
    /// distributed across the unknown cells in a way that is consistent with
    /// the known cells. If there is no fixed number of mines, and the cell may
    /// be a mine, this returns None.
    pub fn mine_probability(&mut self, row: usize, col: usize) -> Result<Option<f32>, Error> {
        let pos = self.get_pos(row, col)?;
        self.solver
            .checker_state()
            .mine_probability(pos)
            .map_err(Error::SolverFailure)
    }

    pub fn can_be_mine(&mut self, row: usize, col: usize) -> Result<bool, Error> {
        let pos = self.get_pos(row, col)?;
        Ok(self.solver.checker_state().can_be_mine(pos))
    }

    fn get_pos(&self, row: usize, col: usize) -> Result<usize, Error> {
        let &base::Solver { w, h, .. } = &self.solver.inner;
        if row >= h || col >= w {
            Err(Error::RowColumnOutOfRange)
        } else {
            Ok(row * w + col)
        }
    }
}

// These tests are written by Claude.
#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::{board_from_str, board_to_str, iter_neighbors, position_to_row_col};
    use rand::prelude::*;

    // -------------------------------------------------------
    // Helpers
    // -------------------------------------------------------

    type Grid = Vec<Option<usize>>;

    struct TestBoard {
        grid: Grid,
        w: usize,
        h: usize,
    }

    impl TestBoard {
        fn parse(s: &str) -> Self {
            let board = board_from_str(s).unwrap();
            Self {
                grid: board.to_known().collect(),
                w: board.w,
                h: board.h,
            }
        }

        fn solver(&self, seed: u64) -> Solver {
            Solver::new(self.w, self.h, seed)
        }

        fn unknown_count(&self) -> usize {
            self.grid.iter().filter(|cell| cell.is_none()).count()
        }
    }

    fn board(s: &str) -> TestBoard {
        TestBoard::parse(s)
    }

    /// Assert exact mine probabilities at given cells. Panics with a clear
    /// message on mismatch. Requires a fixed mine count (`n_mines =
    /// Some(...)`).
    fn assert_probs(board: &str, n_mines: usize, expected: &[((usize, usize), f32)]) {
        let board = TestBoard::parse(board);
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(n_mines)).unwrap();
        for &((r, c), want) in expected {
            let got = sk
                .mine_probability(r, c)
                .expect("mine_probability returned Err")
                .expect("expected Some(prob) with fixed mine count");
            assert!(
                (got - want).abs() < 1e-4,
                "({r},{c}): expected prob {want}, got {got}"
            );
        }
    }

    // -------------------------------------------------------
    // Construction & size
    // -------------------------------------------------------

    #[test]
    fn new_solver_has_correct_size() {
        let s = Solver::new(8, 5, 0);
        assert_eq!(s.size(), (8, 5));
    }

    #[test]
    fn size_matches_board_dimensions() {
        let board = board("1 1 1\n1 # 1\n1 1 1");
        let solver = board.solver(0);
        assert_eq!(solver.size(), (board.w, board.h));
    }

    // -------------------------------------------------------
    // board_to_str / board_from_str round-trips
    // -------------------------------------------------------

    #[test]
    fn board_str_roundtrip_numbers_and_unknown() {
        let board = "0 1 #\n0 2 #\n0 1 #";
        let parsed = TestBoard::parse(board);
        assert_eq!(board_to_str(&parsed.grid, parsed.w, None), board);
    }

    #[test]
    fn board_str_roundtrip_with_mine_overlay() {
        // "X" in the string means a mine; round-trip via to_known / to_mines.
        let board = "1 X #\n0 1 #";
        let result = board_from_str(board).unwrap();
        let known: Vec<Option<usize>> = result.to_known().collect();
        let mines: Vec<bool> = result.to_mines().collect();
        assert_eq!(board_to_str(&known, result.w, Some(&mines)), board);
    }

    #[test]
    fn board_from_str_parses_mine_cells() {
        use crate::utils::BoardFromStrCell::*;
        let result = board_from_str("1 X #").unwrap();
        assert_eq!(result.cells, vec![Known(1), Mine, Unknown]);
        assert_eq!(result.w, 3);
        assert_eq!(result.h, 1);
    }

    #[test]
    fn board_str_roundtrip_random() {
        let mut rng = SmallRng::seed_from_u64(42);
        for _ in 0..20 {
            let w = rng.random_range(2usize..=10);
            let h = rng.random_range(2usize..=10);
            let grid: Vec<Option<usize>> = (0..w * h)
                .map(|_| {
                    if rng.random_bool(0.3) {
                        None
                    } else {
                        Some(rng.random_range(0usize..=8))
                    }
                })
                .collect();
            let s = board_to_str(&grid, w, None);
            let parsed = TestBoard::parse(&s);
            assert_eq!((w, &grid), (parsed.w, &parsed.grid));
        }
    }

    // -------------------------------------------------------
    // Utils: iter_neighbors, position_to_row_col
    // -------------------------------------------------------

    #[test]
    fn iter_neighbors_corner_gives_three() {
        let ns: Vec<_> = iter_neighbors(0, 0, 5, 5).collect();
        assert_eq!(ns.len(), 3);
        assert!(ns.contains(&(0, 1)));
        assert!(ns.contains(&(1, 0)));
        assert!(ns.contains(&(1, 1)));
    }

    #[test]
    fn iter_neighbors_center_gives_eight() {
        let ns: Vec<_> = iter_neighbors(2, 2, 5, 5).collect();
        assert_eq!(ns.len(), 8);
    }

    #[test]
    fn iter_neighbors_edge_gives_five() {
        let ns: Vec<_> = iter_neighbors(0, 2, 5, 5).collect();
        assert_eq!(ns.len(), 5);
    }

    #[test]
    fn position_to_row_col_correct() {
        assert_eq!(position_to_row_col(0, 5), (0, 0));
        assert_eq!(position_to_row_col(4, 5), (0, 4));
        assert_eq!(position_to_row_col(5, 5), (1, 0));
        assert_eq!(position_to_row_col(7, 3), (2, 1));
    }

    // -------------------------------------------------------
    // with_known — error cases
    // -------------------------------------------------------

    #[test]
    fn with_known_wrong_size_errors() {
        let mut s = Solver::new(3, 3, 0);
        assert!(matches!(
            s.with_known(&vec![None::<usize>; 8], Some(1)),
            Err(Error::InvalidGridSize)
        ));
        assert!(matches!(
            s.with_known(&vec![None::<usize>; 10], Some(1)),
            Err(Error::InvalidGridSize)
        ));
    }

    #[test]
    fn with_known_correct_size_succeeds() {
        let mut s = Solver::new(3, 3, 0);
        assert!(s.with_known(&vec![None::<usize>; 9], Some(1)).is_ok());
    }

    // -------------------------------------------------------
    // Out-of-bounds errors
    // -------------------------------------------------------

    #[test]
    fn mine_probability_out_of_range() {
        let board = board("0 0 0\n0 1 #\n0 1 #");
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(1)).unwrap();
        assert!(matches!(
            sk.mine_probability(10, 0),
            Err(Error::RowColumnOutOfRange)
        ));
        assert!(matches!(
            sk.mine_probability(0, 10),
            Err(Error::RowColumnOutOfRange)
        ));
    }

    #[test]
    fn can_be_mine_out_of_range() {
        let board = board("0 1 #");
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(1)).unwrap();
        assert!(matches!(
            sk.can_be_mine(5, 0),
            Err(Error::RowColumnOutOfRange)
        ));
    }

    // -------------------------------------------------------
    // Error display
    // -------------------------------------------------------

    #[test]
    fn error_display_messages() {
        let cases = [
            (Error::InvalidGridSize, "Invalid size"),
            (Error::RowColumnOutOfRange, "out of range"),
            (
                Error::SolverFailure(SolverFailure::Empty),
                "completely blank",
            ),
            (Error::SolverFailure(SolverFailure::MustGuess), "guaranteed"),
            (
                Error::SolverFailure(SolverFailure::Solved),
                "already solved",
            ),
            (
                Error::SolverFailure(SolverFailure::Unsolvable),
                "unsolvable",
            ),
        ];
        for (err, needle) in cases {
            let msg = err.to_string().to_lowercase();
            assert!(
                msg.contains(&needle.to_lowercase()),
                "Expected '{needle}' in \"{msg}\""
            );
        }
    }

    // -------------------------------------------------------
    // Basic solver behaviour
    // -------------------------------------------------------

    #[test]
    fn fully_revealed_board_is_solved() {
        let board = board("0 0 0\n0 0 0\n0 0 0");
        let mut solver = board.solver(0);
        let result = solver
            .with_known(&board.grid, Some(0))
            .unwrap()
            .find_safe_cell();
        assert!(
            matches!(result, Err(Error::SolverFailure(SolverFailure::Solved))),
            "Expected Solved, got {result:?}"
        );
    }

    #[test]
    fn all_unknown_board_cannot_find_safe_cell() {
        let mut s = Solver::new(3, 3, 0);
        let grid = vec![None::<usize>; 9];
        let result = s.with_known(&grid, Some(1)).unwrap().find_safe_cell();
        assert!(
            result.is_err(),
            "All-unknown board should not find a safe cell"
        );
    }

    #[test]
    fn single_mine_identified_as_prob_one() {
        // (1,2) is the only unknown, and there is 1 mine → prob must be 1.0.
        let board = board("0 1 1\n0 1 #\n0 1 1");
        let mut solver = board.solver(0);
        let p = solver
            .with_known(&board.grid, Some(1))
            .unwrap()
            .mine_probability(1, 2)
            .unwrap()
            .unwrap();
        assert!(
            (p - 1.0).abs() < 1e-6,
            "Sole unknown with 1 mine: expected prob 1.0, got {p}"
        );
    }

    #[test]
    fn safe_cell_is_unknown() {
        // (1,2) is mine, (0,2) is safe. Solver should return (0,2).
        let board = board("0 1 #\n0 1 #\n0 0 0");
        let mut solver = board.solver(0);
        let (r, c) = solver
            .with_known(&board.grid, Some(1))
            .unwrap()
            .find_safe_cell()
            .unwrap();
        assert!(
            board.grid[r * board.w + c].is_none(),
            "Safe cell ({r},{c}) should be unknown"
        );
    }

    #[test]
    fn can_be_mine_consistent_with_probability() {
        // For deterministic cells: can_be_mine should agree with prob 0 / prob 1.
        let board = board("1 2 1\n# # #");
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(2)).unwrap();
        // (1,1) has prob 0 — cannot be a mine.
        assert!(!sk.can_be_mine(1, 1).unwrap(), "(1,1) should not be a mine");
        // (1,0) and (1,2) have prob 1 — must be mines.
        assert!(sk.can_be_mine(1, 0).unwrap(), "(1,0) should be a mine");
        assert!(sk.can_be_mine(1, 2).unwrap(), "(1,2) should be a mine");
    }

    // -------------------------------------------------------
    // Determinism across seeds
    // -------------------------------------------------------

    #[test]
    fn same_seed_gives_same_result() {
        let board = board("# # #\n# 3 #\n# # #");
        for seed in [0u64, 1, 999] {
            let mut s1 = board.solver(seed);
            let mut s2 = board.solver(seed);
            let r1 = s1
                .with_known(&board.grid, Some(3))
                .unwrap()
                .find_safe_cell();
            let r2 = s2
                .with_known(&board.grid, Some(3))
                .unwrap()
                .find_safe_cell();
            assert_eq!(format!("{r1:?}"), format!("{r2:?}"), "seed {seed}");
        }
    }

    // -------------------------------------------------------
    // Exact probability values
    // -------------------------------------------------------

    #[test]
    fn one_two_one_exact_probs() {
        // Constraints: (1,0)+(1,1)=1, (1,0)+(1,1)+(1,2)=2, (1,1)+(1,2)=1
        // → unique solution: (1,0)=mine, (1,1)=safe, (1,2)=mine.
        assert_probs(
            "1 2 1\n# # #",
            2,
            &[((1, 0), 1.0), ((1, 1), 0.0), ((1, 2), 1.0)],
        );
    }

    #[test]
    fn one_one_edge_is_half() {
        // Two unknowns, each constrained once by a "1" → 50/50.
        assert_probs("1 1\n# #", 1, &[((1, 0), 0.5), ((1, 1), 0.5)]);
    }

    #[test]
    fn uniform_ring_around_center() {
        // 8 symmetric unknowns, 2 mines → each exactly 0.25.
        let cells = [
            (0, 0),
            (0, 1),
            (0, 2),
            (1, 0),
            (1, 2),
            (2, 0),
            (2, 1),
            (2, 2),
        ];
        let expected: Vec<_> = cells.iter().map(|&p| (p, 0.25f32)).collect();
        assert_probs("# # #\n# 2 #\n# # #", 2, &expected);
    }

    #[test]
    fn corner_one_three_neighbors_each_third() {
        // "1" in corner; 3 unknown neighbors; 1 mine → each 1/3.
        let third = 1.0f32 / 3.0;
        assert_probs(
            "1 #\n# #",
            1,
            &[((0, 1), third), ((1, 0), third), ((1, 1), third)],
        );
    }

    #[test]
    fn all_forced_mines() {
        // (0,0) adj to "2" seeing only (0,0)&(0,1); "3" seeing all three.
        // Unique solution: all three are mines.
        assert_probs(
            "# # #\n2 3 2",
            3,
            &[((0, 0), 1.0), ((0, 1), 1.0), ((0, 2), 1.0)],
        );
    }

    #[test]
    fn one_two_two_two_one_middle_forced() {
        // Two solutions: mines at {0,2,3} or {1,2,4}. Col 2 is always a mine.
        assert_probs(
            "# # # # #\n1 2 2 2 1",
            3,
            &[
                ((0, 2), 1.0),
                ((0, 0), 0.5),
                ((0, 1), 0.5),
                ((0, 3), 0.5),
                ((0, 4), 0.5),
            ],
        );
    }

    // -------------------------------------------------------
    // Probability invariants
    // -------------------------------------------------------

    #[test]
    fn probabilities_sum_to_mine_count() {
        let board = board("# # # #\n# 3 2 #\n# 2 1 1\n# 1 0 0");
        let mut solver = board.solver(0);
        let n_mines = 4;
        let mut sk = solver.with_known(&board.grid, Some(n_mines)).unwrap();
        let sum: f64 = (0..board.h)
            .flat_map(|r| (0..board.w).map(move |c| (r, c)))
            .filter(|&(r, c)| board.grid[r * board.w + c].is_none())
            .map(|(r, c)| sk.mine_probability(r, c).unwrap().unwrap() as f64)
            .sum();
        assert!(
            (sum - n_mines as f64).abs() < 0.1,
            "Sum of probs {sum:.3} should ≈ {n_mines}"
        );
    }

    #[test]
    fn probability_bounds_always_zero_to_one() {
        let board = board("# # #\n# 2 #\n# # #");
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(2)).unwrap();
        for r in 0..board.h {
            for c in 0..board.w {
                if board.grid[r * board.w + c].is_none() {
                    let p = sk.mine_probability(r, c).unwrap().unwrap();
                    assert!((0.0..=1.0).contains(&p), "({r},{c}) prob {p} out of [0,1]");
                }
            }
        }
    }

    #[test]
    fn symmetric_board_symmetric_probs() {
        let board = board("# # #\n# 4 #\n# # #");
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(4)).unwrap();
        let corner_p = sk.mine_probability(0, 0).unwrap().unwrap();
        let edge_p = sk.mine_probability(0, 1).unwrap().unwrap();
        for (r, c) in [(0, 2), (2, 0), (2, 2)] {
            assert!((sk.mine_probability(r, c).unwrap().unwrap() - corner_p).abs() < 1e-6);
        }
        for (r, c) in [(1, 0), (1, 2), (2, 1)] {
            assert!((sk.mine_probability(r, c).unwrap().unwrap() - edge_p).abs() < 1e-6);
        }
    }

    #[test]
    fn known_cell_cannot_be_mine() {
        let board = board("0 1 #\n0 1 #\n0 0 0");
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(1)).unwrap();
        match sk.mine_probability(0, 0) {
            Ok(Some(p)) => assert!(p < 1e-6, "Known cell prob should be 0, got {p}"),
            Ok(None) | Err(_) => {}
        }
        assert!(
            !sk.can_be_mine(0, 0).unwrap(),
            "Known cell should not can_be_mine"
        );
    }

    // -------------------------------------------------------
    // No fixed mine count → None for ambiguous cells
    // -------------------------------------------------------

    #[test]
    fn no_mine_count_ambiguous_returns_none() {
        let board = board("# # #\n# 1 #\n# # #");
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, None).unwrap();
        for r in 0..board.h {
            for c in 0..board.w {
                if board.grid[r * board.w + c].is_none() {
                    match sk.mine_probability(r, c) {
                        Ok(None) => {}
                        Ok(Some(p)) => assert!(
                            p == 0.0 || p == 1.0,
                            "Without mine count, non-None prob must be 0 or 1, got {p}"
                        ),
                        Err(_) => {}
                    }
                }
            }
        }
    }

    // -------------------------------------------------------
    // All mines on constraint boundary → outside cells safe
    // -------------------------------------------------------

    // Board: one "1" in top-left corner; 1 mine total.
    // Boundary = {(0,1),(1,0),(1,1)}: the three unknowns adjacent to the "1".
    // Outside = {(0,2)..(1,4)}: unknowns with no known neighbor.
    const BOUNDARY_BOARD: &str = "1 # # # #\n# # # # #";
    const BOUNDARY: [(usize, usize); 3] = [(0, 1), (1, 0), (1, 1)];
    const OUTSIDE: [(usize, usize); 6] = [(0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4)];

    #[test]
    fn boundary_fixed_count_outside_safe() {
        let board = board(BOUNDARY_BOARD);
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(1)).unwrap();

        let third = 1.0f32 / 3.0;
        for (r, c) in BOUNDARY {
            let p = sk.mine_probability(r, c).unwrap().unwrap();
            assert!(
                (p - third).abs() < 1e-4,
                "Boundary ({r},{c}) prob {p}, expected ~1/3"
            );
        }
        for (r, c) in OUTSIDE {
            let p = sk.mine_probability(r, c).unwrap().unwrap();
            assert!(p < 1e-6, "Outside ({r},{c}) should be prob 0, got {p}");
            assert!(
                !sk.can_be_mine(r, c).unwrap(),
                "Outside ({r},{c}) cannot be mine"
            );
        }

        let (r, c) = sk
            .find_safe_cell()
            .expect("should find a safe outside cell");
        assert!(
            OUTSIDE.contains(&(r, c)),
            "find_safe_cell returned ({r},{c}), expected outside"
        );
    }

    #[test]
    fn boundary_no_count_must_guess() {
        let board = board(BOUNDARY_BOARD);
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, None).unwrap();

        let result = sk.find_safe_cell();
        assert!(
            matches!(result, Err(Error::SolverFailure(SolverFailure::MustGuess))),
            "Expected MustGuess without mine count, got {result:?}"
        );
        for (r, c) in OUTSIDE {
            let p = sk.mine_probability(r, c).unwrap();
            assert!(
                p.is_none(),
                "Outside ({r},{c}) prob should be None, got {p:?}"
            );
        }
    }

    // -------------------------------------------------------
    // find_safe_cell_filtering
    // -------------------------------------------------------

    #[test]
    fn filtering_steers_toward_preferred_cells() {
        // Outside cells are all guaranteed safe. Filter to only prefer the
        // rightmost column; solver should pick from there (or anywhere safe).
        let board = board(BOUNDARY_BOARD);
        let mut solver = board.solver(0);
        let mut sk = solver.with_known(&board.grid, Some(1)).unwrap();

        // Filter: prefer column 4 (rightmost).
        let result = sk.find_safe_cell_filtering(|pos| pos % board.w == 4);
        let (r, c) = result.expect("should find a safe cell with filter");
        // The result must still be a safe (unknown) cell.
        assert!(
            board.grid[r * board.w + c].is_none(),
            "Filtered safe cell must be unknown"
        );
    }

    // -------------------------------------------------------
    // Random boards: probabilities stay in [0, 1]
    // -------------------------------------------------------

    #[test]
    fn random_boards_probs_in_range() {
        let mut rng = SmallRng::seed_from_u64(777);
        for _ in 0..30 {
            let w = rng.random_range(3usize..=6);
            let h = rng.random_range(3usize..=6);
            let n_mines = rng.random_range(1usize..=(w * h / 3).max(1));
            let board = TestBoard {
                grid: (0..w * h)
                    .map(|_| {
                        if rng.random_bool(0.4) {
                            Some(rng.random_range(0usize..=8))
                        } else {
                            None
                        }
                    })
                    .collect(),
                w,
                h,
            };
            if board.unknown_count() == 0 {
                continue;
            }
            let mut solver = board.solver(rng.random());
            let Ok(mut sk) = solver.with_known(&board.grid, Some(n_mines)) else {
                continue;
            };
            for r in 0..board.h {
                for c in 0..board.w {
                    if board.grid[r * board.w + c].is_none() {
                        if let Ok(Some(p)) = sk.mine_probability(r, c) {
                            assert!(
                                (0.0..=1.0).contains(&p),
                                "({r},{c}) prob {p} out of [0,1] on {h}×{w} board"
                            );
                        }
                    }
                }
            }
        }
    }
}