]> git.sesse.net Git - remoteglot/blob - Board.pm
Remove unused JavaScript.
[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 == 0) {
204                 die "Impossible move $move";
205         }
206         if (scalar @squares != 1) {
207                 die "Ambigious move $move";
208         }
209         return (@{$squares[0]}, $to_row, $to_col, $promo);
210 }
211
212 sub fen {
213         my ($board) = @_;
214         my @rows = ();
215         for my $row (0..7) {
216                 my $str = join('', @{$board->[$row]});
217                 $str =~ s/(-+)/length($1)/ge;
218                 push @rows, $str;
219         }
220
221         return join('/', @rows);
222 }
223
224 sub can_reach {
225         my ($board, $piece, $from_row, $from_col, $to_row, $to_col) = @_;
226         
227         # can't eat your own piece
228         my $dest_piece = $board->[$to_row][$to_col];
229         if ($dest_piece ne '-') {
230                 return 0 if (($piece eq lc($piece)) == ($dest_piece eq lc($dest_piece)));
231         }
232         
233         if ($piece eq 'p') {
234                 # black pawn
235                 if ($to_col == $from_col && $to_row == $from_row + 1) {
236                         return ($dest_piece eq '-');
237                 }
238                 if ($to_col == $from_col && $from_row == 1 && $to_row == 3) {
239                         my $middle_piece = $board->[2][$to_col];
240                         return ($dest_piece eq '-' && $middle_piece eq '-');
241                 }
242                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row + 1) {
243                         if ($dest_piece eq '-') {
244                                 # En passant. TODO: check that the last move was indeed an EP move
245                                 return ($to_row == 5 && $board->[4][$to_col] eq 'P');
246                         } else {
247                                 return 1;
248                         }
249                 }
250                 return 0;
251         }
252         if ($piece eq 'P') {
253                 # white pawn
254                 if ($to_col == $from_col && $to_row == $from_row - 1) {
255                         return ($dest_piece eq '-');
256                 }
257                 if ($to_col == $from_col && $from_row == 6 && $to_row == 4) {
258                         my $middle_piece = $board->[5][$to_col];
259                         return ($dest_piece eq '-' && $middle_piece eq '-');
260                 }
261                 if (abs($to_col - $from_col) == 1 && $to_row == $from_row - 1) {
262                         if ($dest_piece eq '-') {
263                                 # En passant. TODO: check that the last move was indeed an EP move
264                                 return ($to_row == 2 && $board->[3][$to_col] eq 'p');
265                         } else {
266                                 return 1;
267                         }
268                 }
269                 return 0;
270         }
271         
272         if (lc($piece) eq 'r') {
273                 return 0 unless ($from_row == $to_row || $from_col == $to_col);
274
275                 # check that there's a clear passage
276                 if ($from_row == $to_row) {
277                         if ($from_col > $to_col) {
278                                 ($to_col, $from_col) = ($from_col, $to_col);
279                         }
280
281                         for my $c (($from_col+1)..($to_col-1)) {
282                                 my $middle_piece = $board->[$to_row][$c];
283                                 return 0 if ($middle_piece ne '-');
284                         }
285
286                         return 1;
287                 } else {
288                         if ($from_row > $to_row) {
289                                 ($to_row, $from_row) = ($from_row, $to_row);
290                         }
291
292                         for my $r (($from_row+1)..($to_row-1)) {
293                                 my $middle_piece = $board->[$r][$to_col];
294                                 return 0 if ($middle_piece ne '-');     
295                         }
296
297                         return 1;
298                 }
299         }
300         if (lc($piece) eq 'b') {
301                 return 0 unless (abs($from_row - $to_row) == abs($from_col - $to_col));
302
303                 my $dr = ($to_row - $from_row) / abs($to_row - $from_row);
304                 my $dc = ($to_col - $from_col) / abs($to_col - $from_col);
305
306                 my $r = $from_row + $dr;
307                 my $c = $from_col + $dc;
308
309                 while ($r != $to_row) {
310                         my $middle_piece = $board->[$r][$c];
311                         return 0 if ($middle_piece ne '-');
312                         
313                         $r += $dr;
314                         $c += $dc;
315                 }
316
317                 return 1;
318         }
319         if (lc($piece) eq 'n') {
320                 my $diff_r = abs($from_row - $to_row);
321                 my $diff_c = abs($from_col - $to_col);
322                 return 1 if ($diff_r == 2 && $diff_c == 1);
323                 return 1 if ($diff_r == 1 && $diff_c == 2);
324                 return 0;
325         }
326         if ($piece eq 'q') {
327                 return (can_reach($board, 'r', $from_row, $from_col, $to_row, $to_col) ||
328                         can_reach($board, 'b', $from_row, $from_col, $to_row, $to_col));
329         }
330         if ($piece eq 'Q') {
331                 return (can_reach($board, 'R', $from_row, $from_col, $to_row, $to_col) ||
332                         can_reach($board, 'B', $from_row, $from_col, $to_row, $to_col));
333         }
334         if (lc($piece) eq 'k') {
335                 return (abs($from_row - $to_row) <= 1 && abs($from_col - $to_col) <= 1);
336         }
337
338         # unknown piece
339         return 0;
340 }
341
342 my %pieces_against_side = (
343         k => { K => 1, Q => 1, R => 1, N => 1, B => 1, P => 1 },
344         K => { k => 1, q => 1, r => 1, n => 1, b => 1, p => 1 },
345 );
346
347 # Returns whether the given side (given as k or K for black and white) is in check.
348 sub in_check {
349         my ($board, $side) = @_;
350         my ($kr, $kc) = _find_piece($board, $side);
351
352         # check all pieces for the possibility of threatening this king
353         for my $row (0..7) {
354                 next unless grep { exists($pieces_against_side{$side}{$_}) } @{$board->[$row]};
355                 for my $col (0..7) {
356                         my $piece = $board->[$row][$col];
357                         next if ($piece eq '-');
358                         return 1 if ($board->can_reach($piece, $row, $col, $kr, $kc));
359                 }
360         }
361
362         return 0;
363 }
364
365 sub _find_piece {
366         my ($board, $piece) = @_;
367
368         for my $row (0..7) {
369                 next unless grep { $_ eq $piece } @{$board->[$row]};
370                 for my $col (0..7) {
371                         if ($board->[$row][$col] eq $piece) {
372                                 return ($row, $col);
373                         }
374                 }
375         }
376
377         return (undef, undef);
378 }
379
380 # Returns if the given side (given as k or K) is in mate.
381 sub in_mate {
382         my ($board, $side, $in_check) = @_;
383         return 0 if (!$in_check);
384
385         # try all possible moves for the side in check
386         for my $row (0..7) {
387                 for my $col (0..7) {
388                         my $piece = $board->[$row][$col];
389                         next if ($piece eq '-');
390
391                         if ($side eq 'K') {
392                                 next if ($piece eq lc($piece));
393                         } else {
394                                 next if ($piece eq uc($piece));
395                         }
396
397                         for my $dest_row (0..7) {
398                                 for my $dest_col (0..7) {
399                                         next if ($row == $dest_row && $col == $dest_col);
400                                         next unless ($board->can_reach($piece, $row, $col, $dest_row, $dest_col));
401
402                                         my $nb = $board->make_move($row, $col, $dest_row, $dest_col);
403                                         return 0 if (!$nb->in_check($side));
404                                 }
405                         }
406                 }
407         }
408
409         # nothing to do; mate
410         return 1;
411 }
412
413 # Returns the short algebraic form of the move, as well as the new position.
414 sub prettyprint_move {
415         my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
416         my $pretty = $board->_prettyprint_move_no_check_or_mate($from_row, $from_col, $to_row, $to_col, $promo);
417
418         my $nb = $board->make_move($from_row, $from_col, $to_row, $to_col, $promo);
419
420         my $piece = $board->[$from_row][$from_col];
421         my $other_side = (uc($piece) eq $piece) ? 'k' : 'K';
422         my $in_check = $nb->in_check($other_side);
423         if ($nb->in_mate($other_side, $in_check)) {
424                 $pretty .= '#';
425         } elsif ($in_check) {
426                 $pretty .= '+';
427         }
428         return ($pretty, $nb);
429 }
430
431 sub num_pieces {
432         my ($board) = @_;
433
434         my $num = 0;
435         for my $row (0..7) {
436                 for my $col (0..7) {
437                         my $piece = $board->[$row][$col];
438                         ++$num if ($piece ne '-');
439                 }
440         }
441         return $num;    
442 }
443
444 sub _prettyprint_move_no_check_or_mate {
445         my ($board, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
446         my $piece = $board->[$from_row][$from_col];
447         my $move = move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
448
449         if ($piece eq '-') {
450                 die "Invalid move $move";
451         }
452
453         # white short castling
454         if ($move eq 'e1g1' && $piece eq 'K') {
455                 return '0-0';
456         }
457
458         # white long castling
459         if ($move eq 'e1c1' && $piece eq 'K') {
460                 return '0-0-0';
461         }
462
463         # black short castling
464         if ($move eq 'e8g8' && $piece eq 'k') {
465                 return '0-0';
466         }
467
468         # black long castling
469         if ($move eq 'e8c8' && $piece eq 'k') {
470                 return '0-0-0';
471         }
472
473         my $pretty;
474
475         # check if the from-piece is a pawn
476         if (lc($piece) eq 'p') {
477                 # attack?
478                 if ($from_col != $to_col) {
479                         $pretty = substr($move, 0, 1) . 'x' . _pos_to_square($to_row, $to_col);
480                 } else {
481                         $pretty = _pos_to_square($to_row, $to_col);
482
483                         if (defined($promo) && $promo ne '') {
484                                 # promotion
485                                 $pretty .= "=";
486                                 $pretty .= uc($promo);
487                         }
488                 }
489                 return $pretty;
490         }
491
492         $pretty = uc($piece);
493
494         # see how many of these pieces could go here, in all
495         my $num_total = 0;
496         for my $col (0..7) {
497                 for my $row (0..7) {
498                         next unless ($board->[$row][$col] eq $piece);
499                         ++$num_total if ($board->can_reach($piece, $row, $col, $to_row, $to_col));
500                 }
501         }
502
503         # see how many of these pieces from the given row could go here
504         my $num_row = 0;
505         for my $col (0..7) {
506                 next unless ($board->[$from_row][$col] eq $piece);
507                 ++$num_row if ($board->can_reach($piece, $from_row, $col, $to_row, $to_col));
508         }
509
510         # and same for columns
511         my $num_col = 0;
512         for my $row (0..7) {
513                 next unless ($board->[$row][$from_col] eq $piece);
514                 ++$num_col if ($board->can_reach($piece, $row, $from_col, $to_row, $to_col));
515         }
516
517         # see if we need to disambiguate
518         if ($num_total > 1) {
519                 if ($num_col == 1) {
520                         $pretty .= substr($move, 0, 1);
521                 } elsif ($num_row == 1) {
522                         $pretty .= substr($move, 1, 1);
523                 } else {
524                         $pretty .= substr($move, 0, 2);
525                 }
526         }
527
528         # attack?
529         if ($board->[$to_row][$to_col] ne '-') {
530                 $pretty .= 'x';
531         }
532
533         $pretty .= _pos_to_square($to_row, $to_col);
534         return $pretty;
535 }
536
537 1;