From 1f663cf524306bec7fef7320fc6c4b1f83bc5d9d Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Wed, 27 Mar 2024 21:10:15 +0100 Subject: [PATCH] wip rolldice --- bot/src/lib.rs | 3 ++- client_cli/src/app.rs | 1 + store/src/board.rs | 7 ++++--- store/src/game.rs | 16 ++++++++++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/bot/src/lib.rs b/bot/src/lib.rs index 08ec998..290ca79 100644 --- a/bot/src/lib.rs +++ b/bot/src/lib.rs @@ -46,7 +46,8 @@ impl Bot { pub fn consume(&mut self, event: &GameEvent) -> Option { self.game.consume(event); - // println!("{:?}", self.game); + println!("bot game {:?}", self.game); + println!("bot player_id {:?}", self.player_id); if self.game.active_player_id == self.player_id { return match self.game.turn_stage { TurnStage::RollDice => Some(GameEvent::Roll { diff --git a/client_cli/src/app.rs b/client_cli/src/app.rs index 6e1f1f6..d131d57 100644 --- a/client_cli/src/app.rs +++ b/client_cli/src/app.rs @@ -43,6 +43,7 @@ impl Game { pub fn consume(&mut self, event: &GameEvent) -> Option { if self.state.validate(event) { + println!("consuming {:?}", event); self.state.consume(event); return self .bot diff --git a/store/src/board.rs b/store/src/board.rs index a3d365c..2aaccfd 100644 --- a/store/src/board.rs +++ b/store/src/board.rs @@ -35,9 +35,10 @@ impl CheckerMove { return Err(Error::FieldInvalid); } // check that the destination is after the origin field - if to < from && to != 0 { - return Err(Error::MoveInvalid); - } + // --> not applicable for black moves + // if to < from && to != 0 { + // return Err(Error::MoveInvalid); + // } Ok(Self { from, to }) } diff --git a/store/src/game.rs b/store/src/game.rs index 2b9d6d9..383740c 100644 --- a/store/src/game.rs +++ b/store/src/game.rs @@ -288,8 +288,8 @@ impl GameState { fn moves_follows_dices(&self, color: &Color, moves: &(CheckerMove, CheckerMove)) -> bool { let (dice1, dice2) = self.dice.values; let (move1, move2): &(CheckerMove, CheckerMove) = moves.into(); - let dist1 = (move1.get_to() - move1.get_from()) as u8; - let dist2 = (move2.get_to() - move2.get_from()) as u8; + let dist1 = (move1.get_to() as i8 - move1.get_from() as i8).abs() as u8; + let dist2 = (move2.get_to() as i8 - move2.get_from() as i8).abs() as u8; print!("{}, {}, {}, {}", dist1, dist2, dice1, dice2); // basic : same number if cmp::min(dist1, dist2) != cmp::min(dice1, dice2) @@ -415,6 +415,10 @@ impl GameState { PlayerDisconnected { player_id } => { self.players.remove(player_id); } + Roll { player_id: 2 } => { + let dice = self.dice_roller.roll(); + self.consume(&GameEvent::RollResult { player_id: 2, dice }); + } Roll { player_id: _ } => {} RollResult { player_id: _, dice } => { self.dice = *dice; @@ -436,6 +440,7 @@ impl GameState { .find(|id| *id != player_id) .unwrap() .clone(); + self.turn_stage = TurnStage::RollDice; } } @@ -585,6 +590,13 @@ mod tests { CheckerMove::new(6, 9).unwrap(), ); assert!(!state.moves_possible(&Color::White, &moves)); + + // black moves + let moves = ( + CheckerMove::new(24, 20).unwrap(), + CheckerMove::new(20, 19).unwrap(), + ); + assert!(state.moves_possible(&Color::Black, &moves)); } #[test]