@@ -95,61 +95,48 @@ impl RulesEngine {
95
95
#[ staticmethod]
96
96
pub fn can_advance_to (
97
97
board : & Board ,
98
- new_position : usize ,
98
+ distance : usize ,
99
99
player : & Hare ,
100
100
other_player : & Hare ,
101
101
) -> 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
+
102
110
if new_position == 0 {
103
111
return Err ( CannotEnterFieldError :: new_err ( "Cannot jump to position 0" ) ) ;
104
112
}
105
113
114
+ if player. carrots - Self :: calculates_carrots ( distance) < 0 {
115
+ return Err ( MissingCarrotsError :: new_err ( "Not enough carrots" ) ) ;
116
+ }
117
+
106
118
Self :: has_to_eat_salad ( board, player) ?;
107
119
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" ) ) ?;
114
123
115
124
if field != Field :: Goal && new_position == other_player. position {
116
125
return Err ( FieldOccupiedError :: new_err ( "Field is occupied by opponent" ) ) ;
117
126
}
118
127
119
128
match field {
120
129
Field :: Hedgehog => Err ( HedgehogOnlyBackwardsError :: new_err (
121
- "You cannot go on Hedgehog field forwards " ,
130
+ "Cannot advance on Hedgehog field" ,
122
131
) ) ,
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" ) ) ,
153
140
_ => Ok ( ( ) ) ,
154
141
}
155
142
}
0 commit comments