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