]> git.sesse.net Git - remoteglot/blob - Board.pm
1bedd56a23d704f458384a858f69b4120cf652d4
[remoteglot] / Board.pm
1 #! /usr/bin/perl
2 #
3 # There are too many chess modules on CPAN already, so here's another one...
4 #
5 use strict;
6 use warnings;
7
8 package Board;
9
10 sub new {
11         my ($class, @rows) = @_;
12         my $board = [];
13
14         for my $row (0..7) {
15                 for my $col (0..7) {
16                         $board->[$row][$col] = substr($rows[$row], $col, 1);
17                 }
18         }
19
20         return bless $board;
21 }
22
23 sub clone {
24         my ($board) = shift;
25         my $nb = [];
26
27         for my $row (0..7) {
28                 $nb->[$row] = [ @{$board->[$row]} ];
29         }
30
31         return bless $nb;
32 }
33
34 # Returns a new board.
35 sub make_move {
36         my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
37         my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
38         my $piece = $board->[$from_row][$from_col];
39         my $nb = $board->clone();
40
41         if ($piece eq '-') {
42                 die "Invalid move $move";
43         }
44
45         # white short castling
46         if ($move eq 'e1g1' && $piece eq 'K') {
47                 # king
48                 $nb->[7][4] = '-';
49                 $nb->[7][6] = $piece;
50
51                 # rook
52                 $nb->[7][7] = '-';
53                 $nb->[7][5] = 'R';
54
55                 return $nb;
56         }
57
58         # white long castling
59         if ($move eq 'e1c1' && $piece eq 'K') {
60                 # king
61                 $nb->[7][4] = '-';
62                 $nb->[7][2] = $piece;
63
64                 # rook
65                 $nb->[7][0] = '-';
66                 $nb->[7][3] = 'R';
67
68                 return $nb;
69         }
70
71         # black short castling
72         if ($move eq 'e8g8' && $piece eq 'k') {
73                 # king
74                 $nb->[0][4] = '-';
75                 $nb->[0][6] = $piece;
76
77                 # rook
78                 $nb->[0][7] = '-';
79                 $nb->[0][5] = 'r';
80
81                 return $nb;
82         }
83
84         # black long castling
85         if ($move eq 'e8c8' && $piece eq 'k') {
86                 # king
87                 $nb->[0][4] = '-';
88                 $nb->[0][2] = $piece;
89
90                 # rook
91                 $nb->[0][0] = '-';
92                 $nb->[0][3] = 'r';
93
94                 return $nb;
95         }
96
97         # check if the from-piece is a pawn
98         if (lc($piece) eq 'p') {
99                 # attack?
100                 if ($from_col != $to_col) {
101                         # en passant?
102                         if ($board->[$to_row][$to_col] eq '-') {
103                                 if ($piece eq 'p') {
104                                         $nb->[$to_row - 1][$to_col] = '-';
105                                 } else {
106                                         $nb->[$to_row + 1][$to_col] = '-';
107                                 }
108                         }
109                 }
110                 if (defined($promo) && $promo ne '') {
111                         if ($piece eq 'p') {
112                                 $piece = lc($promo);
113                         } else {
114                                 $piece = uc($promo);
115                         }
116                 }
117         }
118
119         # update the board
120         $nb->[$from_row][$from_col] = '-';
121         $nb->[$to_row][$to_col] = $piece;
122
123         return $nb;
124 }
125
126 sub _pos_to_square {
127         my ($row, $col) = @_;
128         return sprintf("%c%d", ord('a') + $col, 8 - $row);
129 }
130
131 sub _col_letter_to_num {
132         return ord(shift) - ord('a');
133 }
134
135 sub _row_letter_to_num {
136         return 7 - (ord(shift) - ord('1'));
137 }
138
139 sub _square_to_pos {
140         my ($square) = @_;
141         $square =~ /^([a-h])([1-8])$/ or die "Invalid square $square";  
142         return (_row_letter_to_num($2), _col_letter_to_num($1));
143 }
144
145 sub move_to_uci_notation {
146         my ($from_row, $from_col, $to_row, $to_col, $promo) = @_;
147         $promo //= "";
148         return _pos_to_square($from_row, $from_col) . _pos_to_square($to_row, $to_col) . $promo;
149 }
150
151 sub parse_pretty_move {
152         my ($board, $move, $toplay) = @_;
153
154         # Strip check or mate
155         $move =~ s/[+#]$//;
156
157         if ($move eq '0-0' or $move eq 'O-O') {
158                 if ($toplay eq 'W') {
159                         return (_square_to_pos('e1'), _square_to_pos('g1'));
160                 } else {
161                         return (_square_to_pos('e8'), _square_to_pos('g8'));
162                 }
163         } elsif ($move eq '0-0-0' or $move eq 'O-O-O') {
164                 if ($toplay eq 'W') {
165                         return (_square_to_pos('e1'), _square_to_pos('c1'));
166                 } else {
167                         return (_square_to_pos('e8'), _square_to_pos('c8'));
168                 }
169         }
170
171         # Parse promo
172         my $promo;
173         if ($move =~ s/=([QRNB])$//) {
174                 $promo = $1;
175         }
176
177         $move =~ /^([KQRBN])?([a-h])?([1-8])?x?([a-h][1-8])$/ or die "Invalid move $move";
178         my $piece = $1 // 'P';
179         my $from_col = defined($2) ? _col_letter_to_num($2) : undef;
180         my $from_row = defined($3) ? _row_letter_to_num($3) : undef;
181         my ($to_row, $to_col) = _square_to_pos($4);
182         
183         # Find all possible from-squares that could have been meant.
184         my @squares = ();
185         my $side = 'K';
186         if ($toplay eq 'B') {
187                 $piece = lc($piece);
188                 $side = 'k';
189         }
190         for my $row (0..7) {
191                 next if (defined($from_row) && $from_row != $row);
192                 for my $col (0..7) {
193                         next if (defined($from_col) && $from_col != $col);
194                         next if ($board->[$row][$col] ne $piece);
195                         next if (!$board->can_reach($piece, $row, $col, $to_row, $to_col));
196
197                         # See if doing this move would put us in check
198                         # (yes, there are clients that expect us to do this).
199                         next if ($board->make_move($row, $col, $to_row, $to_col, $promo)->in_check($side));
200                         push @squares, [ $row, $col ];
201                 }
202         }
203         if (scalar @squares != 1) {
204                 die "Ambigious or impossible move $move";
205         }
206         return (@{$squares[0]}, $to_row, $to_col, $promo);
207 }
208
209 sub fen {
210         my ($board) = @_;
211         my @rows = ();
212         for my $row (0..7) {
213                 my $str = join('', @{$board->[$row]});
214                 $str =~ s/(-+)/length($1)/ge;
215                 push @rows, $str;
216         }
217
218         return join('/', @rows);
219 }
220
221 sub can_reach {
222         my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
223         
224         # can't eat your own piece
225         my $dest_piece = $board->[$to_row][$to_col];
226         if ($dest_piece ne '-') {
227                 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
228         }
229
230         if (lc($piece) eq 'k') {
231                 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
232         }
233         if (lc($piece) eq 'r') {
234                 return 0 unless ($from_row == $to_row || $from_col == $to_col);
235
236                 # check that there's a clear passage
237                 if ($from_row == $to_row) {
238                         if ($from_col > $to_col) {
239                                 ($to_col, $from_col) = ($from_col, $to_col);
240                         }
241
242                         for my $c (($from_col+1)..($to_col-1)) {
243                                 my $middle_piece = $board->[$to_row][$c];
244                                 return 0 if ($middle_piece ne '-');
245                         }
246
247                         return 1;
248                 } else {
249                         if ($from_row > $to_row) {
250                                 ($to_row, $from_row) = ($from_row, $to_row);
251                         }
252
253                         for my $r (($from_row+1)..($to_row-1)) {
254                                 my $middle_piece = $board->[$r][$to_col];
255                                 return 0 if ($middle_piece ne '-');     
256                         }
257
258                         return 1;
259                 }
260         }
261         if (lc($piece) eq 'b') {
262                 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
263
264                 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
265                 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
266
267                 my $r = $from_row + $dr;
268                 my $c = $from_col + $dc;
269
270                 while ($r != $to_row) {
271                         my $middle_piece = $board->[$r][$c];
272                         return 0 if ($middle_piece ne '-');
273                         
274                         $r += $dr;
275                         $c += $dc;
276                 }
277
278                 return 1;
279         }
280         if (lc($piece) eq 'n') {
281                 my $diff_r = abs($from_row - $to_row);
282                 my $diff_c = abs($from_col - $to_col);
283                 return 1 if ($diff_r == 2 && $diff_c == 1);
284                 return 1 if ($diff_r == 1 && $diff_c == 2);
285                 return 0;
286         }
287         if ($piece eq 'q') {
288                 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
289                         can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
290         }
291         if ($piece eq 'Q') {
292                 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
293                         can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
294         }
295
296         if ($piece eq 'p') {
297                 # black pawn
298                 if ($to_col == $from_col && $to_row == $from_row + 1) {
299                         return ($dest_piece eq '-');
300                 }
301                 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
302                         my $middle_piece = $board->[2][$to_col];
303                         return ($dest_piece eq '-' && $middle_piece eq '-');
304                 }
305                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
306                         if ($dest_piece eq '-') {
307                                 # En passant. TODO: check that the last move was indeed an EP move
308                                 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
309                         } else {
310                                 return 1;
311                         }
312                 }
313                 return 0;
314         }
315         if ($piece eq 'P') {
316                 # white pawn
317                 if ($to_col == $from_col && $to_row == $from_row - 1) {
318                         return ($dest_piece eq '-');
319                 }
320                 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
321                         my $middle_piece = $board->[5][$to_col];
322                         return ($dest_piece eq '-' && $middle_piece eq '-');
323                 }
324                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
325                         if ($dest_piece eq '-') {
326                                 # En passant. TODO: check that the last move was indeed an EP move
327                                 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
328                         } else {
329                                 return 1;
330                         }
331                 }
332                 return 0;
333         }
334         
335         # unknown piece
336         return 0;
337 }
338
339 # Returns whether the given side (given as k or K for black and white) is in check.
340 sub in_check {
341         my ($board, $piece) = @_;
342         my ($kr, $kc) = _find_piece($board, $piece);
343
344         # check all pieces for the possibility of threatening this king
345         for my $row (0..7) {
346                 for my $col (0..7) {
347                         my $piece = $board->[$row][$col];
348                         next if ($piece eq '-');
349                         return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
350                 }
351         }
352
353         return 0;
354 }
355
356 sub _find_piece {
357         my ($board, $piece) = @_;
358
359         for my $row (0..7) {
360                 next unless grep { $_ eq $piece } @{$board->[$row]};
361                 for my $col (0..7) {
362                         if ($board->[$row][$col] eq $piece) {
363                                 return ($row, $col);
364                         }
365                 }
366         }
367
368         return (undef, undef);
369 }
370
371 # Returns if the given side (given as k or K) is in mate.
372 sub in_mate {
373         my ($board, $side, $in_check) = @_;
374         return 0 if (!$in_check);
375
376         # try all possible moves for the side in check
377         for my $row (0..7) {
378                 for my $col (0..7) {
379                         my $piece = $board->[$row][$col];
380                         next if ($piece eq '-');
381
382                         if ($side eq 'K') {
383                                 next if ($piece eq lc($piece));
384                         } else {
385                                 next if ($piece eq uc($piece));
386                         }
387
388                         for my $dest_row (0..7) {
389                                 for my $dest_col (0..7) {
390                                         next if ($row == $dest_row && $col == $dest_col);
391                                         next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
392
393                                         my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
394                                         return 0 if (!$nb->in_check($side));
395                                 }
396                         }
397                 }
398         }
399
400         # nothing to do; mate
401         return 1;
402 }
403
404 # Returns the short algebraic form of the move, as well as the new position.
405 sub prettyprint_move {
406         my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
407         my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
408
409         my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
410
411         my $piece = $board->[$from_row][$from_col];
412         my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
413         my $in_check = $nb->in_check($other_side);
414         if ($nb->in_mate($other_side, $in_check)) {
415                 $pretty .= '#';
416         } elsif ($in_check) {
417                 $pretty .= '+';
418         }
419         return ($pretty, $nb);
420 }
421
422 sub _prettyprint_move_no_check_or_mate {
423         my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
424         my $piece = $board->[$from_row][$from_col];
425         my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
426
427         if ($piece eq '-') {
428                 die "Invalid move $move";
429         }
430
431         # white short castling
432         if ($move eq 'e1g1' && $piece eq 'K') {
433                 return '0-0';
434         }
435
436         # white long castling
437         if ($move eq 'e1c1' && $piece eq 'K') {
438                 return '0-0-0';
439         }
440
441         # black short castling
442         if ($move eq 'e8g8' && $piece eq 'k') {
443                 return '0-0';
444         }
445
446         # black long castling
447         if ($move eq 'e8c8' && $piece eq 'k') {
448                 return '0-0-0';
449         }
450
451         my $pretty;
452
453         # check if the from-piece is a pawn
454         if (lc($piece) eq 'p') {
455                 # attack?
456                 if ($from_col != $to_col) {
457                         $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
458                 } else {
459                         $pretty = _pos_to_square($to_row, $to_col);
460
461                         if (defined($promo) && $promo ne '') {
462                                 # promotion
463                                 $pretty .= "=";
464                                 $pretty .= $promo;
465                         }
466                 }
467                 return $pretty;
468         }
469
470         $pretty = uc($piece);
471
472         # see how many of these pieces could go here, in all
473         my $num_total = 0;
474         for my $col (0..7) {
475                 for my $row (0..7) {
476                         next unless ($board->[$row][$col] eq $piece);
477                         ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
478                 }
479         }
480
481         # see how many of these pieces from the given row could go here
482         my $num_row = 0;
483         for my $col (0..7) {
484                 next unless ($board->[$from_row][$col] eq $piece);
485                 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
486         }
487
488         # and same for columns
489         my $num_col = 0;
490         for my $row (0..7) {
491                 next unless ($board->[$row][$from_col] eq $piece);
492                 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
493         }
494
495         # see if we need to disambiguate
496         if ($num_total > 1) {
497                 if ($num_col == 1) {
498                         $pretty .= substr($move, 0, 1);
499                 } elsif ($num_row == 1) {
500                         $pretty .= substr($move, 1, 1);
501                 } else {
502                         $pretty .= substr($move, 0, 2);
503                 }
504         }
505
506         # attack?
507         if ($board->[$to_row][$to_col] ne '-') {
508                 $pretty .= 'x';
509         }
510
511         $pretty .= _pos_to_square($to_row, $to_col);
512         return $pretty;
513 }
514
515 1;