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