]> git.sesse.net Git - remoteglot/blob - Position.pm
Show highlight and arrows for history moves.
[remoteglot] / Position.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 use MIME::Base64;
8
9 require 'Board.pm';
10
11 package Position;
12
13 # Takes in a FICS style 12-type position.
14 sub new {
15         my ($class, $str) = @_;
16         my $pos = {};
17         my (@x) = split / /, $str;
18
19         $pos->{'board'} = Board->new(@x[1..8]);
20         $pos->{'toplay'} = $x[9];
21         $pos->{'ep_file_num'} = $x[10];
22         $pos->{'white_castle_k'} = $x[11];
23         $pos->{'white_castle_q'} = $x[12];
24         $pos->{'black_castle_k'} = $x[13];
25         $pos->{'black_castle_q'} = $x[14];
26         $pos->{'time_since_100move_rule_reset'} = $x[15];
27         $pos->{'player_w'} = $x[17];
28         $pos->{'player_b'} = $x[18];
29         $pos->{'player_w'} =~ s/^W?[FCIG]M//;
30         $pos->{'player_b'} =~ s/^W?[FCIG]M//;
31         $pos->{'move_num'} = $x[26];
32         if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
33                 $pos->{'last_move_uci'} = $1 . $2;
34         } else {
35                 $pos->{'last_move_uci'} = undef;
36         }
37         $pos->{'last_move'} = $x[29];
38         $pos->{'prettyprint_cache'} = {};
39
40         bless $pos, $class;
41         return $pos;
42 }
43
44 sub start_pos {
45         my ($class, $white, $black) = @_;
46         $white = "base64:" . MIME::Base64::encode_base64($white);
47         $black = "base64:" . MIME::Base64::encode_base64($black);
48         return $class->new("<12> rnbqkbnr pppppppp -------- -------- -------- -------- PPPPPPPP RNBQKBNR W -1 1 1 1 1 0 dummygamenum $white $black -2 dummytime dummyincrement 39 39 dummytime dummytime 1 none (0:00) none 0 0 0");
49 }
50
51 sub fen {
52         my $pos = shift;
53
54         # the board itself
55         my $fen = $pos->{'board'}->fen();
56
57         # white/black to move
58         $fen .= " ";
59         $fen .= lc($pos->{'toplay'});
60
61         # castling
62         my $castling = "";
63         $castling .= "K" if ($pos->{'white_castle_k'} == 1);
64         $castling .= "Q" if ($pos->{'white_castle_q'} == 1);
65         $castling .= "k" if ($pos->{'black_castle_k'} == 1);
66         $castling .= "q" if ($pos->{'black_castle_q'} == 1);
67         $castling = "-" if ($castling eq "");
68         # $castling = "-"; # chess960
69         $fen .= " ";
70         $fen .= $castling;
71
72         # en passant
73         my $ep = "-";
74         if ($pos->{'ep_file_num'} != -1) {
75                 my $col = $pos->{'ep_file_num'};
76                 my $nep = (qw(a b c d e f g h))[$col];
77
78                 if ($pos->{'toplay'} eq 'B') {
79                         $nep .= "3";
80                 } else {
81                         $nep .= "6";
82                 }
83
84                 #
85                 # Showing the en passant square when actually no capture can be made
86                 # seems to confuse at least Rybka. Thus, check if there's actually
87                 # a pawn of the opposite side that can do the en passant move, and if
88                 # not, just lie -- it doesn't matter anyway. I'm unsure what's the
89                 # "right" thing as per the standard, though.
90                 #
91                 if ($pos->{'toplay'} eq 'B') {
92                         $ep = $nep if ($col > 0 && $pos->{'board'}[4][$col-1] eq 'p');
93                         $ep = $nep if ($col < 7 && $pos->{'board'}[4][$col+1] eq 'p');
94                 } else {
95                         $ep = $nep if ($col > 0 && $pos->{'board'}[3][$col-1] eq 'P');
96                         $ep = $nep if ($col < 7 && $pos->{'board'}[3][$col+1] eq 'P');
97                 }
98         }
99         $fen .= " ";
100         $fen .= $ep;
101
102         # half-move clock
103         $fen .= " ";
104         $fen .= $pos->{'time_since_100move_rule_reset'};
105
106         # full-move clock
107         $fen .= " ";
108         $fen .= $pos->{'move_num'};
109
110         return $fen;
111 }
112
113 sub to_json_hash {
114         my $pos = shift;
115         my $json = { %$pos, fen => $pos->fen() };
116         delete $json->{'board'};
117         delete $json->{'prettyprint_cache'};
118         delete $json->{'black_castle_k'};
119         delete $json->{'black_castle_q'};
120         delete $json->{'white_castle_k'};
121         delete $json->{'white_castle_q'};
122         delete $json->{'time_since_100move_rule_reset'};
123         if ($json->{'player_w'} =~ /^base64:(.*)$/) {
124                 $json->{'player_w'} = MIME::Base64::decode_base64($1);
125         }
126         if ($json->{'player_b'} =~ /^base64:(.*)$/) {
127                 $json->{'player_b'} = MIME::Base64::decode_base64($1);
128         }
129         return $json;
130 }
131
132 sub parse_pretty_move {
133         my ($pos, $move) = @_;
134         return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
135 }
136
137 sub num_pieces {
138         my ($pos) = @_;
139         return $pos->{'board'}->num_pieces();
140 }
141
142 # Returns a new Position object.
143 sub make_move {
144         my ($pos, $from_row, $from_col, $to_row, $to_col, $promo) = @_;
145
146         my $from_square = _pos_to_square($from_row, $from_col);
147         my $to_square = _pos_to_square($to_row, $to_col);
148
149         my $np = {};
150         $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
151         if ($pos->{'toplay'} eq 'W') {
152                 $np->{'toplay'} = 'B';
153                 $np->{'move_num'} = $pos->{'move_num'};
154         } else {
155                 $np->{'toplay'} = 'W';
156                 $np->{'move_num'} = $pos->{'move_num'} + 1;
157         }
158
159         my $piece = $pos->{'board'}[$from_row][$from_col];
160         my $dest_piece = $pos->{'board'}[$to_row][$to_col];
161
162         # Find out if this was a two-step pawn move.
163         if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
164                 $np->{'ep_file_num'} = $from_col;
165         } else {
166                 $np->{'ep_file_num'} = -1;
167         }
168
169         # Castling rights.
170         $np->{'white_castle_k'} = $pos->{'white_castle_k'};
171         $np->{'white_castle_q'} = $pos->{'white_castle_q'};
172         $np->{'black_castle_k'} = $pos->{'black_castle_k'};
173         $np->{'black_castle_q'} = $pos->{'black_castle_q'};
174         if ($piece eq 'K') {
175                 $np->{'white_castle_k'} = 0;
176                 $np->{'white_castle_q'} = 0;
177         } elsif ($piece eq 'k') {
178                 $np->{'black_castle_k'} = 0;
179                 $np->{'black_castle_q'} = 0;
180         } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
181                 $np->{'white_castle_q'} = 0;
182         } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
183                 $np->{'white_castle_k'} = 0;
184         } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
185                 $np->{'black_castle_q'} = 0;
186         } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
187                 $np->{'black_castle_k'} = 0;
188         }
189
190         # 50-move rule.
191         if (lc($piece) eq 'p' || $dest_piece ne '-') {
192                 $np->{'time_since_100move_rule_reset'} = 0;
193         } else {
194                 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
195         }
196         $np->{'player_w'} = $pos->{'player_w'};
197         $np->{'player_b'} = $pos->{'player_b'};
198         my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
199         $np->{'last_move'} = $move;
200         $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
201
202         return bless $np;
203 }
204
205 # Returns a new Position object, and the parsed UCI move.
206 sub make_pretty_move {
207         my ($pos, $move) = @_;
208
209         my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
210         my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
211         $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
212         return ($pos, $uci_move);
213 }
214
215 sub _pos_to_square {
216         my ($row, $col) = @_;
217         return sprintf("%c%d", ord('a') + $col, 8 - $row);
218 }
219
220 sub apply_uci_pv {
221         my ($pos, @pv) = @_;
222
223         my $pvpos = $pos;
224         for my $pv_move (@pv) {
225                 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
226                 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
227         }
228
229         return $pvpos;
230 }
231
232 sub _col_letter_to_num {
233         return ord(shift) - ord('a');
234 }
235
236 sub _row_letter_to_num {
237         return 7 - (ord(shift) - ord('1'));
238 }
239
240 sub _parse_uci_move {
241         my $move = shift;
242         my $from_col = _col_letter_to_num(substr($move, 0, 1));
243         my $from_row = _row_letter_to_num(substr($move, 1, 1));
244         my $to_col   = _col_letter_to_num(substr($move, 2, 1));
245         my $to_row   = _row_letter_to_num(substr($move, 3, 1));
246         my $promo    = substr($move, 4, 1);
247         return ($from_row, $from_col, $to_row, $to_col, $promo);
248 }
249
250 1;