| 1 | package Term::Chess::Game; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Chess::Plisco qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Chess::Plisco::Engine::Position; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use Term::Chess::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 11 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 11 | my $P = 'Chess::Plisco::Engine::Position'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 11 | my $R = 'Term::Chess::Error'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 11 | my %REASON = ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | CP_GAME_STALEMATE() => 'stalemate', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | CP_GAME_FIFTY_MOVES() => 'fifty_moves', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | CP_GAME_INSUFFICIENT_MATERIAL() => 'insufficient_material', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 11 | has white => (is => 'ro', isa => Str, default => 'p1'); | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 11 | has status => (is => 'rw', isa => Str, default => 'active'); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 11 | has _held_seed => (is => 'ro', init_arg => 'seed'); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 11 | has _fen => (is => 'ro', init_arg => 'position'); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 11 | has _pos => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 11 | has _start_fen => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 11 | has _first_halfmoves => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 11 | has _signatures => (is => 'ro', isa => ArrayRef, default => [], private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 11 | has _log => (is => 'ro', isa => ArrayRef, default => [], private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 11 | has _san => (is => 'ro', isa => ArrayRef, default => [], private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 11 | has _result => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 226 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 226 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 226 | my $seed = $self->_held_seed; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 226 | ▶ | Carp::croak('Term::Chess::Game: seed must be exactly thirty-two bytes') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $seed and length $seed == 32
MC/DC50%1/2
defined $seed and length $seed == 32
defined $seed
length $seed == 32
Truth table66%2/3
A and B
A= defined $seedB= length $seed == 32
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | unless defined $seed && length($seed) == 32; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 224 | my $white = $self->white; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 224 | ▶ | Carp::croak("Term::Chess::Game: white must be 'p1' or 'p2', not '$white'") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$white eq "p1" or $white eq "p2"
MC/DC100%2/2
$white eq "p1" or $white eq "p2"
$white eq "p1"
$white eq "p2"
Truth table100%3/3
A or B
A= $white eq "p1"B= $white eq "p2"
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | unless $white eq 'p1' || $white eq 'p2'; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 222 | my $fen = $self->_fen; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 222 | ▶ | my $pos = eval { defined $fen ? $P->new($fen) : $P->new }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 222 | ▶ | if (!$pos) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | my $why = $@; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | $why =~ s/\s+at \S+ line \d+\.?\s*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | Carp::croak("Term::Chess::Game: Plisco refused the position: $why"); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 218 | $self->_pos($pos); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 218 | $self->_start_fen($pos->toFEN); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 218 | $self->_first_halfmoves($pos->halfmoves); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 218 | push @{ $self->_signatures }, $pos->signature; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 218 | $self->_result({ winner => undef, reason => undef }); | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 218 | $self->_settle; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 218 | return; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 582 | ▶ | sub other { $_[0] eq 'p1' ? 'p2' : 'p1' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
other: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 819 | ▶ | sub position { $_[0]->_pos } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 239 | ▶ | sub signatures { [ @{ $_[0]->_signatures } ] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signatures: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 111 | ▶ | sub log { [ @{ $_[0]->_log } ] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 749 | ▶ | sub _seat_of { $_[1] == CP_WHITE ? $_[0]->white : other($_[0]->white) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_seat_of: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 224 | ▶ | sub black { other($_[0]->white) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
black: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 750 | ▶ | sub turn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
turn: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 750 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 750 | ▶ | return undef unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 749 | return $self->_seat_of($self->_pos->turn); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 68 | ▶ | sub side { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
side: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 68 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 68 | ▶ | return $self->_pos->turn == CP_WHITE ? 'white' : 'black'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 41 | ▶ | sub legal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 41 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 41 | ▶ | return [] unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 32 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 32 | return [ map { $pos->moveCoordinateNotation($_) } $pos->legalMoves ]; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | sub seed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seed: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | ▶ | return undef unless $self->status eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | return $self->_held_seed; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 33 | ▶ | sub san_log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
san_log: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 33 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 33 | my @san = @{ $self->_san }; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 33 | ▶ | return '' unless @san; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 27 | my $ply = $self->_first_halfmoves; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 27 | my $number = int($ply / 2) + 1; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 27 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 27 | ▶ | if ($ply % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | push @out, "$number... " . shift @san; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | $number++; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 27 | ▶ | while (@san) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 207 | my $pair = "$number. " . shift @san; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 207 | ▶ | $pair .= ' ' . shift @san if @san; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 207 | push @out, $pair; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 207 | $number++; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 27 | return join ' ', @out; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 849 | ▶ | sub play { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
play: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 849 | my ($self, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 849 | ▶ | return $R->throw('game_over') unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 847 | ▶ | return $R->throw('bad_move') unless defined $text && !ref $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $text and not ref $text
MC/DC100%2/2
defined $text and not ref $text
defined $text
not ref $text
Truth table100%3/3
A and not(B)
A= defined $textB= not ref $text
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 845 | $text =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 845 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 845 | my $uci; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 845 | ▶ | if ($text =~ /\A[a-h][1-8][a-h][1-8][qrbnQRBN]?\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 814 | $uci = lc $text; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 31 | my $m = eval { $pos->parseMove($text) }; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 31 | ▶ | return $R->throw('bad_move') unless $m; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 16 | $uci = $pos->moveCoordinateNotation($m); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 830 | my $m = $self->_find($uci); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 830 | ▶ | return $m unless ref $m eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 812 | push @{ $self->_san }, $pos->SAN($m); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 812 | push @{ $self->_log }, $uci; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 812 | $pos->doMove($m); | |||||||||||||||||||||||||||||||||||||||||
| 155 | 812 | push @{ $self->_signatures }, $pos->signature; | |||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 812 | $self->_settle; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 812 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 830 | ▶ | sub _find { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 830 | my ($self, $uci) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 830 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 830 | my @legal = $pos->legalMoves; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 830 | for my $m (@legal) { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 10529 | ▶ | return $m if $pos->moveCoordinateNotation($m) eq $uci; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 18 | my ($from, $to) = (substr($uci, 0, 2), substr($uci, 2, 2)); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 18 | my ($piece, $colour) = $pos->pieceAtSquare($from); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 18 | ▶ | return $R->throw('no_piece') unless $piece; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 14 | ▶ | return $R->throw('not_your_turn') unless $colour == $pos->turn; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | return $R->throw('bad_promotion') | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 11 | ▶ | if grep { substr($pos->moveCoordinateNotation($_), 0, 4) eq "$from$to" } @legal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 10 | my @coords = map { $pos->moveCoordinateNotation($_) } @legal; | |||||||||||||||||||||||||||||||||||||||||
| 178 | return $R->throw('in_check', legal => \@coords) | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 10 | ▶ | if grep { $pos->moveCoordinateNotation($_) eq $uci } $pos->pseudoLegalMoves; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | return $R->throw('not_legal', legal => \@coords); | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 1030 | ▶ | sub _settle { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_settle: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 1030 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 1030 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 1030 | my $state = $pos->gameOver; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 1030 | ▶ | if ($state & CP_GAME_OVER) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 30 | ▶ | if ($state & CP_GAME_WHITE_WINS) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 8 | return $self->_finish($self->white, 'checkmate'); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 22 | ▶ | if ($state & CP_GAME_BLACK_WINS) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 10 | return $self->_finish($self->black, 'checkmate'); | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 12 | for my $flag (CP_GAME_STALEMATE, CP_GAME_FIFTY_MOVES, CP_GAME_INSUFFICIENT_MATERIAL) { | |||||||||||||||||||||||||||||||||||||||||
| 196 | 27 | ▶ | return $self->_finish(undef, $REASON{$flag}) if $state & $flag; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | return $self->_finish(undef, 'over'); | |||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1000 | my $now = $self->_signatures->[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 1000 | my $seen = grep { $_ == $now } @{ $self->_signatures }; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 1000 | ▶ | return $self->_finish(undef, 'repetition') if $seen >= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 996 | return; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 41 | ▶ | sub _finish { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_finish: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 41 | my ($self, $winner, $reason) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 41 | $self->status('finished'); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 41 | $self->_result({ winner => $winner, reason => $reason }); | |||||||||||||||||||||||||||||||||||||||||
| 211 | 41 | return; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 9 | ▶ | sub resign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resign: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 9 | my ($self, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 9 | ▶ | return $R->throw('game_over') unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 8 | ▶ | return $R->throw('bad_move') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $seat and $seat eq "p1" || $seat eq "p2"
$seat eq "p1" or $seat eq "p2"
MC/DC66%2/3
defined $seat and $seat eq "p1" || $seat eq "p2"
defined $seat
$seat eq "p1"
$seat eq "p2"
Truth table75%3/4
A and B or C
A= defined $seatB= $seat eq "p1"C= $seat eq "p2"
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | unless defined $seat && ($seat eq 'p1' || $seat eq 'p2'); | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 7 | $self->_finish(other($seat), 'resignation'); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 7 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 50 | ▶ | sub result { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
result: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 50 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 50 | return { %{ $self->_result } }; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | ▶ | sub winner { $_[0]->result->{winner} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
winner: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 25 | ▶ | sub bot { require Term::Chess::Bot; 'Term::Chess::Bot' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bot: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 12 | ▶ | sub replay { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replay: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 12 | my ($proto, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 12 | ▶ | if (ref $proto) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | return $proto->_replayed($moves); | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 7 | ▶ | return undef if @rest % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 5 | my %o = @rest; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 5 | my $self = $proto->new(%o); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 5 | ▶ | return $self->_walk($o{moves} || []); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'moves'} || []
MC/DC0%0/1
$o{'moves'} || []
$o{'moves'}
Truth table50%1/2
A
A= $o{'moves'}
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 5 | ▶ | sub _replayed { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_replayed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 5 | my ($self, $moves) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 5 | my $fresh = (ref $self)->new( | |||||||||||||||||||||||||||||||||||||||||
| 249 | seed => $self->_held_seed, | ||||||||||||||||||||||||||||||||||||||||||
| 250 | white => $self->white, | ||||||||||||||||||||||||||||||||||||||||||
| 251 | position => $self->_start_fen, | ||||||||||||||||||||||||||||||||||||||||||
| 252 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 5 | return $fresh->_walk($moves); | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 41 | ▶ | sub _walk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walk: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 41 | my ($self, $moves) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 41 | for my $i (0 .. $#$moves) { | |||||||||||||||||||||||||||||||||||||||||
| 259 | 494 | my $why = $self->play($moves->[$i]); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 494 | ▶ | return wantarray ? ($self, $why, $i) : undef if $why; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 38 | ▶ | return wantarray ? ($self, 0, scalar @$moves) : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 190 | ▶ | sub to_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_text: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 190 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 190 | my $result = $self->_result; | |||||||||||||||||||||||||||||||||||||||||
| 268 | return join "\n", | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 'chess 1', | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 'seed ' . unpack('H*', $self->_held_seed), | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 'white ' . $self->white, | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 'start ' . $self->_start_fen, | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 190 | ▶ | join(' ', 'moves', @{ $self->_log }), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$result->{'reason'} || ''
MC/DC100%1/1
$result->{'reason'} || ''
$result->{'reason'}
Truth table100%2/2
A
A= $result->{'reason'}
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 190 | 'resigned ' . (($result->{reason} || '') eq 'resignation' ? other($result->{winner}) : '-'), | |||||||||||||||||||||||||||||||||||||||||
| 275 | 'status ' . $self->status, | ||||||||||||||||||||||||||||||||||||||||||
| 276 | ''; | ||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 43 | ▶ | sub from_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_text: called CC=19 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 43 | my ($class, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 43 | ▶ | Carp::croak('Term::Chess::Game: from_text needs the text to_text wrote, and was given ' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
not defined $text or ref $text
MC/DC100%2/2
not defined $text or ref $text
not defined $text
ref $text
Truth table100%3/3
not(A) or B
A= not defined $textB= ref $text
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | . (defined $text ? 'a reference' : 'nothing')) | ||||||||||||||||||||||||||||||||||||||||||
| 283 | if !defined $text || ref $text; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 41 | my @lines = split /\n/, $text; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 41 | ▶ | Carp::croak('Term::Chess::Game: the text is not a saved game of chess, version 1') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@lines and $lines[0] eq "chess 1"
MC/DC100%2/2
@lines and $lines[0] eq "chess 1"
@lines
$lines[0] eq "chess 1"
Truth table100%3/3
A and B
A= @linesB= $lines[0] eq "chess 1"
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | unless @lines && $lines[0] eq 'chess 1'; | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 39 | my %field; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 39 | for my $line (@lines[1 .. $#lines]) { | |||||||||||||||||||||||||||||||||||||||||
| 291 | 235 | ▶ | my ($name, $value) = $line =~ /\A(seed|white|start|moves|resigned|status)(?: (.*))?\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | or Carp::croak("Term::Chess::Game: the saved game has a line it does not know: '$line'"); | ||||||||||||||||||||||||||||||||||||||||||
| 293 | Carp::croak("Term::Chess::Game: the saved game gives $name twice") | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 234 | ▶ | if exists $field{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 233 | ▶ | $field{$name} = defined $value ? $value : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 37 | for my $name (qw/seed white start moves resigned status/) { | |||||||||||||||||||||||||||||||||||||||||
| 298 | Carp::croak("Term::Chess::Game: the saved game has no $name line") | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 218 | ▶ | unless exists $field{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | Carp::croak('Term::Chess::Game: the saved seed is not sixty-four hex digits') | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 36 | ▶ | unless $field{seed} =~ /\A[0-9a-f]{64}\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | Carp::croak("Term::Chess::Game: the saved resignation must be 'p1', 'p2' or '-', not '$field{resigned}'") | ||||||||||||||||||||||||||||||||||||||||||
| 305 | 35 | ▶ | unless $field{resigned} =~ /\A(?:p1|p2|-)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | Carp::croak("Term::Chess::Game: the saved status must be 'active' or 'finished', not '$field{status}'") | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 34 | ▶ | unless $field{status} eq 'active' || $field{status} eq 'finished'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$field{"status"} eq "active" or $field{"status"} eq "finished"
MC/DC100%2/2
$field{"status"} eq "active" or $field{"status"} eq "finished"
$field{"status"} eq "active"
$field{"status"} eq "finished"
Truth table100%3/3
A or B
A= $field{"status"} eq "active"B= $field{"status"} eq "finished"
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | my $self = $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 310 | seed => pack('H*', $field{seed}), | ||||||||||||||||||||||||||||||||||||||||||
| 311 | white => $field{white}, | ||||||||||||||||||||||||||||||||||||||||||
| 312 | position => $field{start}, | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 33 | ); | |||||||||||||||||||||||||||||||||||||||||
| 314 | 31 | my @moves = split ' ', $field{moves}; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 31 | my (undef, $why, $at) = $self->_walk(\@moves); | |||||||||||||||||||||||||||||||||||||||||
| 316 | 31 | ▶ | Carp::croak('Term::Chess::Game: the saved move ' . ($at + 1) . ", $moves[$at], was refused: " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | . $why->code) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | if $why; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 30 | ▶ | if ($field{resigned} ne '-') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | my $refused = $self->resign($field{resigned}); | |||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | ▶ | Carp::croak("Term::Chess::Game: the saved resignation by $field{resigned} was refused: " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | . $refused->code) | ||||||||||||||||||||||||||||||||||||||||||
| 324 | if $refused; | ||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 30 | $self->status($field{status}); | |||||||||||||||||||||||||||||||||||||||||
| 328 | 30 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | __END__ | ||||||||||||||||||||||||||||||||||||||||||