wip fix allowed moves infinite loop

This commit is contained in:
Henri Bourcereau 2025-01-26 17:52:57 +01:00
parent 38100a61b2
commit 2a67996453

View file

@ -33,6 +33,13 @@ pub enum MoveError {
MustPlayStrongerDie,
}
#[derive(std::cmp::PartialEq, Debug)]
pub enum TricTracRule {
ExitRule,
MustFillQuarterRule,
CornerRule,
}
/// MoveRules always consider that the current player is White
/// You must use 'mirror' functions on board & CheckerMoves if player is Black
#[derive(Default)]
@ -62,12 +69,16 @@ impl MoveRules {
}
}
pub fn moves_follow_rules(&self, moves: &(CheckerMove, CheckerMove)) -> bool {
pub fn moves_follow_rules(
&self,
moves: &(CheckerMove, CheckerMove),
ignored_rules: Vec<TricTracRule>,
) -> bool {
// Check moves possibles on the board
// Check moves conforms to the dice
// Check move is allowed by the rules (to desactivate when playing with schools)
self.moves_possible(moves) && self.moves_follows_dices(moves) && {
let is_allowed = self.moves_allowed(moves);
let is_allowed = self.moves_allowed(moves, ignored_rules);
if is_allowed.is_err() {
info!("Move not allowed : {:?}", is_allowed.unwrap_err());
false
@ -165,7 +176,11 @@ impl MoveRules {
}
/// ---- moves_allowed : Third of three checks for moves
pub fn moves_allowed(&self, moves: &(CheckerMove, CheckerMove)) -> Result<(), MoveError> {
pub fn moves_allowed(
&self,
moves: &(CheckerMove, CheckerMove),
ignored_rules: Vec<TricTracRule>,
) -> Result<(), MoveError> {
self.check_corner_rules(moves)?;
if self.is_move_by_puissance(moves) {
@ -197,7 +212,9 @@ impl MoveRules {
}
// check exit rules
if !ignored_rules.contains(&TricTracRule::ExitRule) {
self.check_exit_rules(moves)?;
}
// --- interdit de jouer dans un cadran que l'adversaire peut encore remplir ----
let farthest = cmp::max(moves.0.get_to(), moves.1.get_to());
@ -267,12 +284,14 @@ impl MoveRules {
}
// toutes les sorties directes sont autorisées, ainsi que les nombres défaillants
let possible_moves_sequences = self.get_possible_moves_sequences(false);
if !possible_moves_sequences.contains(moves) {
let possible_moves_sequences_without_excedent = self.get_possible_moves_sequences(false);
if possible_moves_sequences_without_excedent.contains(moves) {
return Ok(());
}
// À ce stade au moins un des déplacements concerne un nombre en excédant
// - si d'autres séquences de mouvements sans nombre en excédant étaient possibles, on
// - si d'autres séquences de mouvements sans nombre en excédant sont possibles, on
// refuse cette séquence
if !possible_moves_sequences.is_empty() {
if !possible_moves_sequences_without_excedent.is_empty() {
return Err(MoveError::ExitByEffectPossible);
}
@ -312,7 +331,6 @@ impl MoveRules {
}
}
}
}
Ok(())
}