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