From 7e114300c781bc7978921bd7ab8368076bee82d8 Mon Sep 17 00:00:00 2001 From: YoEnte Date: Mon, 20 Jan 2025 21:26:28 +0100 Subject: [PATCH 1/3] fix: perform_move() bug Fix: check which player is in front. Add: check if player for update_carrot() is even on turn. --- src/plugin/game_state.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/plugin/game_state.rs b/src/plugin/game_state.rs index c5341ce..8b63043 100644 --- a/src/plugin/game_state.rs +++ b/src/plugin/game_state.rs @@ -48,23 +48,26 @@ impl GameState { Some(Field::Position1) if player.position > opponent_position => { player.carrots += 10 } - Some(Field::Position2) if player.position > opponent_position => { + Some(Field::Position2) if player.position < opponent_position => { player.carrots += 30 } _ => {} } } - update_carrots( - &mut new_state.player_one, - new_state.player_two.position, - &new_state.board, - ); - update_carrots( - &mut new_state.player_two, - new_state.player_one.position, - &new_state.board, - ); + if new_state.turn % 2 == 0 { + update_carrots( + &mut new_state.player_one, + new_state.player_two.position, + &new_state.board, + ); + } else { + update_carrots( + &mut new_state.player_two, + new_state.player_one.position, + &new_state.board, + ); + } Ok(new_state) } From 0c440ac32689bcf278249f43e597cee2baa92d0d Mon Sep 17 00:00:00 2001 From: YoEnte Date: Mon, 20 Jan 2025 21:34:27 +0100 Subject: [PATCH 2/3] add: GameState.last_move for perform fn --- src/plugin/move.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugin/move.rs b/src/plugin/move.rs index db2fe0b..4960bbd 100644 --- a/src/plugin/move.rs +++ b/src/plugin/move.rs @@ -22,6 +22,7 @@ impl Move { if result.is_ok() { let mut player = state.clone_current_player(); player.last_move = Some(self.clone()); + state.last_move = Some(self.clone()); state.update_player(player); } result From 769ec8eccd9fb04d6ab322b46c673ddc4d56a187 Mon Sep 17 00:00:00 2001 From: YoEnte Date: Tue, 21 Jan 2025 00:37:35 +0100 Subject: [PATCH 3/3] add: test for fix modify: test board to accommodate old and new tests, because salad field on 1 was a bit misplaced --- src/plugin/test/state_test.rs | 88 +++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 3 deletions(-) diff --git a/src/plugin/test/state_test.rs b/src/plugin/test/state_test.rs index c1eb46c..dbc39f8 100644 --- a/src/plugin/test/state_test.rs +++ b/src/plugin/test/state_test.rs @@ -9,6 +9,7 @@ mod tests { game_state::GameState, hare::{Hare, TeamEnum}, r#move::Move, + rules_engine::RulesEngine, }; fn create_player( @@ -31,13 +32,15 @@ mod tests { fn create_board() -> Board { Board::new(vec![ Field::Start, - Field::Salad, + Field::Carrots, Field::Position2, Field::Hare, - Field::Carrots, - Field::Market, Field::Position1, + Field::Market, + Field::Carrots, Field::Hare, + Field::Hedgehog, + Field::Salad, Field::Goal, ]) } @@ -80,4 +83,83 @@ mod tests { vec![Card::HurryAhead, Card::FallBack, Card::EatSalad] ))))); } + + #[test] + fn test_correct_carrot_update() { + let state_depth_0 = GameState::new( + create_board(), + 0, + create_player(TeamEnum::One, 0, vec![], 75, 0), + create_player(TeamEnum::Two, 0, vec![], 200, 0), + None, + ); + + // perform all poss moves for current player ("A") + let moves_depth_0 = state_depth_0.possible_moves(); + for move_depth_0 in moves_depth_0.iter() { + let depth_1 = state_depth_0.perform_move(move_depth_0); + assert!(depth_1.is_ok()); + + match depth_1 { + Ok(state_depth_1) => { + let moves_depth_1 = state_depth_1.possible_moves(); + let ref move_first_depth_1 = moves_depth_1[0]; + let ref move_last_depth_1 = moves_depth_1[moves_depth_1.len() - 1]; + + // performed player "A" on Pos1 or Pos2 Field -> calculate next depth (player B) + let on_pos1 = state_depth_1.board.get_field(state_depth_1.clone_other_player().position) == Some(Field::Position1); + let on_pos2 = state_depth_1.board.get_field(state_depth_1.clone_other_player().position) == Some(Field::Position2); + if on_pos1 || on_pos2 { + + let moved_distance = match &move_depth_0.action { + Action::Advance(advance) => advance.distance, + _ => 0, + }; + + let expected_carrots = state_depth_0.clone_current_player().carrots - RulesEngine::calculates_carrots(moved_distance); + + // player "A" should be missing the exact carrot amount for the distance + assert_eq!(expected_carrots, state_depth_1.clone_other_player().carrots); + + // first (shortest) poss move of player "B" gets performed -> A is (with this board) in front + let depth_2_first = state_depth_1.perform_move(move_first_depth_1); + assert!(depth_2_first.is_ok()); + match depth_2_first { + Ok(state_depth_2_first) => { + // "A" got the 10 ten extra carrots if on pos1 field and in front + if on_pos1 { + assert_eq!(expected_carrots + 10, state_depth_2_first.clone_current_player().carrots); + } + + // no carrots should have been added to "A" if on pos2 field and in front + if on_pos2 { + assert_eq!(expected_carrots, state_depth_2_first.clone_current_player().carrots); + } + } + Err(e) => println!("Error {e}") + } + + // last (farthest) poss move of player "B" gets performed -> A is (with this board) behind + let depth_2_last = state_depth_1.perform_move(move_last_depth_1); + assert!(depth_2_last.is_ok()); + match depth_2_last { + Ok(state_depth_2_last) => { + // no carrots should have been added to "A" if on pos1 field and behind + if on_pos1 { + assert_eq!(expected_carrots, state_depth_2_last.clone_current_player().carrots); + } + + // "A" got the 30 ten extra carrots if on pos2 field and behind + if on_pos2 { + assert_eq!(expected_carrots + 30, state_depth_2_last.clone_current_player().carrots); + } + } + Err(e) => println!("Error {e}") + } + } + }, + Err(e) => println!("Error {e}") + } + } + } }