From c5321e6186807a39867d193dbda74ad22d07f751 Mon Sep 17 00:00:00 2001 From: Henri Bourcereau Date: Wed, 2 Oct 2024 18:03:44 +0200 Subject: [PATCH] feat: show last jans & moves --- client_cli/src/app.rs | 9 +++++++++ store/src/board.rs | 4 ++++ store/src/game.rs | 14 +++++++++----- store/src/game_rules_points.rs | 21 ++++++++++++++------- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/client_cli/src/app.rs b/client_cli/src/app.rs index 0486e86..c640565 100644 --- a/client_cli/src/app.rs +++ b/client_cli/src/app.rs @@ -230,6 +230,13 @@ impl App { output = output + "\nRolled dice : " + &self.game.state.dice.to_display_string(); if self.game.state.stage != Stage::PreGame { + output = output + "\nRolled dice jans : " + &format!("{:?}", self.game.state.dice_jans); + output = output + + "\nLast move : " + + &self.game.state.dice_moves.0.to_display_string() + + ", " + + &self.game.state.dice_moves.1.to_display_string(); + // display players points output += format!("\n\n{:<11} :: {:<5} :: {}", "Player", "holes", "points").as_str(); @@ -293,6 +300,8 @@ Rolled dice : 0 & 0 let expected = "------------------------------- InGame > myself > RollDice Rolled dice : 4 & 6 +Rolled dice jans : {} +Last move : CheckerMove { from: 24, to: 20 } , CheckerMove { from: 24, to: 18 } Player :: holes :: points 1. myself :: 0 :: 0 diff --git a/store/src/board.rs b/store/src/board.rs index 17f0c6a..4dd7415 100644 --- a/store/src/board.rs +++ b/store/src/board.rs @@ -35,6 +35,10 @@ impl Default for CheckerMove { } impl CheckerMove { + pub fn to_display_string(self) -> String { + format!("{:?} ", self) + } + pub fn new(from: Field, to: Field) -> Result { // println!("from {} to {}", from, to); // check if the field is on the board diff --git a/store/src/game.rs b/store/src/game.rs index 00d9bc5..6e8fa34 100644 --- a/store/src/game.rs +++ b/store/src/game.rs @@ -2,7 +2,7 @@ use crate::board::{Board, CheckerMove}; use crate::dice::Dice; use crate::game_rules_moves::MoveRules; -use crate::game_rules_points::PointsRules; +use crate::game_rules_points::{PointsRules, PossibleJans}; use crate::player::{Color, Player, PlayerId}; use log::error; @@ -45,6 +45,8 @@ pub struct GameState { pub dice: Dice, /// players points computed for the last dice pair rolled dice_points: (u8, u8), + pub dice_moves: (CheckerMove, CheckerMove), + pub dice_jans: PossibleJans, /// true if player needs to roll first roll_first: bool, // NOTE: add to a Setting struct if other fields needed @@ -77,6 +79,8 @@ impl Default for GameState { history: Vec::new(), dice: Dice::default(), dice_points: (0, 0), + dice_moves: (CheckerMove::default(), CheckerMove::default()), + dice_jans: PossibleJans::default(), roll_first: true, schools_enabled: false, } @@ -339,7 +343,6 @@ impl GameState { } let player_id = self.players.len() + 1; - println!("player_id {}", player_id); let color = if player_id == 1 { Color::White } else { @@ -419,7 +422,7 @@ impl GameState { self.dice = *dice; self.inc_roll_count(self.active_player_id); self.turn_stage = TurnStage::MarkPoints; - self.dice_points = self.get_rollresult_points(dice); + (self.dice_jans, self.dice_points) = self.get_rollresult_jans(dice); if !self.schools_enabled { // Schools are not enabled. We mark points automatically // the points earned by the opponent will be marked on its turn @@ -460,6 +463,7 @@ impl GameState { let player = self.players.get(player_id).unwrap(); self.board.move_checker(&player.color, moves.0).unwrap(); self.board.move_checker(&player.color, moves.1).unwrap(); + self.dice_moves = *moves; self.active_player_id = *self.players.keys().find(|id| *id != player_id).unwrap(); self.turn_stage = if self.schools_enabled { TurnStage::MarkAdvPoints @@ -490,10 +494,10 @@ impl GameState { self.board = Board::new(); } - fn get_rollresult_points(&self, dice: &Dice) -> (u8, u8) { + fn get_rollresult_jans(&self, dice: &Dice) -> (PossibleJans, (u8, u8)) { let player = &self.players.get(&self.active_player_id).unwrap(); let points_rules = PointsRules::new(&player.color, &self.board, *dice); - points_rules.get_points(player.dice_roll_count) + points_rules.get_result_jans(player.dice_roll_count) } /// Determines if someone has won the game diff --git a/store/src/game_rules_points.rs b/store/src/game_rules_points.rs index feb2b91..653cc59 100644 --- a/store/src/game_rules_points.rs +++ b/store/src/game_rules_points.rs @@ -1,14 +1,15 @@ -use std::cmp; -use std::collections::HashMap; - use crate::board::{Board, Field, EMPTY_MOVE}; -use crate::dice::{self, Dice}; +use crate::dice::Dice; use crate::game_rules_moves::MoveRules; use crate::player::Color; use crate::CheckerMove; use crate::Error; -#[derive(PartialEq, Eq, Hash, Clone, Debug)] +use serde::{Deserialize, Serialize}; +use std::cmp; +use std::collections::HashMap; + +#[derive(PartialEq, Eq, Hash, Clone, Debug, Serialize, Deserialize)] pub enum Jan { FilledQuarter, TrueHitSmallJan, @@ -61,9 +62,9 @@ impl Jan { } } -type PossibleJans = HashMap>; +pub type PossibleJans = HashMap>; -trait PossibleJansMethods { +pub trait PossibleJansMethods { fn push(&mut self, jan: Jan, cmoves: (CheckerMove, CheckerMove)); fn merge(&mut self, other: Self); // fn get_points(&self) -> u8; @@ -468,6 +469,12 @@ impl PointsRules { let jans = self.get_jans(&self.board, dice_rolls_count); self.get_jans_points(jans) } + + pub fn get_result_jans(&self, dice_rolls_count: u8) -> (PossibleJans, (u8, u8)) { + let jans = self.get_jans(&self.board, dice_rolls_count); + let points_jans = jans.clone(); + (jans, self.get_jans_points(points_jans)) + } } #[cfg(test)]