Skip to content

Commit 7956328

Browse files
committed
fix: Handle entering fields without any cards in Advance action
1 parent 5aa870e commit 7956328

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/plugin/action/advance.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use pyo3::{pyclass, pymethods, PyErr};
22

33
use crate::plugin::{
4-
errors::{CannotPlayCardError, MustBuyOneCardError},
4+
errors::{CannotPlayCardError, MustBuyOneCardError, MustPlayCardError},
55
field::Field,
66
game_state::GameState,
77
};
@@ -30,15 +30,27 @@ impl Advance {
3030

3131
player.advance_by(state, self.distance)?;
3232

33+
let current_field = state.board.get_field(player.position).unwrap();
34+
if self.cards.is_empty() {
35+
match current_field {
36+
Field::Market | Field::Hare => {
37+
return Err(MustPlayCardError::new_err(
38+
"Cannot enter field without any cards",
39+
));
40+
}
41+
_ => {}
42+
}
43+
}
44+
3345
let mut last_card: Option<&Card> = None;
3446
let mut card_bought = false;
3547

3648
for card in &self.cards {
37-
match state.board.get_field(player.position).unwrap() {
49+
match current_field {
50+
Field::Market if card_bought => {
51+
return Err(MustBuyOneCardError::new_err("Only one card allowed to buy"));
52+
}
3853
Field::Market => {
39-
if card_bought {
40-
return Err(MustBuyOneCardError::new_err("Only one card allowed to buy"));
41-
}
4254
player.consume_carrots(state, 10)?;
4355
card_bought = true;
4456
player.cards.push(*card);

src/plugin/test/advance_test.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,48 @@ mod tests {
6262
}
6363

6464
#[test]
65-
fn test_perform_buy_card_sucess() {
65+
fn test_perform_success_without_cards() {
66+
let board = Board::new(vec![
67+
Field::Start,
68+
Field::Position1,
69+
Field::Position2,
70+
Field::Hare,
71+
Field::Hedgehog,
72+
Field::Market,
73+
Field::Hare,
74+
Field::Position1,
75+
Field::Goal,
76+
]);
77+
let player_one = Hare::new(
78+
TeamEnum::One,
79+
Some(vec![Card::FallBack, Card::EatSalad, Card::SwapCarrots]),
80+
Some(60),
81+
Some(3),
82+
None,
83+
Some(0),
84+
);
85+
let player_two = Hare::new(
86+
TeamEnum::Two,
87+
Some(vec![Card::HurryAhead]),
88+
Some(60),
89+
Some(3),
90+
None,
91+
Some(0),
92+
);
93+
94+
let mut state = GameState::new(board, 0, player_one, player_two);
95+
96+
let advance = Advance::new(2, vec![]);
97+
98+
let result = advance.perform(&mut state);
99+
assert!(result.is_ok());
100+
101+
let current_player = state.clone_current_player();
102+
assert_eq!(current_player.position, 2);
103+
}
104+
105+
#[test]
106+
fn test_perform_buy_card_success() {
66107
let cards = vec![Card::HurryAhead];
67108
let advance = Advance::new(2, cards.clone());
68109

0 commit comments

Comments
 (0)