From 0887cb29aa80eff8e01c18e94127ba3f3d3f2d72 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Thu, 7 Sep 2023 19:14:43 -0700 Subject: [PATCH] Fix bug in AI handling of blank letters --- src/board.rs | 4 ++++ src/player_interaction/ai.rs | 22 +++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/board.rs b/src/board.rs index 1413328..2d12dd9 100644 --- a/src/board.rs +++ b/src/board.rs @@ -100,6 +100,10 @@ impl Letter { } } + pub fn partial_match(&self, other: &Letter) -> bool { + self == other || (self.is_blank == other.is_blank && self.points == other.points) + } + } #[derive(Debug, Copy, Clone, Serialize)] diff --git a/src/player_interaction/ai.rs b/src/player_interaction/ai.rs index fcd9b22..ab34fd9 100644 --- a/src/player_interaction/ai.rs +++ b/src/player_interaction/ai.rs @@ -376,8 +376,6 @@ impl AI { ) { - assert!(!letter.is_blank); // we should have handled blanks in evaluate_letter_at_spot - // let's now assign the letter to this spot let mut line_letters = line_letters.clone(); *line_letters.get_mut(line_index as usize).unwrap() = Some(letter); @@ -476,13 +474,15 @@ impl AI { available_letters.iter() .for_each(|in_tray| { - if skipped_one || *in_tray != letter { + if skipped_one || !in_tray.partial_match(&letter) { new_available_letters.push(*in_tray); } else { skipped_one = true; } }); + assert!(skipped_one); // at least one should have been removed + self.evaluate_spot_heading_right( &line_letters, line_cross_letters, @@ -771,15 +771,27 @@ mod tests { }); let mut ai = AI::new(difficulty, &dictionary); - + let moves = ai.find_all_moves(&tray, &board); //let rng = SmallRng::seed_from_u64(123); + println!("Moves are {:?}", moves); + + assert_eq!(moves.len(), 10); + + tray.letters[4] = Some( + Letter { + text: ' ', + points: 0, + ephemeral: false, + is_blank: true, + } + ); + let moves = ai.find_all_moves(&tray, &board); println!("Moves are {:?}", moves); assert_eq!(moves.len(), 10); - } #[test]