wip rolldice

This commit is contained in:
Henri Bourcereau 2024-03-27 21:10:15 +01:00
parent dff9f7f3e1
commit 1f663cf524
4 changed files with 21 additions and 6 deletions

View file

@ -46,7 +46,8 @@ impl Bot {
pub fn consume(&mut self, event: &GameEvent) -> Option<GameEvent> { pub fn consume(&mut self, event: &GameEvent) -> Option<GameEvent> {
self.game.consume(event); 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 { if self.game.active_player_id == self.player_id {
return match self.game.turn_stage { return match self.game.turn_stage {
TurnStage::RollDice => Some(GameEvent::Roll { TurnStage::RollDice => Some(GameEvent::Roll {

View file

@ -43,6 +43,7 @@ impl Game {
pub fn consume(&mut self, event: &GameEvent) -> Option<GameEvent> { pub fn consume(&mut self, event: &GameEvent) -> Option<GameEvent> {
if self.state.validate(event) { if self.state.validate(event) {
println!("consuming {:?}", event);
self.state.consume(event); self.state.consume(event);
return self return self
.bot .bot

View file

@ -35,9 +35,10 @@ impl CheckerMove {
return Err(Error::FieldInvalid); return Err(Error::FieldInvalid);
} }
// check that the destination is after the origin field // check that the destination is after the origin field
if to < from && to != 0 { // --> not applicable for black moves
return Err(Error::MoveInvalid); // if to < from && to != 0 {
} // return Err(Error::MoveInvalid);
// }
Ok(Self { from, to }) Ok(Self { from, to })
} }

View file

@ -288,8 +288,8 @@ impl GameState {
fn moves_follows_dices(&self, color: &Color, moves: &(CheckerMove, CheckerMove)) -> bool { fn moves_follows_dices(&self, color: &Color, moves: &(CheckerMove, CheckerMove)) -> bool {
let (dice1, dice2) = self.dice.values; let (dice1, dice2) = self.dice.values;
let (move1, move2): &(CheckerMove, CheckerMove) = moves.into(); let (move1, move2): &(CheckerMove, CheckerMove) = moves.into();
let dist1 = (move1.get_to() - move1.get_from()) as u8; let dist1 = (move1.get_to() as i8 - move1.get_from() as i8).abs() as u8;
let dist2 = (move2.get_to() - move2.get_from()) as u8; let dist2 = (move2.get_to() as i8 - move2.get_from() as i8).abs() as u8;
print!("{}, {}, {}, {}", dist1, dist2, dice1, dice2); print!("{}, {}, {}, {}", dist1, dist2, dice1, dice2);
// basic : same number // basic : same number
if cmp::min(dist1, dist2) != cmp::min(dice1, dice2) if cmp::min(dist1, dist2) != cmp::min(dice1, dice2)
@ -415,6 +415,10 @@ impl GameState {
PlayerDisconnected { player_id } => { PlayerDisconnected { player_id } => {
self.players.remove(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: _ } => {} Roll { player_id: _ } => {}
RollResult { player_id: _, dice } => { RollResult { player_id: _, dice } => {
self.dice = *dice; self.dice = *dice;
@ -436,6 +440,7 @@ impl GameState {
.find(|id| *id != player_id) .find(|id| *id != player_id)
.unwrap() .unwrap()
.clone(); .clone();
self.turn_stage = TurnStage::RollDice;
} }
} }
@ -585,6 +590,13 @@ mod tests {
CheckerMove::new(6, 9).unwrap(), CheckerMove::new(6, 9).unwrap(),
); );
assert!(!state.moves_possible(&Color::White, &moves)); 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] #[test]