blib/lib/Game/Xiangqi.pm

filter: Statementstmt 98.2% 171 / 174 Branchbran 90.5% 105 / 116 Conditioncond 71.8% 23 / 32 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 93.1% 27 / 29 Podpod 92.8% 13 / 14 totaltotal 90.9% 350 / 385 CC 74 SCAR 26.0
CC
74
Cov
93%
_refuse
30.5
from_text
18.0
_settle
8.0
CRAP 13.4
« blib/lib/Game/Xiangqi/Terminal.pm ↑ Summary blib/lib/Game/Xiangqi/Notation.pm »
1 package Game::Xiangqi;
2  
3 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use Game::Xiangqi::Engine ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Game::Xiangqi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Game::Xiangqi::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 our $VERSION = '0.05';
15  
16 13 my $E = 'Game::Xiangqi::Engine';
17 13 my $N = 'Game::Xiangqi::Notation';
18 13 my $R = 'Game::Xiangqi::Error';
19  
20 13 has red => (is => 'ro', isa => Str, default => 'p1');
21  
22 13 has status => (is => 'rw', isa => Str, default => 'active');
23  
24 13 has _held_seed => (is => 'ro', init_arg => 'seed');
25  
26 13 has _pos => (is => 'rw', init_arg => 'position', private => 1);
27  
28 13 has _start => (is => 'rw', private => 1);
29  
30 13 has _log => (is => 'ro', isa => ArrayRef, default => [], private => 1);
31  
32 13 has _played => (is => 'ro', isa => ArrayRef, default => [], private => 1);
33  
34 13 has _result => (is => 'rw', private => 1);
35  
36 136 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
37 136 my ($self) = @_;
38  
39 136 my $seed = $self->_held_seed;
40 136 ▶ Carp::croak('Game::Xiangqi: seed must be exactly thirty-two bytes')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and length $seed == 32 2 134 136
Condition100%3/3
defined $seed and length $seed == 32
!ll&&!rl&&r
11134
MC/DC100%2/2
defined $seed and length $seed == 32
defined $seed length $seed == 32
Truth table100%3/3
A and B
A= defined $seedB= length $seed == 32
ABresult
0X0
100
111
41 unless defined $seed && length($seed) == 32;
42  
43 134 my $red = $self->red;
44 134 ▶ Carp::croak("Game::Xiangqi: red must be 'p1' or 'p2', not '$red'")
Branch: 1 branch
BranchTrueFalseTotal
unless $red eq "p1" or $red eq "p2" 2 132 134
Condition100%3/3
$red eq "p1" or $red eq "p2"
l!l&&r!l&&!r
120122
MC/DC100%2/2
$red eq "p1" or $red eq "p2"
$red eq "p1" $red eq "p2"
Truth table100%3/3
A or B
A= $red eq "p1"B= $red eq "p2"
ABresult
1X1
011
000
45 unless $red eq 'p1' || $red eq 'p2';
46  
47 132 ▶ my $pos = $self->_pos ? $self->_pos->clone : $E->new;
Branch: 1 branch
BranchTrueFalseTotal
$self->_pos ? : 56 76 132
48 132 $self->_pos($pos);
49 132 $self->_start($pos->clone);
50 132 $self->_result({ winner => undef, reason => undef, rule => 0, loop => undef });
51  
52 132 $self->_settle;
53 132 return;
54 }
55  
56 544 ▶sub position { $_[0]->_pos }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
57 34 ▶sub signature { $_[0]->_pos->key_hex }
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
58 105 ▶sub log { [ @{ $_[0]->_log } ] }
Subroutine log: called CC=1 SCAR=0.0
Pod documented
59  
60 574 ▶sub _seat_of { $_[1] == RED ? $_[0]->red : ($_[0]->red eq 'p1' ? 'p2' : 'p1') }
Subroutine _seat_of: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
$_[1] == 8 ? : 306 268 574
$_[0]->red eq 'p1' ? : 260 8 268
61 0 ▶sub _colour_of { $_[1] eq $_[0]->red ? RED : BLACK }
Subroutine _colour_of: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq $_[0]->red ? : 0 0 0
62  
63 561 ▶sub turn {
Subroutine turn: called CC=2 SCAR=6.9
Pod documented
64 561 my ($self) = @_;
65 561 ▶ return undef unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 6 555 561
66 555 return $self->_seat_of($self->_pos->side);
67 }
68  
69 39 ▶sub legal {
Subroutine legal: called CC=2 SCAR=6.9
Pod documented
70 39 my ($self) = @_;
71 39 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 7 32 39
72 32 return [ map { $N->iccs_of($_) } $self->_pos->legal ];
73 }
74  
75 4 ▶sub seed {
Subroutine seed: called CC=2 SCAR=6.9
Pod documented
76 4 my ($self) = @_;
77 4 ▶ return undef unless $self->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "finished" 2 2 4
78 2 return $self->_held_seed;
79 }
80  
81 414 ▶sub play {
Subroutine play: called CC=1 SCAR=0.0
Pod documented
82 414 my ($self, $iccs) = @_;
83 414 return $self->_play($iccs, 1);
84 }
85  
86 677 ▶sub _play {
Subroutine _play: called CC=4 SCAR=13.9
87 677 my ($self, $iccs, $judge) = @_;
88 677 ▶ return $R->throw('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 676 677
89  
90 676 my $mv = $N->move_of_iccs($iccs);
91 676 ▶ return $R->throw('bad_move') unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 10 666 676
92  
93 666 my $why = $self->_refuse($mv);
94 666 ▶ return $why if $why;
Branch: 1 branch
BranchTrueFalseTotal
if $why 33 633 666
95  
96 633 push @{ $self->_log }, $N->iccs_of($mv);
97 633 push @{ $self->_played }, $mv;
98 633 $self->_pos->do_move($mv);
99  
100 633 $self->_settle($judge);
101 633 return 0;
102 }
103  
104 765 ▶sub _settle {
Subroutine _settle: called CC=8 SCAR=20.8
105 765 my ($self, $judge) = @_;
106 765 ▶ $judge = 1 unless defined $judge;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $judge 132 633 765
107  
108 765 my ($winner, $reason) = $self->_pos->outcome;
109 765 ▶ if ($winner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($winner) 9 756 765
110 9 $self->status('finished');
111 9 ▶ $self->_result({
Branch: 1 branch
BranchTrueFalseTotal
$reason == 1 ? : 4 5 9
112 winner => $self->_seat_of($winner),
113 reason => $reason == BY_CHECKMATE ? 'checkmate' : 'stalemate',
114 rule => 0,
115 loop => undef,
116 });
117 9 return;
118 }
119 756 ▶ return unless $judge;
Branch: 1 branch
BranchTrueFalseTotal
unless $judge 242 514 756
120  
121 514 my $v = $self->_start->judge($self->_played);
122 514 ▶ return unless $v->{reason} != J_ONGOING;
Branch: 1 branch
BranchTrueFalseTotal
unless $v->{"reason"} != 0 503 11 514
123  
124 11 my %name = (
125 J_PERPETUAL_CHECK() => 'perpetual_check',
126 J_PERPETUAL_CHASE() => 'perpetual_chase',
127 J_MUTUAL() => 'mutual',
128 J_NO_VIOLATION() => 'no_violation',
129 J_EFFECTIVE() => 'effective',
130 J_PROGRESS() => 'progress',
131 J_MOVES() => 'moves',
132 );
133 11 $self->status('finished');
134 $self->_result({
135 winner => $v->{winner} ? $self->_seat_of($v->{winner}) : undef,
136 reason => $name{ $v->{reason} },
137 rule => $v->{rule},
138 loop => $v->{loop_from} >= 0
139 11 ▶ ? [ @{ $self->_log }[ $v->{loop_from} .. $#{ $self->_log } ] ]
Branch: 2 branches
BranchTrueFalseTotal
$v->{'winner'} ? : 10 1 11
$v->{'loop_from'} >= 0 ? : 11 0 11
140 : undef,
141 });
142 }
143  
144 666 ▶sub _refuse {
Subroutine _refuse: called CC=27 SCAR=34.2
145 666 my ($self, $mv) = @_;
146 666 my $p = $self->_pos;
147 666 my ($from, $to) = ($E->move_from($mv), $E->move_to($mv));
148  
149 666 my $piece = $p->at($from);
150 666 ▶ return $R->throw('no_piece')
Branch: 1 branch
BranchTrueFalseTotal
unless $piece and $piece != 32 and Game::Xiangqi::Engine::kind_of($piece) 4 662 666
Condition66%4/6
$piece and $piece != 32 and Game::Xiangqi::Engine::kind_of($piece)
!ll&&!rl&&r
40662
$piece and $piece != 32
!ll&&!rl&&r
40662
MC/DC33%1/3
$piece and $piece != 32 and Game::Xiangqi::Engine::kind_of($piece)
$piece $piece != 32 Game::Xiangqi::Engine::kind_of($piece)
Truth table50%2/4
A and B and C
A= $pieceB= $piece != 32C= Game::Xiangqi::Engine::kind_of($piece)
ABCresult
0XX0
10X0
1100
1111
151 unless $piece && $piece != BORDER && Game::Xiangqi::Engine::kind_of($piece);
152 662 ▶ return $R->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Xiangqi::Engine::colour_of($piece) == $p->side 4 658 662
153 unless Game::Xiangqi::Engine::colour_of($piece) == $p->side;
154  
155 658 ▶ return 0 if grep { $_ == $mv } $p->legal;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $mv;} $p->legal 633 25 658
156  
157 25 ▶ if (grep { $_ == $mv } $p->moves) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ == $mv;} $p->moves) 2 23 25
158 2 my (undef, $u) = $p->do_move($mv);
159 2 my $face = $p->generals_face;
160 2 $p->undo_move($u);
161 2 ▶ return $R->throw($face ? 'generals_face' : 'in_check', legal => $self->legal);
Branch: 1 branch
BranchTrueFalseTotal
$face ? : 1 1 2
162 }
163  
164 23 my $kind = Game::Xiangqi::Engine::kind_of($piece);
165 23 my $colour = Game::Xiangqi::Engine::colour_of($piece);
166  
167 23 ▶ return $R->throw('own_piece')
Branch: 1 branch
BranchTrueFalseTotal
if Game::Xiangqi::Engine::colour_of($p->at($to)) == $colour 6 17 23
168 if Game::Xiangqi::Engine::colour_of($p->at($to)) == $colour;
169  
170 17 ▶ if ($kind == GENERAL) {
Branch: 6 branches
BranchTrueFalseTotal
if ($kind == 1) { } 1 16 17
elsif ($kind == 2) { } 1 15 16
elsif ($kind == 3) { } 2 13 15
elsif ($kind == 5) { } 2 11 13
elsif ($kind == 6) { } 1 10 11
elsif ($kind == 7) { } 4 6 10
171 1 ▶ return $R->throw('general_leaves_palace') unless $E->in_palace($to, $colour);
Branch: 1 branch
BranchTrueFalseTotal
unless $E->in_palace($to, $colour) 1 0 1
172 }
173 elsif ($kind == ADVISOR) {
174 1 ▶ return $R->throw('advisor_leaves_palace') unless $E->in_palace($to, $colour);
Branch: 1 branch
BranchTrueFalseTotal
unless $E->in_palace($to, $colour) 1 0 1
175 }
176 elsif ($kind == ELEPHANT) {
177 2 ▶ return $R->throw('elephant_crosses_river') if $E->crossed_river($to, $colour);
Branch: 1 branch
BranchTrueFalseTotal
if $E->crossed_river($to, $colour) 1 1 2
178 1 my $eye = $p->elephant_eye($from, $to);
179 1 ▶ return $R->throw('elephant_eye_blocked') if $eye >= 0 && $p->at($eye) != EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $eye >= 0 and $p->at($eye) != 0 1 0 1
Condition33%1/3
$eye >= 0 and $p->at($eye) != 0
!ll&&!rl&&r
001
MC/DC0%0/2
$eye >= 0 and $p->at($eye) != 0
$eye >= 0 $p->at($eye) != 0
Truth table33%1/3
A and B
A= $eye >= 0B= $p->at($eye) != 0
ABresult
0X0
100
111
180 }
181 elsif ($kind == HORSE) {
182 2 my $leg = $p->horse_leg($from, $to);
183 2 ▶ return $R->throw('horse_leg_blocked') if $leg >= 0 && $p->at($leg) != EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $leg >= 0 and $p->at($leg) != 0 2 0 2
Condition33%1/3
$leg >= 0 and $p->at($leg) != 0
!ll&&!rl&&r
002
MC/DC0%0/2
$leg >= 0 and $p->at($leg) != 0
$leg >= 0 $p->at($leg) != 0
Truth table33%1/3
A and B
A= $leg >= 0B= $p->at($leg) != 0
ABresult
0X0
100
111
184 }
185 elsif ($kind == CANNON) {
186 1 my $n = $p->cannon_screens($from, $to);
187 1 ▶ return $R->throw('cannon_needs_one_screen') if $n >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= 0 1 0 1
188 }
189 elsif ($kind == SOLDIER) {
190 4 my ($fr, $tr) = ($E->rank_of($from), $E->rank_of($to));
191 4 ▶ my $back = $colour == RED ? $tr < $fr : $tr > $fr;
Branch: 1 branch
BranchTrueFalseTotal
$colour == 8 ? : 4 0 4
192 4 ▶ return $R->throw('soldier_no_retreat') if $back;
Branch: 1 branch
BranchTrueFalseTotal
if $back 2 2 4
193 2 ▶ return $R->throw('soldier_no_sideways')
Branch: 1 branch
BranchTrueFalseTotal
if $fr == $tr and not $E->crossed_river($from, $colour) 2 0 2
Condition33%1/3
$fr == $tr and not $E->crossed_river($from, $colour)
!ll&&!rl&&r
002
MC/DC0%0/2
$fr == $tr and not $E->crossed_river($from, $colour)
$fr == $tr not $E->crossed_river($from, $colour)
Truth table33%1/3
A and not(B)
A= $fr == $trB= not $E->crossed_river($from, $colour)
ABresult
0X0
100
111
194 if $fr == $tr && !$E->crossed_river($from, $colour);
195 }
196 6 return $R->throw('not_legal', legal => $self->legal);
197 }
198  
199 33 ▶sub result {
Subroutine result: called CC=2 SCAR=6.9
Pod documented
200 33 my ($self) = @_;
201 33 ▶ return { winner => undef, reason => undef, rule => 0, loop => undef }
Branch: 1 branch
BranchTrueFalseTotal
if $self->status eq "active" 15 18 33
202 if $self->status eq 'active';
203 18 return { %{ $self->_result } };
204 }
205  
206 0 ▶sub winner { $_[0]->result->{winner} }
Subroutine winner: not called CC=1 SCAR=6.9
Pod documented
207  
208 70 ▶sub bot { require Game::Xiangqi::Bot; 'Game::Xiangqi::Bot' }
Subroutine bot: called CC=1 SCAR=0.0
Pod documented
209  
210 18 ▶sub replay {
Subroutine replay: called CC=5 SCAR=16.4
Pod documented
211 18 my ($proto, @rest) = @_;
212  
213 18 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 6 12 18
214 0 ▶ my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ];
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[0] eq 'ARRAY' ? : 6 0 6
215 6 return $proto->_replayed($moves);
216 }
217  
218 12 ▶ return undef if @rest % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 2 10 12
219 10 my %o = @rest;
220 10 my $self = $proto->new(%o);
221 10 ▶ return $self->_walk($o{moves} || []);
Condition50%1/2
$o{'moves'} || []
l!l
100
MC/DC0%0/1
$o{'moves'} || []
$o{'moves'}
Truth table50%1/2
A
A= $o{'moves'}
Aresult
11
00
222 }
223  
224 6 ▶sub _replayed {
Subroutine _replayed: called CC=1 SCAR=0.0
225 6 my ($self, $moves) = @_;
226 6 my $fresh = (ref $self)->new(
227 seed => $self->_held_seed,
228 red => $self->red,
229 position => $self->_start,
230 );
231 6 return $fresh->_walk($moves);
232 }
233  
234 16 ▶sub _walk {
Subroutine _walk: called CC=5 SCAR=16.1
235 16 my ($self, $moves) = @_;
236 16 for my $i (0 .. $#$moves) {
237 100 my $why = $self->play($moves->[$i]);
238 100 ▶ return wantarray ? ($self, $why, $i) : undef if $why;
Branch: 2 branches
BranchTrueFalseTotal
if $why 4 96 100
wantarray ? : 2 2 4
239 }
240 12 ▶ return wantarray ? ($self, 0, scalar @$moves) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 10 12
241 }
242  
243 119 ▶sub to_text {
Subroutine to_text: called CC=1 SCAR=0.0
Pod documented
244 119 my ($self) = @_;
245 return join "\n",
246 'xiangqi 1',
247 'seed ' . unpack('H*', $self->_held_seed),
248 'red ' . $self->red,
249 'start ' . $self->_start->to_fen,
250 119 join(' ', 'moves', @{ $self->_log }),
251 'status ' . $self->status,
252 '';
253 }
254  
255 40 ▶sub from_text {
Subroutine from_text: called CC=18 SCAR=28.9
Pod documented
256 40 my ($class, $text) = @_;
257 40 ▶ Carp::croak('Game::Xiangqi: from_text needs the text to_text wrote, and was given '
Branch: 2 branches
BranchTrueFalseTotal
if not defined $text or ref $text 2 38 40
defined $text ? : 1 1 2
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
1138
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
ABresult
1X1
011
000
258 . (defined $text ? 'a reference' : 'nothing'))
259 if !defined $text || ref $text;
260  
261 38 my @lines = split /\n/, $text;
262 38 ▶ Carp::croak('Game::Xiangqi: the text is not a saved game of xiangqi, version 1')
Branch: 1 branch
BranchTrueFalseTotal
unless @lines and $lines[0] eq "xiangqi 1" 2 36 38
Condition100%3/3
@lines and $lines[0] eq "xiangqi 1"
!ll&&!rl&&r
1136
MC/DC100%2/2
@lines and $lines[0] eq "xiangqi 1"
@lines $lines[0] eq "xiangqi 1"
Truth table100%3/3
A and B
A= @linesB= $lines[0] eq "xiangqi 1"
ABresult
0X0
100
111
263 unless @lines && $lines[0] eq 'xiangqi 1';
264  
265 36 my %field;
266 36 for my $line (@lines[1 .. $#lines]) {
267 181 ▶ my ($name, $value) = $line =~ /\A(seed|red|start|moves|status)(?: (.*))?\z/
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A(seed|red|start|moves|status)(?: (.*))?\z/ or Carp::croak("Game::Xiangqi: the saved game has a line it does not know: '${line}'") 180 1 181
268 or Carp::croak("Game::Xiangqi: the saved game has a line it does not know: '$line'");
269 Carp::croak("Game::Xiangqi: the saved game gives $name twice")
270 180 ▶ if exists $field{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $field{$name} 1 179 180
271 179 ▶ $field{$name} = defined $value ? $value : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 171 8 179
272 }
273 34 for my $name (qw/seed red start moves status/) {
274 Carp::croak("Game::Xiangqi: the saved game has no $name line")
275 167 ▶ unless exists $field{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $field{$name} 1 166 167
276 }
277  
278 Carp::croak('Game::Xiangqi: the saved seed is not sixty-four hex digits')
279 33 ▶ unless $field{seed} =~ /\A[0-9a-f]{64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field{"seed"} =~ /\A[0-9a-f]{64}\z/ 1 32 33
280 Carp::croak("Game::Xiangqi: the saved status must be 'active' or 'finished', not '$field{status}'")
281 32 ▶ unless $field{status} eq 'active' || $field{status} eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $field{"status"} eq "active" or $field{"status"} eq "finished" 1 31 32
Condition100%3/3
$field{"status"} eq "active" or $field{"status"} eq "finished"
l!l&&r!l&&!r
2561
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"
ABresult
1X1
011
000
282  
283 31 my ($start, $err) = $E->of_fen($field{start});
284 31 ▶ Carp::croak("Game::Xiangqi: the saved start position was refused, code $err")
Branch: 1 branch
BranchTrueFalseTotal
unless $start 1 30 31
285 unless $start;
286  
287 my $self = $class->new(
288 seed => pack('H*', $field{seed}),
289 red => $field{red},
290 30 position => $start,
291 );
292 29 my @moves = split ' ', $field{moves};
293 29 for my $at (0 .. $#moves) {
294 263 my $why = $self->_play($moves[$at], $at == $#moves);
295 263 ▶ Carp::croak('Game::Xiangqi: the saved move ' . ($at + 1) . ", $moves[$at], was refused: "
Branch: 1 branch
BranchTrueFalseTotal
if $why 2 261 263
296 . $why->code)
297 if $why;
298 }
299  
300 27 $self->status($field{status});
301 27 return $self;
302 }
303  
304 13 1;
305  
306 __END__
« blib/lib/Game/Xiangqi/Terminal.pm ↑ Summary blib/lib/Game/Xiangqi/Notation.pm »