| 1 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # Games::Chess - represent chess pieces, positions, moves and games | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # Gareth Rees <garethr@cre.canon.co.uk> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Copyright (c) 1999 Gareth Rees. This module is free software: you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # can distribute and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # $Id: Chess.pm,v 1.5 1999/06/06 18:47:24 gareth Exp $ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | package Games::Chess; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use vars qw($RCSID $VERSION $ERRMSG $DEBUG @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | $RCSID = q$Id: Chess.pm,v 1.5 1999/06/06 18:47:24 gareth Exp $; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | $ERRMSG = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | @EXPORT = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | @EXPORT_OK = qw(algebraic_to_xy colour_valid debug errmsg error | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | halfmove_count_valid move_number_valid piece_valid xy_valid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | xy_to_algebraic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | EMPTY WHITE BLACK PAWN KNIGHT BISHOP ROOK QUEEN KING); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | %EXPORT_TAGS = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | ( colours => [qw(EMPTY WHITE BLACK)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | pieces => [qw(EMPTY PAWN KNIGHT BISHOP ROOK QUEEN KING)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | constants => [qw(EMPTY WHITE BLACK PAWN KNIGHT BISHOP ROOK QUEEN KING)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | functions => [qw(algebraic_to_xy colour_valid debug errmsg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | halfmove_count_valid move_number_valid piece_valid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | xy_valid xy_to_algebraic)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | use constant EMPTY => 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | ▶ | use constant WHITE => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | ▶ | use constant BLACK => 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | ▶ | use constant PAWN => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | use constant KNIGHT => 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | use constant BISHOP => 3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | use constant ROOK => 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | ▶ | use constant QUEEN => 5; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | use constant KING => 6; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub algebraic_to_xy ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
algebraic_to_xy: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my ($sq) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $sq =~ /^([a-h])([1-8])$/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | or return error("$sq does not specify a square in algebraic notation"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return (ord($1) - ord('a'), $2 - 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub colour_valid ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
colour_valid: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my ($colour) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return 1 if $colour == WHITE or $colour == BLACK; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$colour == 1 or $colour == 2
MC/DC0%0/2
$colour == 1 or $colour == 2
$colour == 1
$colour == 2
Truth table0%0/3
A or B
A= $colour == 1B= $colour == 2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return error("colour $colour invalid: must be @{[WHITE]} or @{[BLACK]}"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub debug ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $DEBUG = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub errmsg () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
errmsg: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $ERRMSG; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub error ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $ERRMSG = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($DEBUG > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ($filename,$line) = (caller(2))[1,2]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $message = "$ERRMSG at $filename line $line\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $DEBUG >= 2 ? die($message) : warn($message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub halfmove_count_valid ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
halfmove_count_valid: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my ($halfmove) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return 1 if $halfmove =~ /^[0-9]+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return error("halfmove clock '$halfmove' not a non-negative integer"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub move_number_valid ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
move_number_valid: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my ($move) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return 1 if $move =~ /^[0-9]+$/ and $move > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$move =~ /^[0-9]+$/ and $move > 0
MC/DC0%0/2
$move =~ /^[0-9]+$/ and $move > 0
$move =~ /^[0-9]+$/
$move > 0
Truth table0%0/3
A and B
A= $move =~ /^[0-9]+$/B= $move > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return error("Fullmove number '$move' not a positive integer"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub piece_valid ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
piece_valid: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my ($piece) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | return 1 if PAWN <= $piece and $piece <= KING; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
1 <= $piece and $piece <= 6
MC/DC0%0/2
1 <= $piece and $piece <= 6
1 <= $piece
$piece <= 6
Truth table0%0/3
A and B
A= 1 <= $pieceB= $piece <= 6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | return error("piece $piece invalid: not between @{[PAWN]} and @{[KING]}"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub xy_to_algebraic ($$) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xy_to_algebraic: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my ($x,$y) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return unless xy_valid($x,$y); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return chr($x + ord('a')) . ($y + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub xy_valid ($$) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xy_valid: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my ($x,$y) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | return 1 if 0 <= $x and $x < 8 and 0 <= $y and $y < 8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
0 <= $x and $x < 8 and 0 <= $y and $y < 8
0 <= $x and $x < 8 and 0 <= $y
0 <= $x and $x < 8
MC/DC0%0/4
0 <= $x and $x < 8 and 0 <= $y and $y < 8
0 <= $x
$x < 8
0 <= $y
$y < 8
Truth table0%0/5
A and B and C and D
A= 0 <= $xB= $x < 8C= 0 <= $yD= $y < 8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return error("($x,$y) off chessboard: not in the range (0,0) to (7,7)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # Games::Chess::Piece - representation of a chess piece | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # A piece is represented as a blessed reference to a byte. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | package Games::Chess::Piece; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 4 | Games::Chess->import(qw(error piece_valid colour_valid)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | my @COLOUR_NAMES = qw(empty white black unknown); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | my @PIECE_NAMES = qw(square pawn knight bishop rook queen king unknown); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | my $pieces = 'pnbrqk'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | my @CODE_PIECE = split '', " $pieces "; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | my $PIECE_CODES = " \U$pieces\E$pieces"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | my %PIECE_CODES; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | @PIECE_CODES{split '', $PIECE_CODES} = (0, 9..14, 17..22); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my ($class,$val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | my $self = chr(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (@_ < 2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # Use the default (empty square). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } elsif (@_ > 3) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return error("Piece->new called with more than 3 arguments"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } elsif (@_ == 3) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return unless colour_valid($_[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | return unless piece_valid($_[2]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | $self = chr(($_[1] << 3) + $_[2]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } elsif (UNIVERSAL::isa($val,'Games::Chess::Piece')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $self = $$val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } elsif (exists $PIECE_CODES{$val}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | $self = chr($PIECE_CODES{$val}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } elsif ($val !~ /^\d+$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return error("Piece->new('$val') invalid: '$val' not a chess piece"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ▶ | } elsif (0 <= $val and $val < 256 and $val == int $val) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
0 <= $val and $val < 256 and $val == int $val
0 <= $val and $val < 256
MC/DC0%0/3
0 <= $val and $val < 256 and $val == int $val
0 <= $val
$val < 256
$val == int $val
Truth table0%0/4
A and B and C
A= 0 <= $valB= $val < 256C= $val == int $val
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $self = chr($val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return error("Piece->new($val) invalid: $val outside range 0 to 255"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | bless \$self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub code ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
code: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my $col = (ord($$self) & 24) >> 3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $code = $CODE_PIECE[ord($$self) & 7]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | return $col == 2 ? $code : uc($code); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub colour ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
colour: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | return (ord($$self) & 24) >> 3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub colour_name ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
colour_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | return $COLOUR_NAMES[$self->colour]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub name ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | return join ' ', $self->colour_name, $self->piece_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub piece ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
piece: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | return ord($$self) & 7; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub piece_name ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
piece_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | return $PIECE_NAMES[$self->piece]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # Games::Chess::Move - representation of a chess move | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | package Games::Chess::Move; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 4 | Games::Chess->import(qw(error xy_valid)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my ($class,$xs,$ys,$xd,$yd,@promotion) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | return unless xy_valid($xs,$ys) and xy_valid($xd,$yd); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
&xy_valid($xs, $ys) and &xy_valid($xd, $yd)
MC/DC0%0/2
&xy_valid($xs, $ys) and &xy_valid($xd, $yd)
&xy_valid($xs, $ys)
&xy_valid($xd, $yd)
Truth table0%0/3
A and B
A= &xy_valid($xs, $ys)B= &xy_valid($xd, $yd)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | my $self = { from => [$xs,$ys], to => [$xd,$yd] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (@promotion) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $p = Games::Chess::Piece->new(@promotion); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | return unless $p; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | $self->{'promotion'} = $p; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub cmp ( $$ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmp: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my ($a,$b) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | UNIVERSAL::isa($b, 'Games::Chess::Move') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | or return error("Argument to 'cmp' must be of class Games::Chess::Move"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | return ($a->{'from'}[0] <=> $b->{'from'}[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | or $a->{'from'}[1] <=> $b->{'from'}[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | or $a->{'to'}[0] <=> $b->{'to'}[0] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | or $a->{'to'}[1] <=> $b->{'to'}[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | or do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/12
$a->{'from'}[0] <=> $b->{'from'}[0] || $a->{'from'}[1] <=> $b->{'from'}[1] || $a->{'to'}[0] <=> $b->{'to'}[0] || $a->{'to'}[1] <=> $b->{'to'}[1] || do {
my $ap = $a->{'promotion'};
my $bp = $b->{'promotion'};
defined $ap ? defined $bp ? $$ap <=> $$bp : -1 : 1
}
$a->{"from"}[0] <=> $b->{"from"}[0] or $a->{"from"}[1] <=> $b->{"from"}[1] or $a->{"to"}[0] <=> $b->{"to"}[0] or $a->{"to"}[1] <=> $b->{"to"}[1]
$a->{"from"}[0] <=> $b->{"from"}[0] or $a->{"from"}[1] <=> $b->{"from"}[1] or $a->{"to"}[0] <=> $b->{"to"}[0]
$a->{"from"}[0] <=> $b->{"from"}[0] or $a->{"from"}[1] <=> $b->{"from"}[1]
MC/DC0%0/5
$a->{'from'}[0] <=> $b->{'from'}[0] || $a->{'from'}[1] <=> $b->{'from'}[1] || $a->{'to'}[0] <=> $b->{'to'}[0] || $a->{'to'}[1] <=> $b->{'to'}[1] || do {
my $ap = $a->{'promotion'};
my $bp = $b->{'promotion'};
defined $ap ? defined $bp ? $$ap <=> $$bp : -1 : 1
}
$a->{"from"}[0] <=> $b->{"from"}[0]
$a->{"from"}[1] <=> $b->{"from"}[1]
$a->{"to"}[0] <=> $b->{"to"}[0]
$a->{"to"}[1] <=> $b->{"to"}[1]
do {
my $ap = $a->{'promotion'};
my $bp = $b->{'promotion'};
defined $ap ? defined $bp ? $$ap <=> $$bp : -1 : 1
}
Truth table0%0/6
A or B or C or D || E
A= $a->{"from"}[0] <=> $b->{"from"}[0]B= $a->{"from"}[1] <=> $b->{"from"}[1]C= $a->{"to"}[0] <=> $b->{"to"}[0]D= $a->{"to"}[1] <=> $b->{"to"}[1]E= do {
my $ap = $a->{'promotion'};
my $bp = $b->{'promotion'};
defined $ap ? defined $bp ? $$ap <=> $$bp : -1 : 1
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | my $ap = $a->{'promotion'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my $bp = $b->{'promotion'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | defined $ap ? (defined $bp ? $$ap <=> $$bp : -1) : 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | sub from ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | return @{$self->{'from'}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | sub to ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | return @{$self->{'to'}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | sub promotion ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
promotion: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | return @{$self->{'promotion'}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # Games::Chess::Position - representation of a chess position | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | package Games::Chess::Position; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 4 | ▶ | use vars '%gifs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 4 | Games::Chess->import(qw(:constants :functions error)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 4 | my $init_pos = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=23 SCAR=63.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | my ($class,$val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # Passed another Position object? Return a copy. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | if (defined $val and UNIVERSAL::isa($val,'Games::Chess::Position')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $val and UNIVERSAL::isa($val, "Games::Chess::Position")
MC/DC0%0/2
defined $val and UNIVERSAL::isa($val, "Games::Chess::Position")
defined $val
UNIVERSAL::isa($val, "Games::Chess::Position")
Truth table0%0/3
A and B
A= defined $valB= UNIVERSAL::isa($val, "Games::Chess::Position")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | return bless { %$val }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # We've been passed a board position in Forsythe-Edwards Notation (FEN). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $self = { }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | $val = $init_pos unless $val; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | # Split the FEN string into fields. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my @fields = split ' ', $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # First element is board description: split into into ranks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | my @ranks = split '/', $fields[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | @ranks == 8 or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | return error("Position '$fields[0]' does not have 8 ranks"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # Turn each rank into an array of 8 piece codes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | foreach my $r (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my $rank = $ranks[$r]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $rank =~ s/(\d)/' ' x $1/eg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | length $rank == 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | or return error("Rank $r '$rank' does not have 8 squares"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $ranks[$r] = [ map { $PIECE_CODES{$_} } split '', $rank ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | @{$ranks[$r]} == 8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | or return error("Rank $r '$rank' contains an invalid piece code"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # Transform the 2-d array and assemble into the board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | $self->{'board'} = pack('C64', map { $ranks[7-$_%8][int($_/8)] } 0 .. 63); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # Active color (defaults to white). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | $fields[1] = 'w' unless defined $fields[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | if ($fields[1] eq 'w') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | $self->{'player_to_move'} = &WHITE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | } elsif ($fields[1] eq 'b') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $self->{'player_to_move'} = &BLACK; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | return error("Invalid player to move: '$fields[1]'"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # Castling availability (defaults to none). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | $fields[2] = '-' unless defined $fields[2]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | unless ($fields[2] eq '-') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | (join '', sort split '', $fields[2]) eq $fields[2] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | or return error("Castling availability '$fields[2]' not sorted"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | foreach (split '', $fields[2]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | /^[KQkq]$/ or return error("Castling availability '$_' not KQkq"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | $self->{'can_castle'}{$_} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # En passant target square (default none). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | $fields[3] = '-' unless defined $fields[3]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | unless ($fields[3] eq '-') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | my @square = algebraic_to_xy($fields[3]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | return unless @square == 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | $self->{'en_passant'} = [@square]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # Half-move clock (default 0). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | $fields[4] = '0' unless defined $fields[4]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | return unless halfmove_count_valid($fields[4]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | $self->{'halfmove'} = $fields[4]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # Fullmove number (default 1). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | $fields[5] = '1' unless defined $fields[5]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | return unless move_number_valid($fields[5]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | $self->{'move'} = $fields[5]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # All done. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | sub at { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
at: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | my ($self,$x,$y,@piece) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | return unless xy_valid($x,$y); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | return Games::Chess::Piece->new(vec($self->{'board'}, 8 * $x + $y, 8)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | unless @piece; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $p = Games::Chess::Piece->new(@piece); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | return unless defined $p; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | vec($self->{'board'}, 8 * $x + $y, 8) = ord $$p; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub board ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
board: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | return $self->{'board'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | sub can_castle { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can_castle: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | my ($self,$colour,$piece,$can_castle) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my $p = Games::Chess::Piece->new($colour,$piece); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | return unless defined $p; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my $code = $p->code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | $code =~ /^[KQkq]$/ or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | error("can_castle($colour,$piece) invalid: must be king or queen"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | ▶ | return defined $self->{'can_castle'}{$code} unless defined $can_castle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if ($can_castle) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | $self->{'can_castle'}{$code} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | delete $self->{'can_castle'}{$code}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | ▶ | sub clear ( $$$ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | my ($self,$x,$y) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | ▶ | return unless xy_valid($x,$y); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | vec($self->{'board'}, 8 * $x + $y, 8) = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub en_passant { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
en_passant: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my ($self,@en_passant) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | my $ep = $self->{'en_passant'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | return defined $ep ? @$ep : () unless @en_passant; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | return unless xy_valid(@en_passant); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $self->{'en_passant'} = [@en_passant]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | sub halfmove_clock { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
halfmove_clock: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | my ($self,$halfmove) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | return $self->{'halfmove'} unless defined $halfmove; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | return unless halfmove_count_valid($halfmove); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | $self->{'halfmove'} = $halfmove; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | sub move_number { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
move_number: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | my ($self,$move) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | ▶ | return $self->{'move'} unless defined $move; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | return unless move_number_valid($move); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | $self->{'move'} = $move; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | sub player_to_move { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
player_to_move: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | my ($self,$colour) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | return $self->{'player_to_move'} unless defined $colour; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | return unless colour_valid($colour); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | $self->{'player_to_move'} = $colour; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 4 | my @CASTLE_TESTS = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | [ &WHITE, &KING, { 'e1' => 'K', 'h1' => 'R' } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | [ &WHITE, &QUEEN, { 'e1' => 'K', 'a1' => 'R' } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | [ &BLACK, &KING, { 'e8' => 'k', 'h8' => 'r' } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | [ &BLACK, &QUEEN, { 'e8' => 'k', 'a8' => 'r' } ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | ▶ | sub validate ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=38 SCAR=73.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | my (%n,%m); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | @n{split '', $PIECE_CODES} = (0) x 13; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | @m{split '', $PIECE_CODES} = (0) x 13; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # Count the number of each type of piece. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | foreach my $x (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | foreach my $y (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ++$n{$self->at($x,$y)->code}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | ++$m{$self->at($x,0)->code}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ++$m{$self->at($x,7)->code}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | # More than 8 pawns per side? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | ▶ | $n{p} <= 8 or return error("Black has $n{p} pawns"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 0 | ▶ | $n{P} <= 8 or return error("White has $n{P} pawns"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | # Pawn + promoted piece count plausible? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | ($n{'p'} + (2<$n{'n'} ? $n{'n'}-2 : 0) + (2<$n{'b'} ? $n{'b'}-2 : 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | ▶ | + (2<$n{'r'} ? $n{'r'}-2 : 0) + (1<$n{'q'} ? $n{'q'}-1 : 0) <= 8) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | or return error("Black has more than 8 pawns plus promoted pieces"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | ($n{'P'} + (2<$n{'N'} ? $n{'N'}-2 : 0) + (2<$n{'B'} ? $n{'B'}-2 : 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | + (2<$n{'R'} ? $n{'R'}-2 : 0) + (1<$n{'Q'} ? $n{'Q'}-1 : 0) <= 8) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | or return error("White has more than 8 pawns plus promoted pieces"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | # Not exactly 1 king per side? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | ▶ | $n{'k'} == 1 or return error("Black has $n{'k'} kings"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | ▶ | $n{'K'} == 1 or return error("White has $n{'K'} kings"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | # Pawns on ranks 1 or 8? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | ▶ | $m{'p'} == 0 or return error("Black has a pawn on rank 1 or rank 8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | ▶ | $m{'P'} == 0 or return error("White has a pawn on rank 1 or rank 8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # Impossible en passant target square? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | my $ep = $self->{'en_passant'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | if ($ep) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | ▶ | if ($self->{'player_to_move'} == &WHITE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | $ep->[1] == 5 or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | error("White to move but EP square is @$ep"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | ▶ | $self->at($ep->[0],6)->code == ' ' or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | error("EP square is @$ep but rank 7 is not empty"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | $self->at($ep->[0],5)->code == ' ' or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | error("EP square is @$ep but is not empty"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | $self->at($ep->[0],4)->code == 'p' or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | error("EP square is @$ep but rank 5 does not contain a black pawn"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | $ep->[1] == 2 or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | error("Black to move but EP square is @$ep"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | ▶ | $self->at($ep->[0],1)->code == ' ' or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | error("EP square is @$ep but rank 2 is not empty"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | ▶ | $self->at($ep->[0],2)->code == ' ' or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | error("EP square is @$ep but is not empty"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | ▶ | $self->at($ep->[0],3)->code == 'P' or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | error("EP square is @$ep but rank 4 does not contain a white pawn"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | # Castling availability inconsistent with position? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | foreach my $c (@CASTLE_TESTS) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | my $p = Games::Chess::Piece->new($c->[0], $c->[1]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | ▶ | if ($self->can_castle($c->[0], $c->[1])) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | foreach my $sq (keys %{$c->[2]}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | my $colour = $p->colour_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | my $side = $p->piece_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | my $required = $c->[2]{$sq}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | my $req_name = Games::Chess::Piece->new($required)->piece_name; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | ▶ | $self->at(algebraic_to_xy($sq))->code eq $required or return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | error("$colour can castle ${side}side but no $req_name on $sq"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # Check halfmove count and move number. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | my $h = $self->{'halfmove'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | 0 <= $h or return error("Negative halfmove count $h"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | ▶ | $h == int $h or return error("Non-integer halfmove count $h"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | ▶ | $h <= 50 or return error("Halfmove count $h > 50: game should have drawn"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | my $m = $self->{'move'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | 1 <= $m or return error("Move number $m not positive"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | ▶ | $m == int $m or return error("Non-integer move count $m"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | # Everything checks out OK. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | # Output Games::Chess::Position in varying formats. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | sub to_FEN ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_FEN: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | my $position = join '/', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | my $y = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | my $rank = join '', map { $self->at($_,$y)->code } 0 .. 7; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | $rank =~ s/( +)/length $1/eg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | $rank; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | } reverse 0 .. 7; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | return join ' ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | ( $position, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | ( $self->{'player_to_move'} == &BLACK ? 'b' : 'w'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | ▶ | ( join '', sort keys %{$self->{'can_castle'}} or '-' ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | ( defined $self->{'en_passant'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | ? xy_to_algebraic(@{$self->{'en_passant'}}) : '-' ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | $self->{'halfmove'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | ▶ | $self->{'move'} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
join('', sort(keys %{$$self{'can_castle'};})) || '-'
MC/DC0%0/1
join('', sort(keys %{$$self{'can_castle'};})) || '-'
join('', sort(keys %{$$self{'can_castle'};}))
Truth table0%0/2
A
A= join('', sort(keys %{$$self{'can_castle'};}))
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | ▶ | sub to_text ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_text: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | join "\n", map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | my $y = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | join ' ', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 0 | my $sq = $self->at($_,$y)->code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 0 | ▶ | $sq = '.' if $sq eq ' ' and ($y + $_) % 2 == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$sq eq " " and ($y + $_) % 2 == 0
MC/DC0%0/2
$sq eq " " and ($y + $_) % 2 == 0
$sq eq " "
($y + $_) % 2 == 0
Truth table0%0/3
A and B
A= $sq eq " "B= ($y + $_) % 2 == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | $sq; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | } 0 .. 7; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | } reverse 0 .. 7; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | # Width and height of the GIF images for the pieces. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 4 | my ($width,$height) = (33,33); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | ▶ | sub to_GIF ( $ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_GIF: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | require GD; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | my %opts = ( lmargin => 20, bmargin => 20, border => 2, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | font => GD::Font->Giant, letters => 1, @_ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # Check options. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | ▶ | $opts{lmargin} = $opts{bmargin} = 0 unless $opts{letters}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | foreach (qw(lmargin bmargin border)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | ▶ | 0 <= $opts{$_} or return error("Option $_ $opts{$_} must be >= 0."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | ▶ | UNIVERSAL::isa($opts{font}, 'GD::Font') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | or return error("$opts{font} does not belong to the GD::Font class."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | # Image parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | # $iwidth Total image width | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | # $iheight Total image height | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | my ($iwidth, $iheight) = ($opts{lmargin} + 8 * $width + 2 * $opts{border}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | 8 * $height + $opts{bmargin} + 2 * $opts{border}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | my $img = GD::Image->new($iwidth, $iheight); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | # Colours: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | # $white White squares on the chess board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | # $grey Black squares on the chess board | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | # $black The border and the lettering | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | # $transparent The margins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 0 | my $white = $img->colorAllocate(255,255,255); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | my $grey = $img->colorAllocate(191,191,191); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | my $black = $img->colorAllocate(0,0,0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 0 | my $transparent = $img->colorAllocate(255,192,192); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | $img->transparent($transparent); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | # Colour the board and the margins; draw a border round the board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 0 | $img->filledRectangle(0, 0, $iwidth-1, $iheight-1, $transparent); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | $img->filledRectangle($opts{lmargin}, 0, $iwidth-1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | $iheight-1-$opts{bmargin}, $white); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | ▶ | for (my $i = 0; $i < $opts{border}; ++$i) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | $img->rectangle($opts{lmargin} + $i, $i, $iwidth - 1 - $i, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | $iheight - 1 - $opts{bmargin} - $i, $black); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | # Draw the file letters a-h and the rank numbers 1-8. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 0 | ▶ | if ($opts{letters}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 0 | my ($fw,$fh) = ($opts{font}->width, $opts{font}->height); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | foreach my $n (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | $img->string($opts{font}, ($opts{lmargin} - $fw) / 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | $opts{border} + $n * $height + ($height - $fh) / 2, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 8 - $n, $black); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | $img->string($opts{font}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | $opts{lmargin} + $opts{border} + $n*$width + ($width-$fw)/2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | $iheight - $opts{bmargin} + ($opts{bmargin}-$fh)/2, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | chr(ord('a')+$n), $black); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | # Draw the backgrounds to the black squares and draw the pieces. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | my $gifs = piece_gifs(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 0 | foreach my $x (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 0 | foreach my $y (0 .. 7) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | my ($left,$top) = ($opts{lmargin} + $opts{border} + $x * $width, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | (7 - $y) * $height + $opts{border}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | ▶ | $img->filledRectangle($left,$top,$left+$width-1,$top+$height-1,$grey) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | if ($x + $y) % 2 == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | my $c = $self->at($x,$y)->code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | ▶ | next if $c eq ' '; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | $img->copy($gifs->{$c}, $left, $top, 0, 0, $width, $height); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | # Convert image to GIF and return. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 0 | return $img->gif; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 4 | ▶ | use vars '%gifs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 4 | my %piece_images = | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | ( 'p' => '5555555555555555555555555555555555555555ff75555555555555fff7555555555555dfff5555555555555fff7555555555555dfff5555555555555fff755555555555ffffff7555555555fffffff755555555ffffffff75555555fffffffff7555555dfffffffff5555555dffffffff5555555555dfff5555555555555fff7555555555555dfff555555555555dffff555555555555ffff755555555555dffff555555555555ffff755555555555dffff555555555555ffff755555555555dffff555555555555ffff75555555555dffffff555555555ffffffff75555555fffffffff7555555dfffffffff555555dffffffffff555555ffffffffff75555555555555555555555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 'n' => '5555555555555555555555f755555555555557df5555555555555dffff755555555555ffefff7555555555dfffffff555555555fffffaff55555555dfffffaef5555555dffffffaef5555555fffffffaef555555ffffffffaef55555dffffffffaef5555dffaffffffaf75555ffbefffffbaf7555ffffffffffbef555fffffffffffaef55ffffffffffffae75ffffffffffffbaf5dfffbffffffffbe75fbebfffffffffae5dffbf75dffffffaf5ff7f75dffffffbe75ffd75dfffffffaf555555dffffffffe755555fffffffffbf55555ffffffffffe75555dfffffffffbf5555dfffffffffff75555ffffffffffff5555dfffffffffff55555fffffffffff755555dfffffffff55555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 'b' => '55555555555555555555555f55f555555555555ff5ff55555555555df7df755555555555ff5ff55555555555dffff755555555555fffbf75555555555ffffaf7555555555ffffbaf555555555fffffbaf55555555dfffffbe75555555dffffffbe75555555fffffffaf5555555ffffffffa7555555dffffffffe5555555ffffffffb7555555dffffffffe5555555ffffffffb75555555fffffffff5555555dffffffff75555555ffffffff755555555ffaaaef755555555daaffbae555555555dffffff555555555dfffffff555555555ffaaaef755555555daaffbae555555fffeffffffeff755ffffffffffffff75fffffffffffffff7dfffffffffffffff5dfffff755ffffff5555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 'r' => '55555555555555555555555555555555555fff55df55dff7555dfff5dff5dfff5555ffffffffffff7555dffffffffffff5555ffffffffffff7555dffffffffffff55555ffffbaffff7555555dffaaaeff55555555fbaefaaf755555555beffffa755555555dfffffff555555555fffffff755555555dfffffff555555555fffffff755555555dfffffff555555555fffffff755555555dfffffff555555555fffffff755555555dfffffff555555555fffaeff755555555dfbaaaff55555555dfaafbaef5555555dbaffffbaf555555dffffffffff55555dfffffffffff5555dffffffffffff555dfffffffffffff55dffffffffffffff55ffffffffffffff75dffffffffffffff5555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 'q' => '555555555555555555555f75555f755555555ff7555ff75555555dff555dff55555555ff7555ff755555555f75555f755555555df5555df555555555f75555f755555555dff55dff555555555ff755ff75555f755dfffdfff555fff755fff7fff755ffff55dfffffff55dfff75fffffffff5dffdfffffffffffffff5dffffffffffffff55dfffffffffffff555ffffffaefffff755dfffbaaaaaffff555dfaaafffbaaef5555faffffffffbe75555fffffbfffff75555dffffbafffff55555fffffbfffff75555dfffffffffff55555fffffffffff75555dfffbaaaffff55555dfaaeffaaef555555faffffffbe755555dffffffffff555555dfffffffff55555555fffffff75555555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 'k' => '555555dfff555555555555dffff555555555555fabe755555555555dbeaf555555555555fbaf755555555555dbeaf555555555555fabe7555555dfff7dffff5ffff5dfffffffffffffffdffbafffbfffbafffffbeaeffeffaeeffffbefbeffffaffaffffaffaefffaefbefffbeffbfffbfffaffffbeffefffeffafffffbefbeffbffaffffffbffbffbefbfff7fffaffeffefbeff7dffbefbaaaffafff5dffbaaafbaaafff55dfbaefffffaaff555dfefffbffffef5555dffffbafffff55555dffffbfffff555555ffffffffff755555dffffffffff555555ffffbaffff755555dffbaaaafff555555faaaffbaae755555dffffffffff555555dfffffffff5555555dffffffff55555555dfffffff555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 'P' => '5555555555555555555555555555555555555555ff75555555555555fff7555555555555dbaf5555555555555fae7555555555555dbaf5555555555555fbf755555555555fffeff7555555555ffbaaff755555555fbaaaaaf75555555faaaaaaae7555555dfffffffff55555555ffffffff5555555555dfef5555555555555fbf7555555555555dbaf555555555555dfaef555555555555faae755555555555dbaaf555555555555faae755555555555dbaaf555555555555faae755555555555dfaef555555555555fbaf75555555555dffaeff555555555fffaaeff75555555ffaaaaaef7555555dbaaaaaaaf555555dffffffffff555555ffffffffff75555555555555555555555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 'N' => '5555555555555555555555f755555555555555df55555555555555dfff755555555555dfffff75555555555ffaffff555555555ffbaaeff55555555dfbaaaaff5555555dfaaaaaaef5555555fbaaaaaaef555555fbaaaaaaaef55555dbefaaaaaaf75555dfafbaaaaaaf75555faefaaaaaaef5555faaaaaaaaaaef555fbaaaaaaaaaae755fbaaaaaaaaaaaf75dbaaaaaebaaaaef5dbaaaffffaaaaae75feaaffffaaaaaaf5dbabf75dbaaaaaef5ffff75dfaaaaaaf75fff55dfaaaaaaaf555555dfaaaaaaae755555fbaaaaaaaef55555fbaaaaaaaaf75555dbaaaaaaaaef5555dfaaaaaaaaaf75555fbaaaaaaaaef5555dbaaffffbaaf55555fffffffffff755555dfffffffff55555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 'B' => '55555555555555555555555f55f555555555555ff5ff55555555555df7df755555555555ff5ff55555555555dffff755555555555fefff75555555555faefaf7555555555dbafbaf555555555fbaaebef55555555dbaaafbe75555555dfaaaafaf75555555faaaaafaf5555555fbaaaaefe7555555dbaaaaaebf5555555faaaaaaff7555555dbaaaaaaff5555555fbaaaaaef75555555faaaaaaff5555555dbaaaaaef75555555ffffffff755555555fffffff755555555dfaabaef555555555fbafbaf755555555dfaabaef555555555fffffff755555555dfffffff55555dfffffaaaefffff5dfffffefffefffff5baaaaeffffaaaaa7dfffffffffffffff5dfffff755ffffff5555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 'R' => '55555555555555555555555555555555555fff55df55dff7555dfff5dff5dfff5555faeffbafffae7555dbaafbaafbaaf5555faaaaaaaaaae7555dfbaaaaaaaaff55555fbaaefaaaf7555555dbafffbaf55555555fefbaffe755555555ffaaaef755555555dfaaaaef555555555faaaaae755555555dbaaaaaf555555555faaaaae755555555dbaaaaaf555555555faaaaae755555555dbaaaaaf555555555faaaaae755555555dbaefaaf555555555fafffbe755555555dffbafff55555555dffaaaeff5555555dfaaaaaaef555555dfaaaaaaaef55555dfaaaaaaaaef5555dfaaaaaaaaaef555dfaaaaaaaaaaef55dfaaaaaaaaaaaef55ffffffffffffff75dffffffffffffff5555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 'Q' => '555555555555555555555f75555f755555555ff7555ff75555555dbf555dbf55555555ff7555ff755555555f75555f755555555df5555df555555555f75555f755555555dff55dff555555555fe755fe75555f755dbffdfbf555fff755faf7fbe755ffbf55dbaffbaf55dbfff5dbaeffaaf5dffdffffaaefaaeffff5dfffbaaeaaaffff55dbfbaaaaaaaefe555fbaaaaaaaaaae755dfaaffffffbaef555dffffffffffff5555fffaaaaaaeff75555faaaaeaaaae75555dbaaaefaaaaf55555faaaaeaaaae75555dbaaaaaaaaaf55555faafffffbae75555dfffffffffff55555dffbaaaafff555555fbaaaaaaaf755555dffbaaaafff555555dfffffffff55555555fffffff75555555555555555555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 'K' => '555555dfff555555555555dffff555555555555ffff755555555555dfaef555555555555ffff755555555555dbfbf555555555555fefe7555555dfff7dffff5ffff5dffffffbafffffffdfbaafffffffbaafffbaaaefffffaaaaffbaaaaebaafaaaaaffaaaaaebafaaaaaefbaaaaafaebaaaaaffaaaaaababaaaaaefbaaaaaebfaaaaaaffbaaaaafebaaaaae7fbaaaaabbaaaaae7dfaaaaaefaaaaaef5dfaaeffffffaaaf55dfffffffffffff555dffbaaaaaafff5555dfaaaabaaaef55555dbaaafbaaaf555555faaaabaaae755555dbaaaaaaaaf555555faefffffbe755555dffffffffff555555fffaaaaaff755555dfaaaaaaaaf555555dfbaaaaaef5555555dffffffff55555555dfffffff555510', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | ▶ | sub piece_gifs () { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
piece_gifs: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | ▶ | unless (%gifs) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | # Create GIF image files for the 12 pieces. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | foreach my $code (keys %PIECE_CODES) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | ▶ | next if $code eq ' '; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | $gifs{$code} = GD::Image->new($width,$height); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | my $white = $gifs{$code}->colorAllocate(255,255,255); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 0 | my $black = $gifs{$code}->colorAllocate(0,0,0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 0 | my $transparent = $gifs{$code}->colorAllocate(0,255,0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | $gifs{$code}->transparent($transparent); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 0 | my $v = pack('h*', $piece_images{$code}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | foreach my $x (0 .. $width-1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | foreach my $y (0 .. $width-1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | $gifs{$code}->setPixel($x,$y,($transparent,$white,$black) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | [vec($v, $y * 33 + $x, 2) - 1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | return \%gifs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||