Remove bitpacked_fen, which is only really in use for the book (which is forked off...
[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->{'white_clock'} = $x[24];
32         $pos->{'black_clock'} = $x[25];
33         $pos->{'move_num'} = $x[26];
34         if ($x[27] =~ /([a-h][1-8])-([a-h][1-8])/) {
35                 $pos->{'last_move_uci'} = $1 . $2;
36         } else {
37                 $pos->{'last_move_uci'} = undef;
38         }
39         $pos->{'last_move'} = $x[29];
40         $pos->{'prettyprint_cache'} = {};
41         $pos->{'tbprobe_cache'} = {};
42
43         bless $pos, $class;
44         return $pos;
45 }
46
47 sub start_pos {
48         my ($class, $white, $black) = @_;
49         $white = "base64:" . MIME::Base64::encode_base64($white);
50         $black = "base64:" . MIME::Base64::encode_base64($black);
51         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");
52 }
53
54 sub from_fen {
55         my ($class, $fen) = @_;
56         my ($board, $toplay, $castling, $ep_square, $halfmove_clock, $fullmove_clock) = split / /, $fen;
57
58         my $pos = {};
59         $board =~ s/(\d)/"-"x$1/ge;
60         $pos->{'board'} = Board->new(split /\//, $board);
61         $pos->{'toplay'} = uc($toplay);
62
63         if ($ep_square =~ /^([a-h])/) {
64                 $pos->{'ep_file_num'} = ord($1) - ord('a');
65         } else {
66                 $pos->{'ep_file_num'} = -1;
67         }
68
69         $pos->{'white_castle_k'} = ($castling =~ /K/) ? 1 : 0;
70         $pos->{'white_castle_q'} = ($castling =~ /Q/) ? 1 : 0;
71         $pos->{'black_castle_k'} = ($castling =~ /k/) ? 1 : 0;
72         $pos->{'black_castle_q'} = ($castling =~ /q/) ? 1 : 0;
73         $pos->{'time_since_100move_rule_reset'} = $halfmove_clock // 0;
74         $pos->{'player_w'} = 'white';
75         $pos->{'player_b'} = 'black';
76         $pos->{'white_clock'} = 0;
77         $pos->{'black_clock'} = 0;
78         $pos->{'move_num'} = $fullmove_clock // 0;
79         $pos->{'last_move_uci'} = undef;
80         $pos->{'last_move'} = undef;
81         $pos->{'prettyprint_cache'} = {};
82         $pos->{'tbprobe_cache'} = {};
83         
84         bless $pos, $class;
85         return $pos;
86 }
87
88 sub fen {
89         my $pos = shift;
90
91         # the board itself
92         my $fen = $pos->{'board'}->fen();
93
94         # white/black to move
95         $fen .= " ";
96         $fen .= lc($pos->{'toplay'});
97
98         # castling
99         my $castling = "";
100         $castling .= "K" if ($pos->{'white_castle_k'} == 1);
101         $castling .= "Q" if ($pos->{'white_castle_q'} == 1);
102         $castling .= "k" if ($pos->{'black_castle_k'} == 1);
103         $castling .= "q" if ($pos->{'black_castle_q'} == 1);
104         $castling = "-" if ($castling eq "");
105         # $castling = "-"; # chess960
106         $fen .= " ";
107         $fen .= $castling;
108
109         # en passant
110         my $ep = "-";
111         if ($pos->{'ep_file_num'} != -1) {
112                 my $col = $pos->{'ep_file_num'};
113                 $ep = (qw(a b c d e f g h))[$col];
114
115                 if ($pos->{'toplay'} eq 'B') {
116                         $ep .= "3";
117                 } else {
118                         $ep .= "6";
119                 }
120         }
121         $fen .= " ";
122         $fen .= $ep;
123
124         # half-move clock
125         $fen .= " ";
126         $fen .= $pos->{'time_since_100move_rule_reset'};
127
128         # full-move clock
129         $fen .= " ";
130         $fen .= $pos->{'move_num'};
131
132         return $fen;
133 }
134
135 sub to_json_hash {
136         my $pos = shift;
137         my $json = { %$pos, fen => $pos->fen() };
138         delete $json->{'board'};
139         delete $json->{'prettyprint_cache'};
140         delete $json->{'tbprobe_cache'};
141         delete $json->{'black_castle_k'};
142         delete $json->{'black_castle_q'};
143         delete $json->{'white_castle_k'};
144         delete $json->{'white_castle_q'};
145         delete $json->{'time_since_100move_rule_reset'};
146         if ($json->{'player_w'} =~ /^base64:(.*)$/) {
147                 $json->{'player_w'} = MIME::Base64::decode_base64($1);
148         }
149         if ($json->{'player_b'} =~ /^base64:(.*)$/) {
150                 $json->{'player_b'} = MIME::Base64::decode_base64($1);
151         }
152         return $json;
153 }
154
155 sub parse_pretty_move {
156         my ($pos, $move) = @_;
157         return $pos->{'board'}->parse_pretty_move($move, $pos->{'toplay'});
158 }
159
160 sub num_pieces {
161         my ($pos) = @_;
162         return $pos->{'board'}->num_pieces();
163 }
164
165 # Returns a new Position object.
166 sub make_move {
167         my ($pos, $from_row, $from_col, $to_row, $to_col, $promo, $pretty_move) = @_;
168
169         my $from_square = _pos_to_square($from_row, $from_col);
170         my $to_square = _pos_to_square($to_row, $to_col);
171
172         my $np = {};
173         $np->{'board'} = $pos->{'board'}->make_move($from_row, $from_col, $to_row, $to_col, $promo);
174         if ($pos->{'toplay'} eq 'W') {
175                 $np->{'toplay'} = 'B';
176                 $np->{'move_num'} = $pos->{'move_num'};
177         } else {
178                 $np->{'toplay'} = 'W';
179                 $np->{'move_num'} = $pos->{'move_num'} + 1;
180         }
181
182         my $piece = $pos->{'board'}[$from_row][$from_col];
183         my $dest_piece = $pos->{'board'}[$to_row][$to_col];
184
185         # Find out if this was a two-step pawn move.
186         if (lc($piece) eq 'p' && abs($from_row - $to_row) == 2) {
187                 $np->{'ep_file_num'} = $from_col;
188         } else {
189                 $np->{'ep_file_num'} = -1;
190         }
191
192         # Castling rights.
193         $np->{'white_castle_k'} = $pos->{'white_castle_k'};
194         $np->{'white_castle_q'} = $pos->{'white_castle_q'};
195         $np->{'black_castle_k'} = $pos->{'black_castle_k'};
196         $np->{'black_castle_q'} = $pos->{'black_castle_q'};
197         if ($piece eq 'K') {
198                 $np->{'white_castle_k'} = 0;
199                 $np->{'white_castle_q'} = 0;
200         } elsif ($piece eq 'k') {
201                 $np->{'black_castle_k'} = 0;
202                 $np->{'black_castle_q'} = 0;
203         } elsif ($from_square eq 'a1' || $to_square eq 'a1') {
204                 $np->{'white_castle_q'} = 0;
205         } elsif ($from_square eq 'h1' || $to_square eq 'h1') {
206                 $np->{'white_castle_k'} = 0;
207         } elsif ($from_square eq 'a8' || $to_square eq 'a8') {
208                 $np->{'black_castle_q'} = 0;
209         } elsif ($from_square eq 'h8' || $to_square eq 'h8') {
210                 $np->{'black_castle_k'} = 0;
211         }
212
213         # 50-move rule.
214         if (lc($piece) eq 'p' || $dest_piece ne '-') {
215                 $np->{'time_since_100move_rule_reset'} = 0;
216         } else {
217                 $np->{'time_since_100move_rule_reset'} = $pos->{'time_since_100move_rule_reset'} + 1;
218         }
219         $np->{'player_w'} = $pos->{'player_w'};
220         $np->{'player_b'} = $pos->{'player_b'};
221         if (defined($pretty_move)) {
222                 $np->{'last_move'} = $pretty_move;
223         } else {
224                 my ($move, $nb) = $pos->{'board'}->prettyprint_move($from_row, $from_col, $to_row, $to_col, $promo);
225                 $np->{'last_move'} = $move;
226         }
227         $np->{'last_move_uci'} = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
228
229         return bless $np;
230 }
231
232 # Returns a new Position object, and the parsed UCI move.
233 sub make_pretty_move {
234         my ($pos, $move) = @_;
235
236         my ($from_row, $from_col, $to_row, $to_col, $promo) = $pos->parse_pretty_move($move);
237         my $uci_move = Board::move_to_uci_notation($from_row, $from_col, $to_row, $to_col, $promo);
238         $pos = $pos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
239         return ($pos, $uci_move);
240 }
241
242 sub _pos_to_square {
243         my ($row, $col) = @_;
244         return sprintf("%c%d", ord('a') + $col, 8 - $row);
245 }
246
247 sub apply_uci_pv {
248         my ($pos, @pv) = @_;
249
250         my $pvpos = $pos;
251         for my $pv_move (@pv) {
252                 my ($from_row, $from_col, $to_row, $to_col, $promo) = _parse_uci_move($pv_move);
253                 $pvpos = $pvpos->make_move($from_row, $from_col, $to_row, $to_col, $promo);
254         }
255
256         return $pvpos;
257 }
258
259 sub _col_letter_to_num {
260         return ord(shift) - ord('a');
261 }
262
263 sub _row_letter_to_num {
264         return 7 - (ord(shift) - ord('1'));
265 }
266
267 sub _parse_uci_move {
268         my $move = shift;
269         my $from_col = _col_letter_to_num(substr($move, 0, 1));
270         my $from_row = _row_letter_to_num(substr($move, 1, 1));
271         my $to_col   = _col_letter_to_num(substr($move, 2, 1));
272         my $to_row   = _row_letter_to_num(substr($move, 3, 1));
273         my $promo    = substr($move, 4, 1);
274         return ($from_row, $from_col, $to_row, $to_col, $promo);
275 }
276
277 1;