Chess/Game.pm

filter: Statementstmt 98.9% 182 / 184 Branchbran 98.6% 73 / 74 Conditioncond 76.4% 13 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 31 / 31 Podpod 94.1% 16 / 17 totaltotal 96.4% 321 / 333 CC 47 SCAR 14.8
CC
47
Cov
97%
_settle
7.0
BUILD
7.0
play
7.0
CRAP 4.4
« Chess.pm ↑ Summary Chess/Bot.pm »
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
BranchTrueFalseTotal
unless defined $seed and length $seed == 32 2 165 167
Condition66%2/3
defined $seed and length $seed == 32
!ll&&!rl&&r
02165
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $white eq "p1" or $white eq "p2" 1 164 165
Condition100%3/3
$white eq "p1" or $white eq "p2"
l!l&&r!l&&!r
15681
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"
ABresult
1X1
011
000
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
BranchTrueFalseTotal
defined $fen ? : 72 92 164
60 164 ▶ if (!$pos) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos) 3 161 164
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
BranchTrueFalseTotal
$_[0] eq 'p1' ? : 205 6 211
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
BranchTrueFalseTotal
$_[1] == 0 ? : 205 133 338
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
BranchTrueFalseTotal
unless $self->status eq "active" 1 338 339
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
BranchTrueFalseTotal
$self->_pos->turn == 0 ? : 53 3 56
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
BranchTrueFalseTotal
unless $self->status eq "active" 1 14 15
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
BranchTrueFalseTotal
unless $self->status eq "finished" 1 1 2
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
BranchTrueFalseTotal
unless @san 2 11 13
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
BranchTrueFalseTotal
if ($ply % 2) 3 8 11
119 3 push @out, "$number... " . shift @san;
120 3 $number++;
121 }
122 11 ▶ while (@san) {
Branch: 1 branch
BranchTrueFalseTotal
if (@san) 17 11 28
123 17 my $pair = "$number. " . shift @san;
124 17 ▶ $pair .= ' ' . shift @san if @san;
Branch: 1 branch
BranchTrueFalseTotal
if @san 13 4 17
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
BranchTrueFalseTotal
unless $self->status eq "active" 2 225 227
134 225 ▶ return $R->throw('bad_move') unless defined $text && !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 2 223 225
Condition100%3/3
defined $text and not ref $text
!ll&&!rl&&r
11223
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($text =~ /\A[a-h][1-8][a-h][1-8][qrbnQRBN]?\z/u) { } 192 31 223
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
BranchTrueFalseTotal
unless $m 15 16 31
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
BranchTrueFalseTotal
unless ref $m eq "" 17 191 208
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
BranchTrueFalseTotal
if $pos->moveCoordinateNotation($m) eq $uci 191 2665 2856
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
BranchTrueFalseTotal
unless $piece 3 14 17
172 14 ▶ return $R->throw('not_your_turn') unless $colour == $pos->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $pos->turn 3 11 14
173  
174 return $R->throw('bad_promotion')
175 11 ▶ if grep { substr($pos->moveCoordinateNotation($_), 0, 4) eq "$from$to" } @legal;
Branch: 1 branch
BranchTrueFalseTotal
if grep {substr($pos->moveCoordinateNotation($_), 0, 4) eq "$from$to";} @legal 1 10 11
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
BranchTrueFalseTotal
if grep {$pos->moveCoordinateNotation($_) eq $uci;} $pos->pseudoLegalMoves 2 8 10
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
BranchTrueFalseTotal
if ($state & 1) 24 328 352
189 24 ▶ if ($state & CP_GAME_WHITE_WINS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state & 2) 7 17 24
190 7 return $self->_finish($self->white, 'checkmate');
191 }
192 17 ▶ if ($state & CP_GAME_BLACK_WINS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state & 4) 5 12 17
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
BranchTrueFalseTotal
if $state & $flag 12 15 27
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
BranchTrueFalseTotal
if $seen >= 3 1 327 328
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
BranchTrueFalseTotal
unless $self->status eq "active" 1 5 6
217 5 ▶ return $R->throw('bad_move')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat eq "p1" || $seat eq "p2" 1 4 5
Condition66%4/6
defined $seat and $seat eq "p1" || $seat eq "p2"
!ll&&!rl&&r
014
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
401
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"
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
if (ref $proto) 5 6 11
236 5 ▶ my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ];
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[0] eq 'ARRAY' ? : 5 0 5
237 5 return $proto->_replayed($moves);
238 }
239  
240 6 ▶ return undef if @rest % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 2 4 6
241 4 my %o = @rest;
242 4 my $self = $proto->new(%o);
243 4 ▶ return $self->_walk($o{moves} || []);
Condition50%1/2
$o{'moves'} || []
l!l
40
MC/DC0%0/1
$o{'moves'} || []
$o{'moves'}
Truth table50%1/2
A
A= $o{'moves'}
Aresult
11
00
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
BranchTrueFalseTotal
if $why 2 34 36
wantarray ? : 1 1 2
261 }
262 7 ▶ return wantarray ? ($self, 0, scalar @$moves) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 3 7
263 }
264  
265 7 1;
266  
267 __END__
« Chess.pm ↑ Summary Chess/Bot.pm »