Skip to content

Commit 119a580

Browse files
committed
chore: Add SwapCarrots additional logic
1 parent bd04d1a commit 119a580

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/plugin/action/card.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::plugin::{
99
hare::Hare,
1010
};
1111

12+
use super::Action;
13+
1214
#[pyclass]
1315
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash, Copy)]
1416
pub enum Card {
@@ -51,9 +53,40 @@ impl Card {
5153
current.move_to_field(state, other.position + 1)?;
5254
}
5355
Card::EatSalad => current.eat_salad(state)?,
54-
Card::SwapCarrots => swap(&mut current.carrots, &mut other.carrots),
55-
}
56+
Card::SwapCarrots => {
57+
let last_lettuce_position = state
58+
.board
59+
.get_previous_field(Field::Salad, state.board.track.len() - 1)
60+
.ok_or_else(|| {
61+
CannotPlayCardError::new_err(
62+
"Unable to find the last lettuce field position",
63+
)
64+
})?;
65+
66+
if current.position < last_lettuce_position {
67+
return Err(CannotPlayCardError::new_err(
68+
"You can only play this card if you are standing in front of the last lettuce field",
69+
));
70+
}
5671

72+
if let (Some(current_last_move), Some(other_last_move)) =
73+
(&current.last_move, &other.last_move)
74+
{
75+
if let (Action::Advance(current_advance), Action::Advance(other_advance)) =
76+
(&current_last_move.action, &other_last_move.action)
77+
{
78+
if current_advance.cards.contains(&Card::SwapCarrots)
79+
&& other_advance.cards.contains(&Card::SwapCarrots)
80+
{
81+
return Err(CannotPlayCardError::new_err(
82+
"You can only play this card if the last similar swap card was not used in one of the last two turns",
83+
));
84+
}
85+
}
86+
}
87+
swap(&mut current.carrots, &mut other.carrots);
88+
}
89+
}
5790
Ok(())
5891
}
5992

0 commit comments

Comments
 (0)