blib/lib/Game/Shogi.pm

filter: Statementstmt 98.6% 148 / 150 Branchbran 92.0% 81 / 88 Conditioncond 76.7% 43 / 56 MC/DCmcdc 59.3% 19 / 32 Subroutinesub 94.1% 32 / 34 Podpod 96.0% 24 / 25 totaltotal 90.1% 347 / 385 CC 68 SCAR 20.9
CC
68
Cov
93%
from_text
18.0
from_log
16.2
BUILD
7.0
CRAP 8.1
« blib/lib/Game/Shogi/USI.pm ↑ Summary blib/lib/Game/Shogi/Notation.pm »
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
BranchTrueFalseTotal
if defined $handicap and not "Game::Shogi::Handicap"->known($handicap) 1 122 123
Condition100%3/3
defined $handicap and not "Game::Shogi::Handicap"->known($handicap)
!ll&&!rl&&r
105171
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)
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $handicap and defined $sfen 1 121 122
Condition100%3/3
defined $handicap and defined $sfen
!ll&&!rl&&r
105161
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
ABresult
0X0
100
111
39 if defined $handicap && defined $sfen;
40 121 ▶ my $start = defined $handicap ? Game::Shogi::Handicap->sfen($handicap)
Branch: 2 branches
BranchTrueFalseTotal
defined $handicap ? : 16 105 121
defined $sfen ? : 77 28 105
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
BranchTrueFalseTotal
$_[0]->_result ? : 1 206 207
56 98 ▶sub status { $_[0]->_result ? 'finished' : 'active' }
Subroutine status: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_result ? : 4 94 98
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
BranchTrueFalseTotal
!$_[0]->_result && $_[0]->_board->can_declare ? : 2 10 12
Condition66%2/3
not $_[0]->_result and $_[0]->_board->can_declare
!ll&&!rl&&r
0102
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$r ? : 4 0 4
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
BranchTrueFalseTotal
unless defined $side and $side eq "b" || $side eq "w" 0 24 24
Condition50%3/6
defined $side and $side eq "b" || $side eq "w"
!ll&&!rl&&r
0024
$side eq "b" or $side eq "w"
l!l&&r!l&&!r
11130
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"
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
if $self->_result 1 13 14
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
BranchTrueFalseTotal
ref $move && eval { do { $move->isa('Game::Shogi::Move') } } ? : 1 402 403
Condition66%2/3
ref $move and eval { do { $move->isa("Game::Shogi::Move") } }
!ll&&!rl&&r
40201
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") } }
ABresult
0X0
100
111
80 403 ▶ return Game::Shogi::Error->throw('game_over') if $self->_result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_result 1 402 403
81 402 my $fault = $self->_board->move_fault($usi);
82 402 ▶ return Game::Shogi::Error->of_fault($fault) if $fault;
Branch: 1 branch
BranchTrueFalseTotal
if $fault 19 383 402
83 383 ▶ return Game::Shogi::Error->throw('bad_move') unless $self->_board->do_move($usi);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_board->do_move($usi) 0 383 383
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
BranchTrueFalseTotal
if $r and $r->how eq "resign" || $r->how eq "impasse_declared" 2 7 9
Condition100%6/6
$r and $r->how eq "resign" || $r->how eq "impasse_declared"
!ll&&!rl&&r
612
$r->how eq "resign" or $r->how eq "impasse_declared"
l!l&&r!l&&!r
111
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"
ABCresult
0XX0
1000
11X1
1011
93 7 ▶ return Game::Shogi::Error->throw('nothing_to_undo') unless @{ $self->_moves } && $self->_board->undo_move;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_moves;} and $self->_board->undo_move 1 6 7
Condition66%2/3
@{$self->_moves;} and $self->_board->undo_move
!ll&&!rl&&r
106
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $self->_result 1 5 6
102 5 ▶ my $r = $self->_board->resign($side) or return Game::Shogi::Error->throw('not_your_turn');
Branch: 1 branch
BranchTrueFalseTotal
my $r = $self->_board->resign($side) or return "Game::Shogi::Error"->throw("not_your_turn") 3 2 5
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
BranchTrueFalseTotal
if $self->_result 0 6 6
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
BranchTrueFalseTotal
if $fault 3 3 6
112 3 ▶ my $r = $self->_board->declare
Branch: 1 branch
BranchTrueFalseTotal
my $r = $self->_board->declare or return "Game::Shogi::Error"->throw("cannot_declare", "the game has reached its cap") 3 0 3
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
BranchTrueFalseTotal
if defined $self->handicap 1 0 1
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
BranchTrueFalseTotal
unless ref $log eq "HASH" and ref($log->{"moves"} // []) eq "ARRAY" 1 12 13
Condition60%3/5
ref $log eq "HASH" and ref($log->{"moves"} // []) eq "ARRAY"
!ll&&!rl&&r
1012
$log->{"moves"} // []
l!l
120
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
defined $log->{'handicap'} ? : 3 9 12
exists $opt{'cap'} ? : 2 1 3
defined $log->{'start'} ? : 7 2 9
exists $opt{'cap'} ? : 7 2 9
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
BranchTrueFalseTotal
if defined $log->{"handicap"} and defined $log->{"start"} and $log->{"start"} ne $game->start 0 12 12
Condition66%4/6
defined $log->{"handicap"} and defined $log->{"start"} and $log->{"start"} ne $game->start
!ll&&!rl&&r
930
defined $log->{"handicap"} and defined $log->{"start"}
!ll&&!rl&&r
903
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
ABCresult
0XX0
10X0
1100
1111
140 12 my $i = 0;
141 12 ▶ for my $m (@{ $log->{moves} || [] }) {
Condition50%1/2
$log->{'moves'} || []
l!l
120
MC/DC0%0/1
$log->{'moves'} || []
$log->{'moves'}
Truth table50%1/2
A
A= $log->{'moves'}
Aresult
11
00
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
BranchTrueFalseTotal
if $refusal 2 15 17
Condition50%1/2
$m // "undef"
l!l
20
MC/DC0%0/1
$m // "undef"
$m
Truth table50%1/2
A
A= $m
Aresult
11
00
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
BranchTrueFalseTotal
if defined $self->handicap 6 47 53
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
BranchTrueFalseTotal
if $r and $r->how eq "resign" 2 51 53
Condition100%3/3
$r and $r->how eq "resign"
!ll&&!rl&&r
5012
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"
ABresult
0X0
100
111
158 53 ▶ push @lines, 'ending declare' if $r && $r->how eq 'impasse_declared';
Branch: 1 branch
BranchTrueFalseTotal
if $r and $r->how eq "impasse_declared" 1 52 53
Condition100%3/3
$r and $r->how eq "impasse_declared"
!ll&&!rl&&r
5021
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if not defined $text or ref $text 2 14 16
defined $text ? : 1 1 2
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
1114
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
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
BranchTrueFalseTotal
unless @lines and $lines[0] eq "shogi 1" 1 13 14
Condition66%2/3
@lines and $lines[0] eq "shogi 1"
!ll&&!rl&&r
0113
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
() = $line =~ /\A(handicap|start|cap|moves|ending)(?: (.*))?\z/u or Carp::croak("Game::Shogi: the saved game has a line it does not know: '${line}'") 46 1 47
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
BranchTrueFalseTotal
if exists $field{$name} 1 45 46
175 45 ▶ $field{$name} = $value // '';
Condition100%2/2
$value // ""
l!l
369
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
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
BranchTrueFalseTotal
unless exists $field{$name} 1 31 32
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
BranchTrueFalseTotal
unless $field{"cap"} =~ /\A[0-9]+\z/u 1 9 10
182 9 my %log = (start => $field{start}, moves => [ split ' ', $field{moves} ]);
183 9 ▶ $log{handicap} = $field{handicap} if exists $field{handicap};
Branch: 1 branch
BranchTrueFalseTotal
if exists $field{"handicap"} 2 7 9
184 9 my $game = $class->from_log(\%log, cap => $field{cap});
185 8 ▶ if (exists $field{ending}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field{"ending"}) 5 3 8
186 my $refusal = $field{ending} =~ /\Aresign ([bw])\z/ ? $game->resign($1)
187 5 ▶ : $field{ending} eq 'declare' ? $game->declare
Branch: 2 branches
BranchTrueFalseTotal
$field{'ending'} =~ /\Aresign ([bw])\z/u ? : 2 3 5
$field{'ending'} eq 'declare' ? : 2 1 3
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
BranchTrueFalseTotal
if $refusal 2 2 4
190 if $refusal;
191 }
192 5 return $game;
193 }
194  
195 9 1;
196  
197 __END__
« blib/lib/Game/Shogi/USI.pm ↑ Summary blib/lib/Game/Shogi/Notation.pm »