Skip to content

Commit 169459b

Browse files
authored
fix: enter goal bug in rules + tests (#90)
* fix: enter goal bug in rules + tests * fix: tests failing because of int conversion
1 parent fc7a51d commit 169459b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/plugin/rules_engine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl RulesEngine {
106106
if field != Field::Goal && new_position == other_player.position {
107107
return Err(HUIError::new_err("Field is occupied by opponent"));
108108
}
109-
109+
110110
match field {
111111
Field::Hedgehog => Err(HUIError::new_err("Cannot advance on Hedgehog field")),
112112
Field::Salad if player.salads > 0 => Ok(()),
@@ -115,7 +115,7 @@ impl RulesEngine {
115115
Field::Hare => Err(HUIError::new_err("No card to play")),
116116
Field::Market if player.carrots >= 10 && !cards.is_empty() => Ok(()),
117117
Field::Market => Err(HUIError::new_err("Not enough carrots or no card to play")),
118-
Field::Goal if player.carrots <= 10 && player.salads == 0 => Ok(()),
118+
Field::Goal if player.carrots - RulesEngine::calculates_carrots(distance as usize) <= 10 && player.salads == 0 => Ok(()),
119119
Field::Goal => Err(HUIError::new_err("Too many carrots or salads")),
120120
_ => Ok(()),
121121
}

src/plugin/test/rules_test.rs

+15
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ mod tests {
8585
assert!(RulesEngine::can_move_to(&board, 5, &player_one, &player_two, vec![]).is_err());
8686

8787
player_one.cards = vec![];
88+
player_one.carrots = 60;
8889
assert!(RulesEngine::can_move_to(&board, 6, &player_one, &player_two, vec![]).is_err());
90+
91+
// goal
92+
// too many salads and carrots
93+
assert!(RulesEngine::can_move_to(&board, 8, &player_one, &player_two, vec![]).is_err());
94+
// salads ok, too many carrots
95+
player_one.salads = 0;
96+
assert!(RulesEngine::can_move_to(&board, 8, &player_one, &player_two, vec![]).is_err());
97+
// too many salads, carrots ok
98+
player_one.carrots = 45;
99+
player_one.salads = 3;
100+
assert!(RulesEngine::can_move_to(&board, 8, &player_one, &player_two, vec![]).is_err());
101+
// all ok
102+
player_one.salads = 0;
103+
assert!(RulesEngine::can_move_to(&board, 8, &player_one, &player_two, vec![]).is_ok());
89104
}
90105
}

0 commit comments

Comments
 (0)