File Coverage

blib/lib/Chess/Rep/Coverage.pm
Criterion Covered Total %
statement 145 183 79.2
branch 38 64 59.3
condition 23 32 71.8
subroutine 11 17 64.7
pod 3 3 100.0
total 220 299 73.5


line stmt bran cond sub pod time code
1             package Chess::Rep::Coverage;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: Expose chess ply potential energy
5              
6 1     1   830 use strict;
  1         2  
  1         31  
7 1     1   6 use warnings;
  1         2  
  1         27  
8              
9 1     1   449 use parent 'Chess::Rep';
  1         310  
  1         6  
10              
11 1     1   16039 use constant SIZE => 7;
  1         3  
  1         2318  
12              
13             our $VERSION = '0.1103';
14              
15              
16             sub coverage {
17 3     3 1 18400 my $self = shift;
18              
19             # Get the state of the board
20 3         14 my $fen = $self->get_fen;
21              
22             # Bucket of piece coverages to return
23 3         1713 my $cover = {};
24              
25             # Set the numerical id => piece name lookup table
26 3         7 my %pieces;
27 3         4 @pieces{values %{+Chess::Rep::PIECE_TO_ID()}} = keys %{+Chess::Rep::PIECE_TO_ID()};
  3         30  
  3         14  
28              
29             # Look at each board position.
30 3         9 for my $row (0 .. SIZE) {
31 24         41 for my $col (0 .. SIZE) {
32 192         405 my $p = $self->get_piece_at($row, $col); # decimal of index
33 192 100       2519 if ($p) {
34 37         76 my $c = Chess::Rep::piece_color($p); # 0=black, 0x80=white
35 37         225 my $i = Chess::Rep::get_index($row, $col); # $row << 4 | $col
36 37         227 my $f = Chess::Rep::get_field_id($i); # A-H, 1-8
37              
38             # Set the coverage properties for the piece.
39 37         690 $cover->{$f}{occupant} = $pieces{$p};
40 37         84 $cover->{$f}{piece} = $p;
41 37         81 $cover->{$f}{color} = $c;
42 37         81 $cover->{$f}{index} = $i;
43 37         67 $cover->{$f}{protects} = [];
44 37         77 $cover->{$f}{threatens} = [];
45              
46             # Kings are special-cased.
47 37 100 100     175 if ($p & 0x04) {
    100          
48             # Collect the moves of the piece.
49 3         17 $cover->{$f}{move} = $self->_fetch_new_moves($f, $i, $c);
50              
51             # Inspect the positions surrounding the king.
52 3         25 for my $m ([$row, $col + 1], [$row + 1, $col], [$row + 1, $col + 1], [$row + 1, $col - 1],
53             [$row, $col - 1], [$row - 1, $col], [$row - 1, $col - 1], [$row - 1, $col + 1]
54             ) {
55 24         52 my $x = Chess::Rep::get_index(@$m);
56 24 100       148 next if $x & 0x88;
57 18         37 $self->_set_piece_status($cover, $f, $x, $c);
58             }
59             }
60             # Attacking pawns are special-cased.
61             elsif (($p & 0x01) && $self->to_move != $c) {
62 9 50       93 my $moves = $c == 0
63             ? [ [$row - 1, $col + 1], [$row - 1, $col - 1] ]
64             : [ [$row + 1, $col + 1], [$row + 1, $col - 1] ];
65             # Add diagonal positions unless occupied.
66 9         21 for my $m (@$moves) {
67 18 100 33     105 next if $m->[0] < 0 || $m->[0] > SIZE
      66        
      100        
68             || $m->[1] < 0 || $m->[1] > SIZE;
69 16         33 my $x = Chess::Rep::get_index(@$m);
70 16         102 $self->_set_piece_status($cover, $f, $x, $c);
71             # Collect the moves of the piece.
72 16         20 push @{ $cover->{$f}{move} }, $x;
  16         51  
73             }
74             }
75             else {
76             # Invert the FEN to compute all possible moves, threats and protections.
77 25         155 my $inverted = _invert_fen($fen, $row, $col, $c);
78 25         101 $self->set_from_fen($inverted);
79              
80             # Collect the moves of the piece.
81 25         124271 $cover->{$f}{move} = $self->_fetch_new_moves($f, $i, $c);
82              
83             # Reset original game FEN.
84 25         88 $self->set_from_fen($fen);
85              
86             # Find the threats and protections by the piece.
87 25         231119 $self->_set_piece_status($cover, $f, $_, $c) for @{$cover->{$f}{move}};
  25         145  
88             }
89             }
90             }
91             }
92              
93             # Compute piece and position status.
94 3         21 for my $piece (keys %$cover) {
95 37   100     181 $cover->{$piece}{is_threatened_by} ||= [];
96 37   100     125 $cover->{$piece}{is_protected_by} ||= [];
97              
98             # Compute protection status of a piece.
99 37         52 for my $index (@{$cover->{$piece}{protects}}) {
  37         73  
100 40         87 my $f = Chess::Rep::get_field_id($index); # A-H, 1-8
101 40         450 push @{$cover->{$f}{is_protected_by}}, $cover->{$piece}{index};
  40         124  
102             }
103              
104             # Compute threat status of a piece.
105 37         53 for my $index (@{$cover->{$piece}{threatens}}) {
  37         97  
106 3         7 my $f = Chess::Rep::get_field_id($index); # A-H, 1-8
107 3         38 push @{$cover->{$f}{is_threatened_by}}, $cover->{$piece}{index};
  3         9  
108             }
109              
110             # Compute move status of a position.
111 37         56 for my $index (@{$cover->{$piece}{move}}) {
  37         65  
112 88         181 my $p = $self->get_piece_at($index);
113 88 100       888 if (!$p) {
114 53         98 my $f = Chess::Rep::get_field_id($index); # A-H, 1-8
115              
116 53   100     784 $cover->{$f}{white_can_move_here} ||= [];
117 53   100     195 $cover->{$f}{black_can_move_here} ||= [];
118              
119 53 100       120 my $color = $cover->{$piece}{color} ? 'white' : 'black';
120 53         76 push @{$cover->{$f}{$color . '_can_move_here'}}, $cover->{$piece}{index};
  53         162  
121             }
122             }
123             }
124              
125             # Set the object coverage attribute.
126 3         25 $self->_cover($cover);
127              
128 3         79 return $cover;
129             }
130              
131             sub _invert_fen {
132 25     25   78 my ($fen, $row, $col, $color) = @_;
133              
134             # Grab the board positions only.
135 25         43 my $suffix = '';
136 25 50       218 if ($fen =~ /^(.+?)\s(.*)$/) {
137 25         126 ($fen, $suffix) = ($1, $2);
138             }
139             # Convert pieces to all black or all white, given the piece color.
140 25 100       72 $fen = $color ? lc $fen : uc $fen;
141             # Split the FEN into rows.
142 25         118 my @fen = split /\//, $fen; # rows: 7..0, cols: 0..7
143             # The FEN sections are the rows reversed.
144 25         48 $row = SIZE - $row;
145              
146 25         42 my $position = 0;
147 25         41 my $counter = 0;
148             # Inspect each character in the row to find the position of the piece to invert.
149 25         97 for my $i (split //, $fen[$row]) {
150             # Increment the position if we are on a digit.
151 105 100       212 if ($i =~ /^\d$/) {
152 3         6 $position += $i;
153             }
154             else {
155             # Invert the piece character (to its original state) or increment the position.
156 102 100       168 if ($position == $col) {
157 25         103 substr($fen[$row], $counter, 1) = $i ^ "\x20";
158 25         40 last;
159             }
160             else {
161             # Next!
162 77         109 $position++;
163             }
164             }
165              
166             # Increment the loop counter.
167 80         134 $counter++;
168             }
169              
170 25         147 return join('/', @fen) . " $suffix";
171             }
172              
173             sub _fetch_new_moves {
174 28     28   57 my $self = shift;
175 28         61 my($field, $index, $color) = @_;
176             # Set the "next to move" color to the piece.
177 28         111 $self->to_move($color);
178             # Recompute the move status.
179 28         246 $self->compute_valid_moves;
180             # TODO Pawns can move diagonally to capture. That is a valid move in the abstract.
181             # Collect the moves of the piece.
182 28         45745 return [ map { $_->{to} } grep { $_->{from} == $index } @{ $self->status->{moves} } ];
  72         191  
  113         316  
  28         89  
183             }
184              
185             sub _set_piece_status {
186 99     99   157 my $self = shift;
187 99         192 my($cover, $field, $index, $color) = @_;
188             # Only consider positions with pieces.
189 99         224 my $p = $self->get_piece_at($index);
190 99 100       1128 return unless $p;
191             # Set the protection or threat status of the piece.
192 46 100       94 if (Chess::Rep::piece_color($p) == $color) {
193             # Any piece can be protected but a king.
194 43 100 100     392 push @{$cover->{$field}{protects}}, $index
  40         181  
195             unless $p == 4 or $p == 132;
196             }
197             else {
198             # Any piece can be threatened.
199 3         19 push @{$cover->{$field}{threatens}}, $index;
  3         11  
200             }
201             }
202              
203             sub _cover {
204 223     223   295 my $self = shift;
205 223 100       399 $self->{cover} = shift if @_;
206 223         529 return $self->{cover};
207             }
208              
209              
210             sub board {
211 2     2 1 3440 my $self = shift;
212 2         7 my %args = @_;
213              
214             # Compute coverage if has not been done yet.
215 2 50       4 $self->coverage unless $self->_cover;
216              
217             # Start rendering the board.
218 2         6 my $board = _ascii_board('header');
219 2         5 $board .= _ascii_board('row');
220              
221             # Look at each board position.
222 2         7 for my $row (reverse(1 .. 8)) {
223             # Render the beginning of the row.
224 16         30 $board .= $row . _ascii_board('cell_pad');
225              
226 16         44 for my $col ('A' .. 'H') {
227             # Render a new cell.
228 128         207 $board .= _ascii_board('new_cell');
229              
230             # Inspect the coverage at the column and row position.
231 128 100       240 if ($self->_cover->{$col . $row}) {
232 19 100 66     37 if (exists $self->_cover->{$col . $row}->{is_protected_by} and
    50 33        
233             exists $self->_cover->{$col . $row}->{is_threatened_by}
234             ) {
235             # Show threat and protection status.
236 5         10 my $protects = $self->_cover->{$col . $row}->{is_protected_by};
237 5         11 my $threats = $self->_cover->{$col . $row}->{is_threatened_by};
238 5         14 $board .= @$protects . '/' . @$threats;
239             # $board .= $self->_cover->{$col . $row}->{occupant};
240             }
241             elsif (exists $self->_cover->{$col . $row}->{white_can_move_here} and
242             exists $self->_cover->{$col . $row}->{black_can_move_here}
243             ) {
244             # Show player movement status.
245 14         23 my $whites = $self->_cover->{$col . $row}->{white_can_move_here};
246 14         21 my $blacks = $self->_cover->{$col . $row}->{black_can_move_here};
247 14         31 $board .= @$whites . ':' . @$blacks;
248             # $board .= $self->_cover->{$col . $row}->{occupant};
249             }
250             }
251             else {
252             # Render an empty cell.
253 109         173 $board .= _ascii_board('empty_cell');
254             }
255              
256             # Render the end of a cell.
257 128         221 $board .= _ascii_board('cell_pad');
258             # Render the end of a column if we have reached the last.
259 128 100       306 $board .= _ascii_board('col_edge') if $col eq 'H';
260             }
261              
262             # Render the end of a row.
263 16         31 $board .= "\n" . _ascii_board('row');
264             }
265              
266 2         9 return $board;
267             }
268              
269             sub _ascii_board {
270 417     417   625 my $section = shift;
271              
272 417         705 my ($cells, $size, $empty) = (8, 5, 3);
273              
274 417         895 my %board = (
275             cell_pad => ' ',
276             col_edge => '|',
277             corner => '+',
278             row_edge => '-',
279             );
280 417         799 $board{edge} = $board{corner} . ($board{row_edge} x $size);
281 417         858 $board{row} = ($board{cell_pad} x ($empty - 1)) . ($board{edge} x $cells) . $board{corner} . "\n";
282 417         662 $board{empty_cell} = $board{cell_pad} x $empty;
283 417         686 $board{new_cell} = $board{col_edge} . $board{cell_pad};
284 417         998 $board{header} = ($board{cell_pad} x $size) . join($board{cell_pad} x $size, 'A' .. 'H') . "\n";
285              
286 417         1036 return $board{$section};
287             }
288              
289              
290             sub move_probability {
291 0     0 1   my ($moves, $threat, $threatened, $protect, $protected) = @ARGV;
292              
293             # Bail-out unless the number of moves (greater than or equal to 0).
294 0 0 0       die _usage() unless $moves and $moves >= 0;
295              
296             # Set threat penalty and protection rewards.
297 0           $threat = _set_level('threat', $threat, $moves);
298 0           $protect = _set_level('protect', $protect, $moves);
299              
300             # Create a piece that is unprotected, unthreatened and unbounded.
301 0           my $piece = [ map { 1 / $_ } ($moves) x $moves ];
  0            
302              
303             # Apply threatened and protected states to move probabilities.
304 0           $piece = _influence($threatened, $threat, $piece, 'threat');
305 0           _output_state($piece);
306 0           $piece = _influence($protected, $protect, $piece);
307 0           _output_state($piece);
308             }
309              
310             sub _influence {
311 0     0     my($influenced, $score, $piece, $state) = @_;
312              
313             # "Move along. Nothing to compute here."
314 0 0         return $piece unless $influenced;
315              
316             # Convenience variable for "size of piece" == "number of moves."
317 0           my $size = @$piece - 1;
318              
319             # Traverse the influenced moves and compute the probabilities.
320 0           for my $move (split ',', $influenced) {
321             # Move counter.
322 0           my $n = 0;
323              
324             # Re-evaluate each move given the threat value.
325 0           for my $p (@$piece) {
326 0 0         if ($n + 1 == $move) {
327             # We've found an influenced move!
328 0 0         $p = $state
329             ? $p - $score # For threats, add the score to the move.
330             : $p + $score; # For protection, subract from the move.
331             }
332             else {
333             # All moves not influenced.
334 0 0         if ($size != 0) {
335 0 0         $p = $state
336             ? $p + $score / $size # For threats, subtract a fraction of the score.
337             : $p - $score / $size; # For protection, add a fraction of the score.
338             }
339             }
340              
341             # Increment the move number of the piece.
342 0           $n++;
343             }
344             }
345              
346             # Make sure all elements sum to 1.
347 0           _cross_check($piece);
348              
349 0           return $piece;
350             }
351              
352             sub _output_state {
353 0     0     my $piece = shift;
354 0           my $i = 0;
355 0           print 'P: ', join(' ', map { sprintf '%d:%.4f', ++$i, $_ } @$piece), "\n";
  0            
356             }
357              
358             sub _set_level {
359 0     0     my ($level, $value, $moves) = @_;
360              
361             # Unless given, default value is zero.
362 0 0         $value = defined $value ? $value : 0;
363             # Bail out unless the value is either zero or greater than moves.
364 0 0         die ucfirst($level) . "level must be zero or more.\n" if $value < 0;
365              
366             # Set a non-zero value in relation to the number of moves.
367 0 0         $value = $moves + $value - 1 if $value > 0;
368              
369             # Make value something that can be used in probability equations.
370 0 0         $value = 1 / $value if $value != 0;
371              
372 0           return $value;
373             }
374              
375             sub _cross_check {
376 0     0     my $vector = shift;
377 0           my $sum = 0;
378             # Make sure all elements sum to unity.
379 0           $sum += $_ for @$vector;
380             # TODO Make == work, instead of eq.
381 0 0         warn "Sum: $sum\n" if $sum ne '1';
382             }
383              
384             sub _usage {
385 0     0     return <
386              
387             Compute probabilites of chess moves in a protective, threatening environment.
388              
389             Usage: perl $0 [0-9...] [0-9...] [t1,t2...] [0-9...] [p1,p2...]
390              
391             Ordered arguments:
392             'moves' is the number of moves of a piece.
393             For example, a unobstructed knight can make eight moves.
394             'threat' is the value or score of a single threat.
395             'threatened moves' is a CSV list of threatened move numbers.
396             This means that you can be captured by your enemy if you move there.
397             'protect' is the value or score of a single protection.
398             'protected moves' is a CSV list of protected move numbers.
399             This means that you will be protected by an ally if you move there.
400              
401             Examples:
402             perl move-probability 8 # An unobstructed, unprotected knight
403             perl move-probability 8 1 # Same
404             perl move-probability 8 1 0 # Ditto
405             perl move-probability 8 1 0 1 # "
406             perl move-probability 8 1 0 1 0 # Right. Gotchya. 10-4 Good buddy.
407             perl move-probability 8 1 1,8 # Threaten the 1st & 8th moves.
408             perl move-probability 8 0 0 1 1,2 # Protect the 1st & 2nd moves.
409             perl move-probability 8 1 1,8 1 1,2 # Protect & threaten
410             perl move-probability 8 10 1,8 # Threaten with a penalty of 10.
411              
412             * This logic does not treat "not making a move" as a value, at the moment...
413             USAGE
414             }
415            
416             1;
417              
418             __END__