Skip to content

Commit 73556a0

Browse files
committed
feat: Improve hare movement logic and error handling
1 parent 7f1aa1b commit 73556a0

File tree

2 files changed

+27
-42
lines changed

2 files changed

+27
-42
lines changed

src/plugin/hare.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl Hare {
8383

8484
pub fn advance_by(&mut self, state: &mut GameState, distance: usize) -> Result<(), PyErr> {
8585
let new_position = self.position + distance;
86+
8687
RulesEngine::can_advance_to(
8788
&state.board,
8889
new_position,
@@ -92,14 +93,11 @@ impl Hare {
9293

9394
let needed_carrots = RulesEngine::calculates_carrots(distance);
9495

95-
if self.carrots - needed_carrots < 0 {
96-
return Err(MissingCarrotsError::new_err("Not enough carrots"));
97-
}
98-
9996
self.carrots -= needed_carrots;
10097
self.position = new_position;
10198

10299
state.update_player(self.clone());
100+
103101
Ok(())
104102
}
105103

src/plugin/rules_engine.rs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -95,61 +95,48 @@ impl RulesEngine {
9595
#[staticmethod]
9696
pub fn can_advance_to(
9797
board: &Board,
98-
new_position: usize,
98+
distance: usize,
9999
player: &Hare,
100100
other_player: &Hare,
101101
) -> Result<(), PyErr> {
102+
if distance == 0 {
103+
return Err(CannotEnterFieldError::new_err(
104+
"Advance distance cannot be 0",
105+
));
106+
}
107+
108+
let new_position = player.position + distance;
109+
102110
if new_position == 0 {
103111
return Err(CannotEnterFieldError::new_err("Cannot jump to position 0"));
104112
}
105113

114+
if player.carrots - Self::calculates_carrots(distance) < 0 {
115+
return Err(MissingCarrotsError::new_err("Not enough carrots"));
116+
}
117+
106118
Self::has_to_eat_salad(board, player)?;
107119

108-
let field = match board.get_field(new_position) {
109-
Some(f) => f,
110-
None => {
111-
return Err(CannotEnterFieldError::new_err("Field not found"));
112-
}
113-
};
120+
let field = board
121+
.get_field(new_position)
122+
.ok_or_else(|| CannotEnterFieldError::new_err("Field not found"))?;
114123

115124
if field != Field::Goal && new_position == other_player.position {
116125
return Err(FieldOccupiedError::new_err("Field is occupied by opponent"));
117126
}
118127

119128
match field {
120129
Field::Hedgehog => Err(HedgehogOnlyBackwardsError::new_err(
121-
"You cannot go on Hedgehog field forwards",
130+
"Cannot advance on Hedgehog field",
122131
)),
123-
Field::Salad => {
124-
if player.salads > 0 {
125-
Ok(())
126-
} else {
127-
Err(FieldOccupiedError::new_err("Field is occupied by opponent"))
128-
}
129-
}
130-
Field::Hare => {
131-
if !player.cards.is_empty() {
132-
Ok(())
133-
} else {
134-
Err(CardNotOwnedError::new_err("No card to play"))
135-
}
136-
}
137-
Field::Market => {
138-
if player.carrots >= 10 {
139-
Ok(())
140-
} else {
141-
Err(MissingCarrotsError::new_err("Not enough carrots"))
142-
}
143-
}
144-
Field::Goal => {
145-
if player.carrots <= 10 && player.salads == 0 {
146-
Ok(())
147-
} else {
148-
Err(GoalConditionsError::new_err(
149-
"Too much carrots or/and salads",
150-
))
151-
}
152-
}
132+
Field::Salad if player.salads > 0 => Ok(()),
133+
Field::Salad => Err(FieldOccupiedError::new_err("Field is occupied by opponent")),
134+
Field::Hare if !player.cards.is_empty() => Ok(()),
135+
Field::Hare => Err(CardNotOwnedError::new_err("No card to play")),
136+
Field::Market if player.carrots >= 10 => Ok(()),
137+
Field::Market => Err(MissingCarrotsError::new_err("Not enough carrots")),
138+
Field::Goal if player.carrots <= 10 && player.salads == 0 => Ok(()),
139+
Field::Goal => Err(GoalConditionsError::new_err("Too many carrots or salads")),
153140
_ => Ok(()),
154141
}
155142
}

0 commit comments

Comments
 (0)