blib/lib/Game/Xiangqi.pm

filter: Statementstmt 97.9% 142 / 145 Branchbran 86.6% 78 / 90 Conditioncond 60.8% 14 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 92.3% 24 / 26 Podpod 91.6% 11 / 12 totaltotal 88.3% 274 / 310 CC 55 SCAR 25.4
CC
55
Cov
91%
_refuse
30.5
_settle
6.0
BUILD
6.0
CRAP 12.7
« bin/xiangqi ↑ Summary blib/lib/Game/Xiangqi/Notation.pm »
1 package Game::Xiangqi;
2  
3 9 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Game::Xiangqi::Engine ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Game::Xiangqi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Game::Xiangqi::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 our $VERSION = '0.03';
15  
16 9 my $E = 'Game::Xiangqi::Engine';
17 9 my $N = 'Game::Xiangqi::Notation';
18 9 my $R = 'Game::Xiangqi::Error';
19  
20 9 has red => (is => 'ro', isa => Str, default => 'p1');
21  
22 9 has status => (is => 'rw', isa => Str, default => 'active');
23  
24 9 has _held_seed => (is => 'ro', init_arg => 'seed');
25  
26 9 has _pos => (is => 'rw', init_arg => 'position', private => 1);
27  
28 9 has _start => (is => 'rw', private => 1);
29  
30 9 has _log => (is => 'ro', isa => ArrayRef, default => [], private => 1);
31  
32 9 has _played => (is => 'ro', isa => ArrayRef, default => [], private => 1);
33  
34 9 has _result => (is => 'rw', private => 1);
35  
36 84 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
37 84 my ($self) = @_;
38  
39 84 my $seed = $self->_held_seed;
40 84 ▶ Carp::croak('Game::Xiangqi: seed must be exactly thirty-two bytes')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and length $seed == 32 2 82 84
Condition100%3/3
defined $seed and length $seed == 32
!ll&&!rl&&r
1182
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 82 my $red = $self->red;
44 82 ▶ Carp::croak("Game::Xiangqi: red must be 'p1' or 'p2', not '$red'")
Branch: 1 branch
BranchTrueFalseTotal
unless $red eq "p1" or $red eq "p2" 1 81 82
Condition100%3/3
$red eq "p1" or $red eq "p2"
l!l&&r!l&&!r
7471
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 81 ▶ my $pos = $self->_pos ? $self->_pos->clone : $E->new;
Branch: 1 branch
BranchTrueFalseTotal
$self->_pos ? : 22 59 81
48 81 $self->_pos($pos);
49 81 $self->_start($pos->clone);
50 81 $self->_result({ winner => undef, reason => undef, rule => 0, loop => undef });
51  
52 81 $self->_settle;
53 81 return;
54 }
55  
56 188 ▶sub position { $_[0]->_pos }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
57 16 ▶sub signature { $_[0]->_pos->key_hex }
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
58 24 ▶sub log { [ @{ $_[0]->_log } ] }
Subroutine log: called CC=1 SCAR=0.0
Pod documented
59  
60 242 ▶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 ? : 132 110 242
$_[0]->red eq 'p1' ? : 108 2 110
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 234 ▶sub turn {
Subroutine turn: called CC=2 SCAR=7.1
Pod documented
64 234 my ($self) = @_;
65 234 ▶ return undef unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 234 234
66 234 return $self->_seat_of($self->_pos->side);
67 }
68  
69 15 ▶sub legal {
Subroutine legal: called CC=2 SCAR=6.9
Pod documented
70 15 my ($self) = @_;
71 15 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 14 15
72 14 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 237 ▶sub play {
Subroutine play: called CC=4 SCAR=13.9
Pod documented
82 237 my ($self, $iccs) = @_;
83 237 ▶ return $R->throw('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 236 237
84  
85 236 my $mv = $N->move_of_iccs($iccs);
86 236 ▶ return $R->throw('bad_move') unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 9 227 236
87  
88 227 my $why = $self->_refuse($mv);
89 227 ▶ return $why if $why;
Branch: 1 branch
BranchTrueFalseTotal
if $why 32 195 227
90  
91 195 push @{ $self->_log }, $N->iccs_of($mv);
92 195 push @{ $self->_played }, $mv;
93 195 $self->_pos->do_move($mv);
94  
95 195 $self->_settle;
96 195 return 0;
97 }
98  
99 276 ▶sub _settle {
Subroutine _settle: called CC=6 SCAR=17.9
100 276 my ($self) = @_;
101  
102 276 my ($winner, $reason) = $self->_pos->outcome;
103 276 ▶ if ($winner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($winner) 6 270 276
104 6 $self->status('finished');
105 6 ▶ $self->_result({
Branch: 1 branch
BranchTrueFalseTotal
$reason == 1 ? : 4 2 6
106 winner => $self->_seat_of($winner),
107 reason => $reason == BY_CHECKMATE ? 'checkmate' : 'stalemate',
108 rule => 0,
109 loop => undef,
110 });
111 6 return;
112 }
113  
114 270 my $v = $self->_start->judge($self->_played);
115 270 ▶ return unless $v->{reason} != J_ONGOING;
Branch: 1 branch
BranchTrueFalseTotal
unless $v->{"reason"} != 0 267 3 270
116  
117 3 my %name = (
118 J_PERPETUAL_CHECK() => 'perpetual_check',
119 J_PERPETUAL_CHASE() => 'perpetual_chase',
120 J_MUTUAL() => 'mutual',
121 J_NO_VIOLATION() => 'no_violation',
122 J_EFFECTIVE() => 'effective',
123 J_PROGRESS() => 'progress',
124 J_MOVES() => 'moves',
125 );
126 3 $self->status('finished');
127 $self->_result({
128 winner => $v->{winner} ? $self->_seat_of($v->{winner}) : undef,
129 reason => $name{ $v->{reason} },
130 rule => $v->{rule},
131 loop => $v->{loop_from} >= 0
132 3 ▶ ? [ @{ $self->_log }[ $v->{loop_from} .. $#{ $self->_log } ] ]
Branch: 2 branches
BranchTrueFalseTotal
$v->{'winner'} ? : 2 1 3
$v->{'loop_from'} >= 0 ? : 3 0 3
133 : undef,
134 });
135 }
136  
137 227 ▶sub _refuse {
Subroutine _refuse: called CC=27 SCAR=34.2
138 227 my ($self, $mv) = @_;
139 227 my $p = $self->_pos;
140 227 my ($from, $to) = ($E->move_from($mv), $E->move_to($mv));
141  
142 227 my $piece = $p->at($from);
143 227 ▶ return $R->throw('no_piece')
Branch: 1 branch
BranchTrueFalseTotal
unless $piece and $piece != 32 and Game::Xiangqi::Engine::kind_of($piece) 3 224 227
Condition66%4/6
$piece and $piece != 32 and Game::Xiangqi::Engine::kind_of($piece)
!ll&&!rl&&r
30224
$piece and $piece != 32
!ll&&!rl&&r
30224
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
144 unless $piece && $piece != BORDER && Game::Xiangqi::Engine::kind_of($piece);
145 224 ▶ return $R->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Xiangqi::Engine::colour_of($piece) == $p->side 4 220 224
146 unless Game::Xiangqi::Engine::colour_of($piece) == $p->side;
147  
148 220 ▶ return 0 if grep { $_ == $mv } $p->legal;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $mv;} $p->legal 195 25 220
149  
150 25 ▶ if (grep { $_ == $mv } $p->moves) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ == $mv;} $p->moves) 2 23 25
151 2 my (undef, $u) = $p->do_move($mv);
152 2 my $face = $p->generals_face;
153 2 $p->undo_move($u);
154 2 ▶ return $R->throw($face ? 'generals_face' : 'in_check', legal => $self->legal);
Branch: 1 branch
BranchTrueFalseTotal
$face ? : 1 1 2
155 }
156  
157 23 my $kind = Game::Xiangqi::Engine::kind_of($piece);
158 23 my $colour = Game::Xiangqi::Engine::colour_of($piece);
159  
160 23 ▶ return $R->throw('own_piece')
Branch: 1 branch
BranchTrueFalseTotal
if Game::Xiangqi::Engine::colour_of($p->at($to)) == $colour 6 17 23
161 if Game::Xiangqi::Engine::colour_of($p->at($to)) == $colour;
162  
163 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
164 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
165 }
166 elsif ($kind == ADVISOR) {
167 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
168 }
169 elsif ($kind == ELEPHANT) {
170 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
171 1 my $eye = $p->elephant_eye($from, $to);
172 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
173 }
174 elsif ($kind == HORSE) {
175 2 my $leg = $p->horse_leg($from, $to);
176 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
177 }
178 elsif ($kind == CANNON) {
179 1 my $n = $p->cannon_screens($from, $to);
180 1 ▶ return $R->throw('cannon_needs_one_screen') if $n >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n >= 0 1 0 1
181 }
182 elsif ($kind == SOLDIER) {
183 4 my ($fr, $tr) = ($E->rank_of($from), $E->rank_of($to));
184 4 ▶ my $back = $colour == RED ? $tr < $fr : $tr > $fr;
Branch: 1 branch
BranchTrueFalseTotal
$colour == 8 ? : 4 0 4
185 4 ▶ return $R->throw('soldier_no_retreat') if $back;
Branch: 1 branch
BranchTrueFalseTotal
if $back 2 2 4
186 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
187 if $fr == $tr && !$E->crossed_river($from, $colour);
188 }
189 6 return $R->throw('not_legal', legal => $self->legal);
190 }
191  
192 14 ▶sub result {
Subroutine result: called CC=2 SCAR=6.9
Pod documented
193 14 my ($self) = @_;
194 14 ▶ return { winner => undef, reason => undef, rule => 0, loop => undef }
Branch: 1 branch
BranchTrueFalseTotal
if $self->status eq "active" 3 11 14
195 if $self->status eq 'active';
196 11 return { %{ $self->_result } };
197 }
198  
199 0 ▶sub winner { $_[0]->result->{winner} }
Subroutine winner: not called CC=1 SCAR=6.9
Pod documented
200  
201 21 ▶sub bot { require Game::Xiangqi::Bot; 'Game::Xiangqi::Bot' }
Subroutine bot: called CC=1 SCAR=0.0
Pod documented
202  
203 16 ▶sub replay {
Subroutine replay: called CC=5 SCAR=16.4
Pod documented
204 16 my ($proto, @rest) = @_;
205  
206 16 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 4 12 16
207 4 ▶ my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ];
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[0] eq 'ARRAY' ? : 4 0 4
208 4 return $proto->_replayed($moves);
209 }
210  
211 12 ▶ return undef if @rest % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 2 10 12
212 10 my %o = @rest;
213 10 my $self = $proto->new(%o);
214 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
215 }
216  
217 4 ▶sub _replayed {
Subroutine _replayed: called CC=1 SCAR=0.0
218 4 my ($self, $moves) = @_;
219 4 my $fresh = (ref $self)->new(
220 seed => $self->_held_seed,
221 red => $self->red,
222 position => $self->_start,
223 );
224 4 return $fresh->_walk($moves);
225 }
226  
227 14 ▶sub _walk {
Subroutine _walk: called CC=5 SCAR=16.1
228 14 my ($self, $moves) = @_;
229 14 for my $i (0 .. $#$moves) {
230 43 my $why = $self->play($moves->[$i]);
231 43 ▶ return wantarray ? ($self, $why, $i) : undef if $why;
Branch: 2 branches
BranchTrueFalseTotal
if $why 4 39 43
wantarray ? : 2 2 4
232 }
233 10 ▶ return wantarray ? ($self, 0, scalar @$moves) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 9 10
234 }
235  
236 9 1;
237  
238 __END__
« bin/xiangqi ↑ Summary blib/lib/Game/Xiangqi/Notation.pm »