1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::board::*;
use crate::movement::*;
use crate::piece::*;
use crate::position::*;
use std::error;
use std::fmt;
#[derive(Debug, Clone)]
pub enum InvalidMoveError {
MoveSyntaxError,
OutOfBoardMoveError,
DestinationOccupiedError,
NoPieceAtPositionError,
NoPieceCapturedError,
PieceHasNoSuchMoveError,
NifuViolationError,
NoMovePossibleAfterDropError,
PromotionError,
UncoverCheckError,
CheckmateByPawnDropError,
}
#[allow(non_snake_case)]
#[allow(unused_variables)]
impl fmt::Display for InvalidMoveError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
InvalidMoveError::MoveSyntaxError => write!(f,"Move has an incorrect syntax"),
InvalidMoveError::OutOfBoardMoveError => write!(f,"The move uses squares outside of the board"),
InvalidMoveError::DestinationOccupiedError => write!(f,"The destination square is occupied"),
InvalidMoveError::NoPieceAtPositionError => write!(f,"No (such) piece was found at the start location"),
InvalidMoveError::NoPieceCapturedError => write!(f,"Capture was indicated but no piece was captured"),
InvalidMoveError::PieceHasNoSuchMoveError => write!(f,"The piece cannot move in such a way"),
InvalidMoveError::NifuViolationError => write!(f,"A pawn was dropped in a column already occupied by a non-promoted pawn"),
InvalidMoveError::NoMovePossibleAfterDropError => write!(f,"The piece was dropped in a position but will never be able to move afterwards"),
InvalidMoveError::PromotionError => write!(f,"The promotion of the piece is mandatory or impossible at this position but move do not provide the correct instruction"),
InvalidMoveError::UncoverCheckError => write!(f,"The king cannot be left in check with the current rules"),
InvalidMoveError::CheckmateByPawnDropError => write!(f,"A checkmate cannot be given by dropping a pawn")
}
}
}
impl error::Error for InvalidMoveError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
fn maybe_drop(mv: &str) -> bool {
if mv.len() == 4 && mv.as_bytes()[1] == b'*' {
true
} else {
false
}
}
fn maybe_normal_move(mv: &str) -> bool {
if ((mv.len() == 7 && mv.as_bytes()[6] == b'+') || (mv.len() == 6))
&& (mv.as_bytes()[3] == b'-' || mv.as_bytes()[3] == b'x')
{
true
} else {
false
}
}
pub fn check_syntax(mv: &str) -> Result<&str, InvalidMoveError> {
if !(maybe_drop(mv) || maybe_normal_move(mv)) {
return Err(InvalidMoveError::MoveSyntaxError);
}
let first_char = mv.chars().next().unwrap();
if first_char != 'P'
&& first_char != 'K'
&& first_char != 'R'
&& first_char != 'B'
&& first_char != 'G'
&& first_char != 'S'
&& first_char != 'N'
&& first_char != 'L'
{
return Err(InvalidMoveError::MoveSyntaxError);
}
return Ok(mv);
}
pub fn check_in_board(mv: &str) -> Result<&str, InvalidMoveError> {
let mut in_board = true;
if maybe_drop(mv) {
in_board &= mv.as_bytes()[2] as char >= '1' && mv.as_bytes()[2] as char <= '9';
in_board &= mv.as_bytes()[3] as char >= 'a' && mv.as_bytes()[3] as char <= 'i';
}
if maybe_normal_move(mv) {
in_board &= mv.as_bytes()[1] as char >= '1' && mv.as_bytes()[1] as char <= '9';
in_board &= mv.as_bytes()[2] as char >= 'a' && mv.as_bytes()[2] as char <= 'i';
in_board &= mv.as_bytes()[4] as char >= '1' && mv.as_bytes()[4] as char <= '9';
in_board &= mv.as_bytes()[5] as char >= 'a' && mv.as_bytes()[5] as char <= 'i';
}
if in_board {
Ok(mv)
} else {
Err(InvalidMoveError::OutOfBoardMoveError)
}
}
pub fn check_destination<'a>(mv: &'a str, b: &'a Board) -> Result<&'a str, InvalidMoveError> {
let full_move: Movement = mv.parse().unwrap();
let destination = full_move.end;
if full_move.start == None {
if None == b.is_occupied_by(destination) {
return Ok(mv);
} else {
return Err(InvalidMoveError::DestinationOccupiedError);
}
} else {
let current_player_color = b.get_color();
if full_move.force_capture {
if let Some(p) = b.is_occupied_by(destination) {
if p.color != current_player_color {
return Ok(mv);
}
}
return Err(InvalidMoveError::NoPieceCapturedError);
} else {
if let Some(p) = b.is_occupied_by(destination) {
if p.color == current_player_color {
return Err(InvalidMoveError::DestinationOccupiedError);
}
}
return Ok(mv);
}
}
}
pub fn check_start<'a>(mv: &'a str, b: &'a Board) -> Result<&'a str, InvalidMoveError> {
let full_move: Movement = mv.parse().unwrap();
let color = b.get_color();
if b.iter_pawns(color)
.find(|p| p.position == full_move.start && p.piecetype == full_move.piecetype)
== None
&& b.iter_pieces(color)
.find(|p| p.position == full_move.start && p.piecetype == full_move.piecetype)
== None
{
return Err(InvalidMoveError::NoPieceAtPositionError);
}
return Ok(mv);
}
fn small_move(start: Position, end: Position) -> bool {
let x = start.row() as i32 - end.row() as i32;
let y = start.column() as i32 - end.column() as i32;
return x.abs() <= 1 && y.abs() <= 1;
}
pub fn check_possible_move<'a>(mv: &'a str, b: &'a Board) -> Result<&'a str, InvalidMoveError> {
if maybe_drop(mv) {
return Ok(mv);
}
let full_move: Movement = mv.parse().unwrap();
let start = full_move.start.unwrap();
let piece = b.is_occupied_by(full_move.start.unwrap()).unwrap();
if !piece.get_relative_moves().into_iter().any(|relative_move| {
(relative_move.0 as i32, relative_move.1 as i32)
== (
(full_move.end.0 as i32 % 9 - start.0 as i32 % 9),
(full_move.end.0 as i32 / 9 - start.0 as i32 / 9),
)
}) {
return Err(InvalidMoveError::PieceHasNoSuchMoveError);
}
if full_move.piecetype == PieceType::Rook
&& !small_move(start, full_move.end)
&& !check_rook_path(start, full_move.end, b.clone())
{
return Err(InvalidMoveError::PieceHasNoSuchMoveError);
}
if full_move.piecetype == PieceType::Bishop
&& !small_move(start, full_move.end)
&& !check_bishop_path(start, full_move.end, b.clone())
{
return Err(InvalidMoveError::PieceHasNoSuchMoveError);
}
if full_move.piecetype == PieceType::Lance
&& !small_move(start, full_move.end)
&& !check_lance_path(start, full_move.end, b.clone())
{
return Err(InvalidMoveError::PieceHasNoSuchMoveError);
}
return Ok(mv);
}
fn check_bishop_path(start: Position, end: Position, b: Board) -> bool {
let direction = if (end.row() as u8 as i16 - start.row() as u8 as i16) > 0 {
if (end.column() as u8 as i16 - start.column() as u8 as i16) > 0 {
10
} else {
8
}
} else {
if (end.column() as u8 as i16 - start.column() as u8 as i16) > 0 {
-8
} else {
-10
}
};
let mut counter = start.0 as i32 + direction;
while counter != end.0 as i32 {
if !(None == b.is_occupied_by(Position(counter as u16))) {
return false;
}
counter += direction;
}
return true;
}
fn check_rook_path(start: Position, end: Position, b: Board) -> bool {
let direction;
if start.column() == end.column() {
direction = if end.0 > start.0 { 9 } else { -9 };
} else {
direction = if end.0 > start.0 { 1 } else { -1 };
}
let mut counter = start.0 as i32 + direction;
while counter != end.0 as i32 {
if !(None == b.is_occupied_by(Position(counter as u16))) {
return false;
}
counter += direction;
}
return true;
}
fn check_lance_path(start: Position, end: Position, b: Board) -> bool {
let direction = if end.0 > start.0 { 9 } else { -9 };
let mut counter = start.0 as i32 + direction;
while counter != end.0 as i32 {
if !(None == b.is_occupied_by(Position(counter as u16))) {
return false;
}
counter += direction;
}
return true;
}
pub fn check_nifu<'a>(mv: &'a str, b: &'a Board) -> Result<&'a str, InvalidMoveError> {
let full_move: Movement = mv.parse().unwrap();
if full_move.piecetype != PieceType::Pawn || full_move.start != None {
return Ok(mv);
}
if let Some(_) = b
.iter_pawns(b.get_color())
.filter(|p| p.position != None)
.find(|p| p.position.unwrap().0 % 9 == full_move.end.0 % 9)
{
return Err(InvalidMoveError::NifuViolationError);
}
return Ok(mv);
}
pub fn check_move_possible_after_drop<'a>(
mv: &'a str,
b: &'a Board,
) -> Result<&'a str, InvalidMoveError> {
if !maybe_drop(mv) {
return Ok(mv);
}
let last_row;
let before_last_row;
if b.get_color() == Color::White {
last_row = 'i';
before_last_row = 'h';
} else {
last_row = 'a';
before_last_row = 'b';
}
let full_move: Movement = mv.parse().unwrap();
if full_move.piecetype == PieceType::Pawn && full_move.end.row() == last_row {
return Err(InvalidMoveError::NoMovePossibleAfterDropError);
} else if full_move.piecetype == PieceType::Lance && full_move.end.row() == last_row {
return Err(InvalidMoveError::NoMovePossibleAfterDropError);
} else if full_move.piecetype == PieceType::Knight
&& (full_move.end.row() == last_row || full_move.end.row() == before_last_row)
{
return Err(InvalidMoveError::NoMovePossibleAfterDropError);
} else {
return Ok(mv);
}
}
pub fn check_promotion<'a>(mv: &'a str, b: &'a Board) -> Result<&'a str, InvalidMoveError> {
if !maybe_normal_move(mv) {
return Ok(mv);
}
let last_row;
let before_last_row;
let third_row;
if b.get_color() == Color::White {
last_row = 'i';
before_last_row = 'h';
third_row = 'g';
} else {
last_row = 'a';
before_last_row = 'b';
third_row = 'c';
}
let full_move: Movement = mv.parse().unwrap();
if full_move.promotion
&& (full_move.piecetype == PieceType::Gold || full_move.piecetype == PieceType::King)
{
return Err(InvalidMoveError::PromotionError);
}
if full_move.promotion {
if (full_move.end.row() != last_row
&& full_move.end.row() != before_last_row
&& full_move.end.row() != third_row)
&& (full_move.start.unwrap().row() != last_row
&& full_move.start.unwrap().row() != before_last_row
&& full_move.start.unwrap().row() != third_row)
{
return Err(InvalidMoveError::PromotionError);
}
return Ok(mv);
} else {
if let Some(piece) = b.is_occupied_by(full_move.start.unwrap()) {
if !piece.promoted {
if (full_move.piecetype == PieceType::Pawn && full_move.end.row() == last_row)
|| (full_move.piecetype == PieceType::Lance && full_move.end.row() == last_row)
|| (full_move.piecetype == PieceType::Knight
&& (full_move.end.row() == last_row
|| full_move.end.row() == before_last_row))
{
return Err(InvalidMoveError::PromotionError);
}
}
}
return Ok(mv);
}
}
pub fn check_uncover_check<'a>(mv: &'a str, b: &'a Board) -> Result<&'a str, InvalidMoveError> {
if b.rules.can_uncover_check {
return Ok(mv);
}
let my_color = b.get_color();
let mut opponent_color;
{
opponent_color = my_color;
opponent_color.invert();
}
if !b.contains(PieceType::King, opponent_color) {
return Ok(mv);
}
let board_after_my_move = b.play_move_unchecked(mv);
for opponent_move in board_after_my_move.iter_normal_moves_only(false) {
let future_board = board_after_my_move.play_move_unchecked(&opponent_move);
if !future_board.contains(PieceType::King, my_color) {
return Err(InvalidMoveError::UncoverCheckError);
}
}
return Ok(mv);
}
pub fn check_checkmate_by_pawn_drop<'a>(
mv: &'a str,
b: &'a Board,
) -> Result<&'a str, InvalidMoveError> {
let full_move: Movement = mv.parse().unwrap();
if !(None == full_move.start) || full_move.piecetype != PieceType::Pawn {
return Ok(mv);
}
let direction = if b.get_color() == Color::White { 9 } else { -9 };
if let Some(piece) = b.is_occupied_by(Position((full_move.end.0 as i32 + direction) as u16)) {
if piece.piecetype == PieceType::King && piece.color != b.get_color() {
} else {
return Ok(mv);
}
} else {
return Ok(mv);
}
let board_after_my_move = b.play_move_unchecked(mv);
if board_after_my_move.game_over() {
return Err(InvalidMoveError::CheckmateByPawnDropError);
} else {
Ok(mv)
}
}