| 1 | package Game::Shogi; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
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; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Game::Shogi::Engine (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Game::Shogi::Error; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Game::Shogi::Handicap; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 9 | ▶ | use Game::Shogi::Move; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 9 | my $START = 'lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1'; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | has handicap => (is => 'ro'); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 9 | has _want_sfen => (is => 'ro', init_arg => 'sfen', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 9 | has _want_cap => (is => 'ro', init_arg => 'cap', private => 1, default => 500); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 9 | has _board => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 9 | has _start => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 9 | has _moves => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 9 | has _result => (is => 'rw', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 123 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 123 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 123 | my ($handicap, $sfen) = ($self->handicap, $self->_want_sfen); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 123 | ▶ | Carp::croak("Game::Shogi: '$handicap' is not a handicap; the handicaps are " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $handicap and not "Game::Shogi::Handicap"->known($handicap)
MC/DC100%2/2
defined $handicap and not "Game::Shogi::Handicap"->known($handicap)
defined $handicap
not "Game::Shogi::Handicap"->known($handicap)
Truth table100%3/3
A and not(B)
A= defined $handicapB= not "Game::Shogi::Handicap"->known($handicap)
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | . join(', ', Game::Shogi::Handicap->names)) | ||||||||||||||||||||||||||||||||||||||||||
| 37 | if defined $handicap && !Game::Shogi::Handicap->known($handicap); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 122 | ▶ | Carp::croak("Game::Shogi: a game starts from a handicap or from a position, not both") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $handicap and defined $sfen
MC/DC100%2/2
defined $handicap and defined $sfen
defined $handicap
defined $sfen
Truth table100%3/3
A and B
A= defined $handicapB= defined $sfen
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | if defined $handicap && defined $sfen; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 121 | ▶ | my $start = defined $handicap ? Game::Shogi::Handicap->sfen($handicap) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | : defined $sfen ? $sfen | ||||||||||||||||||||||||||||||||||||||||||
| 42 | : $START; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 121 | my $board = Game::Shogi::Engine->new(sfen => $start, cap => $self->_want_cap); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 120 | $self->_board($board); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 120 | $self->_start($board->sfen); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 120 | $self->_moves([]); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 120 | $self->_result($board->outcome); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 120 | return; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1031 | ▶ | sub board { $_[0]->_board->clone } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
board: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 7 | ▶ | sub start { $_[0]->_start } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 30 | ▶ | sub sfen { $_[0]->_board->sfen } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sfen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | sub side { $_[0]->_board->side_to_move } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
side: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 207 | ▶ | sub turn { $_[0]->_result ? undef : $_[0]->_board->side_to_move } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
turn: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 98 | ▶ | sub status { $_[0]->_result ? 'finished' : 'active' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub move_number { $_[0]->_board->move_number } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
move_number: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub in_check { $_[0]->_board->in_check($_[0]->_board->side_to_move) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_check: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 12 | ▶ | sub can_declare { !$_[0]->_result && $_[0]->_board->can_declare ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
can_declare: called CC=3 SCAR=11.1
Pod
documented
Branch: 1 branch
Condition66%2/3
not $_[0]->_result and $_[0]->_board->can_declare
MC/DC50%1/2
not $_[0]->_result and $_[0]->_board->can_declare
not $_[0]->_result
$_[0]->_board->can_declare
Truth table66%2/3
not(A) and B
A= not $_[0]->_resultB= $_[0]->_board->can_declare
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 34 | ▶ | sub result { $_[0]->_result } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
result: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | sub winner { my $r = $_[0]->_result; $r ? $r->winner : undef } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
winner: called CC=2 SCAR=7.2
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 5 | ▶ | sub moves { @{ $_[0]->_moves } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
moves: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 54 | ▶ | sub history { $_[0]->_board->history } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
history: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 24 | ▶ | sub hand { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hand: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 24 | my ($self, $side) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 24 | ▶ | return undef unless defined $side && ($side eq 'b' || $side eq 'w'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $side and $side eq "b" || $side eq "w"
$side eq "b" or $side eq "w"
MC/DC0%0/3
defined $side and $side eq "b" || $side eq "w"
defined $side
$side eq "b"
$side eq "w"
Truth table50%2/4
A and B or C
A= defined $sideB= $side eq "b"C= $side eq "w"
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 24 | return { %{ $self->_board->hands->{$side} } }; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 14 | ▶ | sub legal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 14 | ▶ | return () if $self->_result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 13 | return map { Game::Shogi::Move->new(usi => $_) } $self->_board->legal; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 403 | ▶ | sub play { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
play: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 403 | my ($self, $move) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 403 | ▶ | my $usi = ref $move && eval { $move->isa('Game::Shogi::Move') } ? $move->usi : $move; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $move and eval {
do {
$move->isa("Game::Shogi::Move")
}
}
MC/DC50%1/2
ref $move and eval {
do {
$move->isa("Game::Shogi::Move")
}
}
ref $move
eval {
do {
$move->isa("Game::Shogi::Move")
}
}
Truth table66%2/3
A and B
A= ref $moveB= eval {
do {
$move->isa("Game::Shogi::Move")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 403 | ▶ | return Game::Shogi::Error->throw('game_over') if $self->_result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 402 | my $fault = $self->_board->move_fault($usi); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 402 | ▶ | return Game::Shogi::Error->of_fault($fault) if $fault; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 383 | ▶ | return Game::Shogi::Error->throw('bad_move') unless $self->_board->do_move($usi); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 383 | push @{ $self->_moves }, $usi; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 383 | $self->_result($self->_board->outcome); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 383 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 9 | ▶ | sub undo { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
undo: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 9 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 9 | my $r = $self->_result; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 9 | ▶ | return Game::Shogi::Error->throw('game_over') if $r && ($r->how eq 'resign' || $r->how eq 'impasse_declared'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$r and $r->how eq "resign" || $r->how eq "impasse_declared"
$r->how eq "resign" or $r->how eq "impasse_declared"
MC/DC100%3/3
$r and $r->how eq "resign" || $r->how eq "impasse_declared"
$r
$r->how eq "resign"
$r->how eq "impasse_declared"
Truth table100%4/4
A and B or C
A= $rB= $r->how eq "resign"C= $r->how eq "impasse_declared"
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | ▶ | return Game::Shogi::Error->throw('nothing_to_undo') unless @{ $self->_moves } && $self->_board->undo_move; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$self->_moves;} and $self->_board->undo_move
MC/DC50%1/2
@{$self->_moves;} and $self->_board->undo_move
@{$self->_moves;}
$self->_board->undo_move
Truth table66%2/3
A and B
A= @{$self->_moves;}B= $self->_board->undo_move
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 6 | pop @{ $self->_moves }; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | $self->_result($self->_board->outcome); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | sub resign { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resign: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | my ($self, $side) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | ▶ | return Game::Shogi::Error->throw('game_over') if $self->_result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 5 | ▶ | my $r = $self->_board->resign($side) or return Game::Shogi::Error->throw('not_your_turn'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | $self->_result($r); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 3 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 6 | ▶ | sub declare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
declare: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 6 | ▶ | return Game::Shogi::Error->throw('game_over') if $self->_result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | my $fault = $self->_board->declare_fault; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | ▶ | return Game::Shogi::Error->throw('cannot_declare', Game::Shogi::Engine->declare_reason($fault)) if $fault; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | my $r = $self->_board->declare | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | or return Game::Shogi::Error->throw('cannot_declare', 'the game has reached its cap'); | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | $self->_result($r); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | sub log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | my %log = (start => $self->_start, moves => [ @{ $self->_moves } ]); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | $log{handicap} = $self->handicap if defined $self->handicap; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | return \%log; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | ▶ | sub record { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 4 | require Game::Shogi::Notation; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 4 | return Game::Shogi::Notation->record($self->_start, @{ $self->_moves }); | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 13 | ▶ | sub from_log { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_log: called CC=15 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 13 | my ($class, $log, %opt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 133 | Carp::croak("Game::Shogi: a log is a hash reference with a start and moves") | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 13 | ▶ | unless ref $log eq 'HASH' && ref($log->{moves} // []) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
ref $log eq "HASH" and ref($log->{"moves"} // []) eq "ARRAY"
$log->{"moves"} // []
MC/DC50%1/2
ref $log eq "HASH" and ref($log->{"moves"} // []) eq "ARRAY"
ref $log eq "HASH"
$log->{"moves"}
Truth table66%2/3
A and B
A= ref $log eq "HASH"B= $log->{"moves"}
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | my $game = defined $log->{handicap} | ||||||||||||||||||||||||||||||||||||||||||
| 136 | ? $class->new(handicap => $log->{handicap}, (exists $opt{cap} ? (cap => $opt{cap}) : ())) | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 12 | ▶ | : $class->new((defined $log->{start} ? (sfen => $log->{start}) : ()), (exists $opt{cap} ? (cap => $opt{cap}) : ())); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | Carp::croak("Game::Shogi: the log's start is not its handicap's") | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 12 | ▶ | if defined $log->{handicap} && defined $log->{start} && $log->{start} ne $game->start; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $log->{"handicap"} and defined $log->{"start"} and $log->{"start"} ne $game->start
defined $log->{"handicap"} and defined $log->{"start"}
MC/DC0%0/3
defined $log->{"handicap"} and defined $log->{"start"} and $log->{"start"} ne $game->start
defined $log->{"handicap"}
defined $log->{"start"}
$log->{"start"} ne $game->start
Truth table50%2/4
A and B and C
A= defined $log->{"handicap"}B= defined $log->{"start"}C= $log->{"start"} ne $game->start
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 12 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 12 | ▶ | for my $m (@{ $log->{moves} || [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$log->{'moves'} || []
MC/DC0%0/1
$log->{'moves'} || []
$log->{'moves'}
Truth table50%1/2
A
A= $log->{'moves'}
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 17 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 17 | my $refusal = $game->play($m); | |||||||||||||||||||||||||||||||||||||||||
| 144 | 17 | ▶ | Carp::croak("Game::Shogi: move $i ('" . ($m // 'undef') . "') of the log was refused: " . $refusal->message) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$m // "undef"
MC/DC0%0/1
$m // "undef"
$m
Truth table50%1/2
A
A= $m
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | if $refusal; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 10 | return $game; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | 53 | ▶ | sub to_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_text: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 53 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 53 | my $r = $self->_result; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 53 | my @lines = ('shogi 1'); | |||||||||||||||||||||||||||||||||||||||||
| 154 | 53 | ▶ | push @lines, 'handicap ' . $self->handicap if defined $self->handicap; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | push @lines, 'start ' . $self->_start, 'cap ' . $self->_board->cap, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 53 | join(' ', 'moves', @{ $self->_moves }); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 53 | ▶ | push @lines, 'ending resign ' . $r->loser if $r && $r->how eq 'resign'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r and $r->how eq "resign"
MC/DC100%2/2
$r and $r->how eq "resign"
$r
$r->how eq "resign"
Truth table100%3/3
A and B
A= $rB= $r->how eq "resign"
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 53 | ▶ | push @lines, 'ending declare' if $r && $r->how eq 'impasse_declared'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r and $r->how eq "impasse_declared"
MC/DC100%2/2
$r and $r->how eq "impasse_declared"
$r
$r->how eq "impasse_declared"
Truth table100%3/3
A and B
A= $rB= $r->how eq "impasse_declared"
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 53 | return join "\n", @lines, ''; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 16 | ▶ | sub from_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_text: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 16 | my ($class, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 16 | ▶ | Carp::croak('Game::Shogi: 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
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | . (defined $text ? 'a reference' : 'nothing')) | ||||||||||||||||||||||||||||||||||||||||||
| 166 | if !defined $text || ref $text; | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 14 | my @lines = split /\n/, $text; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 14 | ▶ | Carp::croak('Game::Shogi: the text is not a saved game of shogi, version 1') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@lines and $lines[0] eq "shogi 1"
MC/DC50%1/2
@lines and $lines[0] eq "shogi 1"
@lines
$lines[0] eq "shogi 1"
Truth table66%2/3
A and B
A= @linesB= $lines[0] eq "shogi 1"
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | unless @lines && $lines[0] eq 'shogi 1'; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 13 | my %field; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 13 | for my $line (@lines[1 .. $#lines]) { | |||||||||||||||||||||||||||||||||||||||||
| 172 | 47 | ▶ | my ($name, $value) = $line =~ /\A(handicap|start|cap|moves|ending)(?: (.*))?\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | or Carp::croak("Game::Shogi: the saved game has a line it does not know: '$line'"); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 46 | ▶ | Carp::croak("Game::Shogi: the saved game gives $name twice") if exists $field{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 45 | ▶ | $field{$name} = $value // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$value // ""
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 11 | for my $name (qw/start cap moves/) { | |||||||||||||||||||||||||||||||||||||||||
| 178 | 32 | ▶ | Carp::croak("Game::Shogi: the saved game has no $name line") unless exists $field{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | Carp::croak("Game::Shogi: the saved cap is not a whole number: '$field{cap}'") | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 10 | ▶ | unless $field{cap} =~ /\A[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 9 | my %log = (start => $field{start}, moves => [ split ' ', $field{moves} ]); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 9 | ▶ | $log{handicap} = $field{handicap} if exists $field{handicap}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 9 | my $game = $class->from_log(\%log, cap => $field{cap}); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 8 | ▶ | if (exists $field{ending}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | my $refusal = $field{ending} =~ /\Aresign ([bw])\z/ ? $game->resign($1) | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 5 | ▶ | : $field{ending} eq 'declare' ? $game->declare | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | : Carp::croak("Game::Shogi: the saved ending is not one it knows: '$field{ending}'"); | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 4 | ▶ | Carp::croak("Game::Shogi: the saved ending '$field{ending}' was refused: " . $refusal->message) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | if $refusal; | ||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 5 | return $game; | |||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||||||||||||||||||||||||||