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