@@ -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