| 1 | package Term::Chess::Game; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Chess::Plisco qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Chess::Plisco::Engine::Position; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Term::Chess::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | my $P = 'Chess::Plisco::Engine::Position'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | my $R = 'Term::Chess::Error'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | my %REASON = ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | CP_GAME_STALEMATE() => 'stalemate', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | CP_GAME_FIFTY_MOVES() => 'fifty_moves', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | CP_GAME_INSUFFICIENT_MATERIAL() => 'insufficient_material', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | has white => (is => 'ro', isa => Str, default => 'p1'); | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | has status => (is => 'rw', isa => Str, default => 'active'); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | has _held_seed => (is => 'ro', init_arg => 'seed'); | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | has _fen => (is => 'ro', init_arg => 'position'); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 7 | has _pos => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 7 | has _start_fen => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 7 | has _first_halfmoves => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | has _signatures => (is => 'ro', isa => ArrayRef, default => [], private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | has _log => (is => 'ro', isa => ArrayRef, default => [], private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | has _san => (is => 'ro', isa => ArrayRef, default => [], private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | has _result => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 167 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 167 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 167 | my $seed = $self->_held_seed; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 167 | ▶ | 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 | 165 | my $white = $self->white; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 165 | ▶ | 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 | 164 | my $fen = $self->_fen; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 164 | ▶ | my $pos = eval { defined $fen ? $P->new($fen) : $P->new }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 164 | ▶ | if (!$pos) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | my $why = $@; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 3 | $why =~ s/\s+at \S+ line \d+\.?\s*\z//; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | Carp::croak("Term::Chess::Game: Plisco refused the position: $why"); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 161 | $self->_pos($pos); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 161 | $self->_start_fen($pos->toFEN); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 161 | $self->_first_halfmoves($pos->halfmoves); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 161 | push @{ $self->_signatures }, $pos->signature; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 161 | $self->_result({ winner => undef, reason => undef }); | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 161 | $self->_settle; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 161 | return; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 211 | ▶ | sub other { $_[0] eq 'p1' ? 'p2' : 'p1' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
other: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 418 | ▶ | sub position { $_[0]->_pos } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 94 | ▶ | sub signatures { [ @{ $_[0]->_signatures } ] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signatures: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 22 | ▶ | sub log { [ @{ $_[0]->_log } ] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 338 | ▶ | 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 | 74 | ▶ | sub black { other($_[0]->white) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
black: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 339 | ▶ | sub turn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
turn: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 339 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 339 | ▶ | return undef unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 338 | return $self->_seat_of($self->_pos->turn); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 56 | ▶ | sub side { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
side: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 56 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 56 | ▶ | return $self->_pos->turn == CP_WHITE ? 'white' : 'black'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 15 | ▶ | sub legal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 15 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 15 | ▶ | return [] unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 14 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 14 | 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 | 13 | ▶ | sub san_log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
san_log: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 13 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 13 | my @san = @{ $self->_san }; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 13 | ▶ | return '' unless @san; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 11 | my $ply = $self->_first_halfmoves; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 11 | my $number = int($ply / 2) + 1; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 11 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 11 | ▶ | if ($ply % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | push @out, "$number... " . shift @san; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | $number++; | |||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 11 | ▶ | while (@san) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 17 | my $pair = "$number. " . shift @san; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 17 | ▶ | $pair .= ' ' . shift @san if @san; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 17 | push @out, $pair; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 17 | $number++; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 11 | return join ' ', @out; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 227 | ▶ | sub play { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
play: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 227 | my ($self, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 227 | ▶ | return $R->throw('game_over') unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 225 | ▶ | 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 | 223 | $text =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 223 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 223 | my $uci; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 223 | ▶ | if ($text =~ /\A[a-h][1-8][a-h][1-8][qrbnQRBN]?\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 192 | $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 | 208 | my $m = $self->_find($uci); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 208 | ▶ | return $m unless ref $m eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 191 | push @{ $self->_san }, $pos->SAN($m); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 191 | push @{ $self->_log }, $uci; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 191 | $pos->doMove($m); | |||||||||||||||||||||||||||||||||||||||||
| 155 | 191 | push @{ $self->_signatures }, $pos->signature; | |||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 191 | $self->_settle; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 191 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 208 | ▶ | sub _find { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 208 | my ($self, $uci) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 208 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 208 | my @legal = $pos->legalMoves; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 208 | for my $m (@legal) { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 2856 | ▶ | return $m if $pos->moveCoordinateNotation($m) eq $uci; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 17 | my ($from, $to) = (substr($uci, 0, 2), substr($uci, 2, 2)); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 17 | my ($piece, $colour) = $pos->pieceAtSquare($from); | |||||||||||||||||||||||||||||||||||||||||
| 171 | 17 | ▶ | 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 | 352 | ▶ | sub _settle { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_settle: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 352 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 352 | my $pos = $self->_pos; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 352 | my $state = $pos->gameOver; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 352 | ▶ | if ($state & CP_GAME_OVER) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 24 | ▶ | if ($state & CP_GAME_WHITE_WINS) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 7 | return $self->_finish($self->white, 'checkmate'); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 17 | ▶ | if ($state & CP_GAME_BLACK_WINS) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 5 | 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 | 328 | my $now = $self->_signatures->[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 328 | my $seen = grep { $_ == $now } @{ $self->_signatures }; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 328 | ▶ | return $self->_finish(undef, 'repetition') if $seen >= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 327 | return; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 29 | ▶ | sub _finish { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_finish: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 29 | my ($self, $winner, $reason) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 209 | 29 | $self->status('finished'); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 29 | $self->_result({ winner => $winner, reason => $reason }); | |||||||||||||||||||||||||||||||||||||||||
| 211 | 29 | return; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 6 | ▶ | sub resign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resign: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | my ($self, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 6 | ▶ | return $R->throw('game_over') unless $self->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 5 | ▶ | return $R->throw('bad_move') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $seat and $seat eq "p1" || $seat eq "p2"
$seat eq "p1" or $seat eq "p2"
MC/DC33%1/3
defined $seat and $seat eq "p1" || $seat eq "p2"
defined $seat
$seat eq "p1"
$seat eq "p2"
Truth table50%2/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 | 4 | $self->_finish(other($seat), 'resignation'); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 28 | ▶ | sub result { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
result: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 28 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 28 | return { %{ $self->_result } }; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 4 | ▶ | sub winner { $_[0]->result->{winner} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
winner: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 46 | ▶ | sub bot { require Term::Chess::Bot; 'Term::Chess::Bot' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bot: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 11 | ▶ | sub replay { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replay: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 11 | my ($proto, @rest) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 11 | ▶ | if (ref $proto) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | ▶ | my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 5 | return $proto->_replayed($moves); | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 6 | ▶ | return undef if @rest % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 4 | my %o = @rest; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 4 | my $self = $proto->new(%o); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 4 | ▶ | 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 | 9 | ▶ | sub _walk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walk: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 9 | my ($self, $moves) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 9 | for my $i (0 .. $#$moves) { | |||||||||||||||||||||||||||||||||||||||||
| 259 | 36 | my $why = $self->play($moves->[$i]); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 36 | ▶ | return wantarray ? ($self, $why, $i) : undef if $why; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 7 | ▶ | return wantarray ? ($self, 0, scalar @$moves) : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | __END__ | ||||||||||||||||||||||||||||||||||||||||||