|
| 1 | +use crate::Dir; |
| 2 | + |
1 | 3 | use super::{ |
2 | 4 | Board, |
3 | 5 | hash::{CustomHashMap as HashMap, CustomHashSet as HashSet}, |
4 | 6 | }; |
5 | 7 |
|
6 | 8 | /// calculate the chances of winning the game by chosing possible moves at random |
7 | | -pub fn calculate_p_random_chance_success(feasible: Vec<Board>) -> HashMap<Board, f64> { |
8 | | - let feasible: HashSet<_> = feasible.into_iter().collect(); |
| 9 | +pub fn calculate_p_random_chance_success(feasible: impl Iterator<Item = Board>) -> HashMap<Board, f64> { |
9 | 10 | let mut chances = HashMap::default(); |
10 | 11 | chances.insert(Board::solved(), 1.0); |
11 | | - for i in 2..=(Board::SLOTS - 1) { |
12 | | - let feasible_with_i_pegs = feasible |
13 | | - .iter() |
14 | | - .copied() |
15 | | - .filter(|b| b.count_pegs() == i) |
16 | | - .collect::<Vec<_>>(); |
17 | | - for constellation in feasible_with_i_pegs { |
18 | | - let legal_moves = constellation.get_legal_moves(); |
| 12 | + |
| 13 | + let mut index: HashSet<Board> = HashSet::default(); |
| 14 | + let mut by_pegs: Vec<Vec<Board>> = vec![Vec::new(); Board::SLOTS + 2]; |
| 15 | + for board in feasible { |
| 16 | + index.insert(board); |
| 17 | + by_pegs[board.count_pegs()].push(board); |
| 18 | + } |
| 19 | + |
| 20 | + for layer in by_pegs.iter().skip(2) { |
| 21 | + for board in layer { |
| 22 | + let mut buffer = [Board::empty(); MAX_MOVES]; |
| 23 | + let successors = successors_of(*board, &mut buffer); |
19 | 24 |
|
20 | 25 | // we assume each legal move has equal chance of being taken (1 / n) |
21 | 26 | // p_success = sum(moves, P(move) * P(success | move)) |
22 | 27 | // P(success | move) = 0.0 if infeasible, else lookup |
23 | | - let p_move = 1.0 / legal_moves.len() as f64; |
| 28 | + let p_move = 1.0 / successors.len() as f64; |
24 | 29 |
|
25 | 30 | let mut p_success = 0.0; |
26 | 31 |
|
27 | | - for mov in legal_moves { |
28 | | - let c_new = constellation.mov(mov).normalize(); |
29 | | - p_success += if feasible.contains(&c_new) { |
30 | | - p_move * *chances.get(&c_new).expect("already present") |
| 32 | + for succ in successors { |
| 33 | + p_success += if index.contains(&succ) { |
| 34 | + p_move * *chances.get(&succ).expect("already present") |
31 | 35 | } else { |
32 | 36 | p_move * 0.0 |
33 | 37 | }; |
34 | 38 | } |
35 | 39 |
|
36 | | - chances.insert(constellation, p_success); |
| 40 | + chances.insert(*board, p_success); |
37 | 41 | } |
38 | 42 | } |
39 | 43 | chances |
40 | 44 | } |
| 45 | + |
| 46 | +const MAX_MOVES: usize = 76; |
| 47 | + |
| 48 | +fn successors_of(board: Board, buffer: &mut [Board; MAX_MOVES]) -> &[Board] { |
| 49 | + let syms = board.symmetries(); |
| 50 | + let mut len = 0; |
| 51 | + for dir in Dir::enumerate() { |
| 52 | + for idx in board.mov_pattern_mask(dir) { |
| 53 | + debug_assert!(len < MAX_MOVES, "more than {MAX_MOVES} moves from one board"); |
| 54 | + buffer[len] = Board::normalize_after_move(&syms, idx, dir); |
| 55 | + len += 1; |
| 56 | + } |
| 57 | + } |
| 58 | + &buffer[..len] |
| 59 | +} |
0 commit comments