| 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
Condition100%3/3
defined $seed and length $seed == 32
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$red eq "p1" or $red eq "p2"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | unless $red eq 'p1' || $red eq 'p2'; | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 132 | ▶ | my $pos = $self->_pos ? $self->_pos->clone : $E->new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub _colour_of { $_[1] eq $_[0]->red ? RED : BLACK } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_colour_of: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 676 | my $mv = $N->move_of_iccs($iccs); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 676 | ▶ | return $R->throw('bad_move') unless defined $mv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 666 | my $why = $self->_refuse($mv); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 666 | ▶ | return $why if $why; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 765 | my ($winner, $reason) = $self->_pos->outcome; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 765 | ▶ | if ($winner) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | $self->status('finished'); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 9 | ▶ | $self->_result({ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 514 | my $v = $self->_start->judge($self->_played); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 514 | ▶ | return unless $v->{reason} != J_ONGOING; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$piece and $piece != 32 and Game::Xiangqi::Engine::kind_of($piece)
$piece and $piece != 32
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | unless $piece && $piece != BORDER && Game::Xiangqi::Engine::kind_of($piece); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 662 | ▶ | return $R->throw('not_your_turn') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | unless Game::Xiangqi::Engine::colour_of($piece) == $p->side; | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 658 | ▶ | return 0 if grep { $_ == $mv } $p->legal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 25 | ▶ | if (grep { $_ == $mv } $p->moves) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | if Game::Xiangqi::Engine::colour_of($p->at($to)) == $colour; | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 17 | ▶ | if ($kind == GENERAL) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 1 | ▶ | return $R->throw('general_leaves_palace') unless $E->in_palace($to, $colour); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | elsif ($kind == ADVISOR) { | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | return $R->throw('advisor_leaves_palace') unless $E->in_palace($to, $colour); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | elsif ($kind == ELEPHANT) { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 2 | ▶ | return $R->throw('elephant_crosses_river') if $E->crossed_river($to, $colour); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$eye >= 0 and $p->at($eye) != 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$leg >= 0 and $p->at($leg) != 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 4 | ▶ | return $R->throw('soldier_no_retreat') if $back; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | return $R->throw('soldier_no_sideways') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fr == $tr and not $E->crossed_river($from, $colour)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | return $proto->_replayed($moves); | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 12 | ▶ | return undef if @rest % 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 10 | my %o = @rest; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 10 | my $self = $proto->new(%o); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 10 | ▶ | 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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 12 | ▶ | return wantarray ? ($self, 0, scalar @$moves) : $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@lines and $lines[0] eq "xiangqi 1"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 179 | ▶ | $field{$name} = defined $value ? $value : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||