blib/lib/Game/Shogi/Notation.pm

filter: Statementstmt 99.1% 117 / 118 Branchbran 91.4% 75 / 82 Conditioncond 86.3% 19 / 22 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 94.1% 16 / 17 Podpod 100% 4 / 4 totaltotal 94.1% 241 / 256 CC 53 SCAR 20.5
CC
53
Cov
95%
japanese
13.0
_resolve
10.0
_rivals
8.0
CRAP 7.8
« blib/lib/Game/Shogi.pm ↑ Summary blib/lib/Game/Shogi/Result.pm »
1 package Game::Shogi::Notation;
2  
3 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Game::Shogi::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Game::Shogi::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 our $VERSION = '0.01';
13  
14 7 my $E = 'Game::Shogi::Engine';
15  
16 7 my @FILE = (undef, qw(1 2 3 4 5 6 7 8 9));
17 7 my @RANK = (undef, qw(一 二 三 四 五 六 七 八 九));
18 7 my %NAME = (
19 P => '歩', L => '香', N => '桂', S => '銀', G => '金', B => '角', R => '飛', K => '玉',
20 '+P' => 'と', '+L' => '成香', '+N' => '成桂', '+S' => '成銀', '+B' => '馬', '+R' => '龍',
21 );
22  
23 7 my %STRAIGHT = map { $_ => 1 } qw(S G +P +L +N +S);
24  
25 183 ▶sub _legal_set { my ($b) = @_; return { map { $_ => 1 } $b->legal } }
Subroutine _legal_set: called CC=1 SCAR=0.0
26  
27 146 ▶sub _rivals {
Subroutine _rivals: called CC=8 SCAR=20.8
28 146 my ($b, $legal, $token, $to, $skip) = @_;
29 146 my @from;
30 146 for my $sq (0 .. 80) {
31 11826 my $t = $b->at($sq);
32 11826 ▶ next unless defined $t && $t eq $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and $t eq $token 11325 501 11826
Condition100%3/3
defined $t and $t eq $token
!ll&&!rl&&r
89312394501
MC/DC100%2/2
defined $t and $t eq $token
defined $t $t eq $token
Truth table100%3/3
A and B
A= defined $tB= $t eq $token
ABresult
0X0
100
111
33 501 my $name = $E->square_name($sq);
34 501 ▶ next if defined $skip && $name eq $skip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $skip and $name eq $skip 141 360 501
Condition100%3/3
defined $skip and $name eq $skip
!ll&&!rl&&r
3357141
MC/DC100%2/2
defined $skip and $name eq $skip
defined $skip $name eq $skip
Truth table100%3/3
A and B
A= defined $skipB= $name eq $skip
ABresult
0X0
100
111
35 360 ▶ push @from, $name if $legal->{"$name$to"} || $legal->{"$name$to+"};
Branch: 1 branch
BranchTrueFalseTotal
if $$legal{"$name$to"} or $$legal{"$name$to+"} 75 285 360
Condition66%2/3
$$legal{"$name$to"} or $$legal{"$name$to+"}
l!l&&r!l&&!r
750285
MC/DC50%1/2
$$legal{"$name$to"} or $$legal{"$name$to+"}
$$legal{"$name$to"} $$legal{"$name$to+"}
Truth table66%2/3
A or B
A= $$legal{"$name$to"}B= $$legal{"$name$to+"}
ABresult
1X1
011
000
36 }
37 146 return @from;
38 }
39  
40 0 ▶sub _token_side { $_[0] =~ /[a-z]/ ? 'w' : 'b' }
Subroutine _token_side: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /[a-z]/u ? : 0 0 0
41  
42 118 ▶sub hodges {
Subroutine hodges: called CC=8 SCAR=20.8
Pod documented
43 118 my ($class, $b, $usi) = @_;
44 118 ▶ my $m = Game::Shogi::Move->of($usi) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $m = "Game::Shogi::Move"->of($usi) or return undef 117 1 118
45 117 my $legal = _legal_set($b);
46 117 ▶ return undef unless $legal->{$usi};
Branch: 1 branch
BranchTrueFalseTotal
unless $legal->{$usi} 1 116 117
47 116 ▶ return $m->drop . '*' . $m->to if $m->is_drop;
Branch: 1 branch
BranchTrueFalseTotal
if $m->is_drop 35 81 116
48  
49 81 my $token = $b->at($E->square($m->from));
50 81 my $letter = uc $token;
51 81 ▶ my $origin = _rivals($b, $legal, $token, $m->to, $m->from) ? $m->from : '';
Branch: 1 branch
BranchTrueFalseTotal
_rivals($b, $legal, $token, $m->to, $m->from) ? : 24 57 81
52 81 ▶ my $kind = defined $b->at($E->square($m->to)) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
defined $b->at($E->square($m->to)) ? : 13 68 81
53 81 my $plain = $m->from . $m->to;
54 81 ▶ my $status = $m->promote ? '+' : $legal->{"$plain+"} ? '=' : '';
Branch: 2 branches
BranchTrueFalseTotal
$m->promote ? : 11 70 81
$$legal{"$plain+"} ? : 9 61 70
55 81 return $letter . $origin . $kind . $m->to . $status;
56 }
57  
58 91 ▶sub _motion {
Subroutine _motion: called CC=4 SCAR=13.9
59 91 my ($side, $from, $to) = @_;
60 91 my ($rf, $rt) = (ord(substr $from, 1) - 96, ord(substr $to, 1) - 96);
61 91 ▶ ($rf, $rt) = (-$rf, -$rt) if $side eq 'w';
Branch: 1 branch
BranchTrueFalseTotal
if $side eq "w" 15 76 91
62 91 ▶ return $rt < $rf ? '上' : $rt > $rf ? '引' : '寄';
Branch: 2 branches
BranchTrueFalseTotal
$rt < $rf ? : 52 39 91
$rt > $rf ? : 31 8 39
63 }
64  
65 71 ▶sub _rightness {
Subroutine _rightness: called CC=2 SCAR=6.9
66 71 my ($side, $sq) = @_;
67 71 my $file = substr $sq, 0, 1;
68 71 ▶ return $side eq 'b' ? -$file : $file;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'b' ? : 66 5 71
69 }
70  
71 24 ▶sub _resolve {
Subroutine _resolve: called CC=10 SCAR=23.0
72 24 my ($side, $token, $mover, $to, @rivals) = @_;
73 24 my @all = ($mover, @rivals);
74 24 my $move = _motion($side, $mover, $to);
75 24 ▶ return $move if 1 == grep { _motion($side, $_, $to) eq $move } @all;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == grep({_motion($side, $_, $to) eq $move;} @all) 10 14 24
76  
77 14 ▶ return '直' if $STRAIGHT{ uc $token } && $move eq '上'
Branch: 1 branch
BranchTrueFalseTotal
if $STRAIGHT{uc $token} and $move eq "\x{4e0a}" and substr($mover, 0, 1) eq substr($to, 0, 1) 2 12 14
Condition100%6/6
$STRAIGHT{uc $token} and $move eq "\x{4e0a}" and substr($mover, 0, 1) eq substr($to, 0, 1)
!ll&&!rl&&r
662
$STRAIGHT{uc $token} and $move eq "\x{4e0a}"
!ll&&!rl&&r
338
MC/DC100%3/3
$STRAIGHT{uc $token} and $move eq "\x{4e0a}" and substr($mover, 0, 1) eq substr($to, 0, 1)
$STRAIGHT{uc $token} $move eq "\x{4e0a}" substr($mover, 0, 1) eq substr($to, 0, 1)
Truth table100%4/4
A and B and C
A= $STRAIGHT{uc $token}B= $move eq "\x{4e0a}"C= substr($mover, 0, 1) eq substr($to, 0, 1)
ABCresult
0XX0
10X0
1100
1111
78 && substr($mover, 0, 1) eq substr($to, 0, 1);
79  
80 12 my $x = _rightness($side, $mover);
81 12 my @x = map { _rightness($side, $_) } @all;
82 12 my ($max, $min) = ((sort { $b <=> $a } @x)[0], (sort { $a <=> $b } @x)[0]);
83 12 ▶ my $tag = $x == $max ? '右' : $x == $min ? '左' : undef;
Branch: 2 branches
BranchTrueFalseTotal
$x == $max ? : 5 7 12
$x == $min ? : 6 1 7
84 12 ▶ if (defined $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 11 1 12
85 11 my @group = grep { _rightness($side, $_) == $x } @all;
86 11 ▶ return $tag if @group == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @group == 1 8 3 11
87 3 ▶ return $tag . $move if 1 == grep { _motion($side, $_, $to) eq $move } @group;
Branch: 1 branch
BranchTrueFalseTotal
if 1 == grep({_motion($side, $_, $to) eq $move;} @group) 3 0 3
88 }
89 1 return '(' . substr($mover, 0, 1) . (ord(substr $mover, 1) - 96) . ')';
90 }
91  
92 66 ▶sub japanese {
Subroutine japanese: called CC=13 SCAR=25.7
Pod documented
93 66 my ($class, $b, $usi, %opt) = @_;
94 66 ▶ my $m = Game::Shogi::Move->of($usi) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $m = "Game::Shogi::Move"->of($usi) or return undef 66 0 66
95 66 my $legal = _legal_set($b);
96 66 ▶ return undef unless $legal->{$usi};
Branch: 1 branch
BranchTrueFalseTotal
unless $legal->{$usi} 1 65 66
97  
98 65 my $side = $b->side_to_move;
99 65 ▶ my $mark = $opt{marks} ? ($side eq 'b' ? '▲' : '△') : '';
Branch: 2 branches
BranchTrueFalseTotal
$opt{'marks'} ? : 28 37 65
$side eq 'b' ? : 16 12 28
100 65 my $to = $m->to;
101 65 ▶ my $where = defined $opt{previous} && $opt{previous} eq $to
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'previous'} && $opt{'previous'} eq $to ? : 7 58 65
Condition100%3/3
defined $opt{"previous"} and $opt{"previous"} eq $to
!ll&&!rl&&r
47117
MC/DC100%2/2
defined $opt{"previous"} and $opt{"previous"} eq $to
defined $opt{"previous"} $opt{"previous"} eq $to
Truth table100%3/3
A and B
A= defined $opt{"previous"}B= $opt{"previous"} eq $to
ABresult
0X0
100
111
102 ? '同'
103 : $FILE[ substr $to, 0, 1 ] . $RANK[ ord(substr $to, 1) - 96 ];
104  
105 65 ▶ if ($m->is_drop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->is_drop) 5 60 65
106 5 ▶ my $token = $side eq 'w' ? lc $m->drop : $m->drop;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'w' ? : 1 4 5
107 5 ▶ my $held = _rivals($b, $legal, $token, $to) ? '打' : '';
Branch: 1 branch
BranchTrueFalseTotal
_rivals($b, $legal, $token, $to) ? : 2 3 5
108 5 return $mark . $where . $NAME{ $m->drop } . $held;
109 }
110  
111 60 my $token = $b->at($E->square($m->from));
112 60 my @rivals = _rivals($b, $legal, $token, $to, $m->from);
113 60 ▶ my $how = @rivals ? _resolve($side, $token, $m->from, $to, @rivals) : '';
Branch: 1 branch
BranchTrueFalseTotal
@rivals ? : 24 36 60
114 60 my $plain = $m->from . $to;
115 60 ▶ my $status = $m->promote ? '成' : $legal->{"$plain+"} ? '不成' : '';
Branch: 2 branches
BranchTrueFalseTotal
$m->promote ? : 7 53 60
$$legal{"$plain+"} ? : 3 50 53
116 60 return $mark . $where . $NAME{ uc $token } . $how . $status;
117 }
118  
119 8 ▶sub record {
Subroutine record: called CC=7 SCAR=19.5
Pod documented
120 8 my ($class, $start, @moves) = @_;
121 8 ▶ my $b = defined $start ? $E->new(sfen => $start) : $E->new;
Branch: 1 branch
BranchTrueFalseTotal
defined $start ? : 5 3 8
122 8 my ($previous, @line);
123 8 for my $i (0 .. $#moves) {
124 26 my $usi = $moves[$i];
125 26 my $fault = $b->move_fault($usi);
126 26 ▶ Carp::croak("Game::Shogi::Notation: move " . ($i + 1) . " ('" . ($usi // 'undef')
Branch: 1 branch
BranchTrueFalseTotal
if $fault 1 25 26
Condition50%1/2
$usi // "undef"
l!l
10
MC/DC0%0/1
$usi // "undef"
$usi
Truth table50%1/2
A
A= $usi
Aresult
11
00
127 . "') is not legal: " . $E->fault_reason($fault)) if $fault;
128 25 ▶ Carp::croak("Game::Shogi::Notation: move " . ($i + 1) . " comes after the game ended")
Branch: 1 branch
BranchTrueFalseTotal
if $b->outcome 0 25 25
129 if $b->outcome;
130 25 push @line, {
131 usi => $usi,
132 side => $b->side_to_move,
133 hodges => $class->hodges($b, $usi),
134 japanese => $class->japanese($b, $usi, previous => $previous, marks => 1),
135 };
136 25 $b->do_move($usi);
137 25 $previous = Game::Shogi::Move->of($usi)->to;
138 }
139 return {
140 7 ▶ start => defined $start ? $start : 'lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1',
Branch: 1 branch
BranchTrueFalseTotal
defined $start ? : 5 2 7
141 moves => \@line,
142 sfen => $b->sfen,
143 result => $b->outcome,
144 };
145 }
146  
147 104 ▶sub replay {
Subroutine replay: called CC=7 SCAR=19.8
Pod documented
148 104 my ($class, $start, @moves) = @_;
149 104 ▶ my $b = defined $start ? $E->new(sfen => $start) : $E->new;
Branch: 1 branch
BranchTrueFalseTotal
defined $start ? : 2 102 104
150 104 for my $i (0 .. $#moves) {
151 14846 my $fault = $b->move_fault($moves[$i]);
152 14846 ▶ Carp::croak("Game::Shogi::Notation: move " . ($i + 1) . " ('" . ($moves[$i] // 'undef')
Branch: 1 branch
BranchTrueFalseTotal
if $fault 2 14844 14846
Condition50%1/2
$moves[$i] // "undef"
l!l
20
MC/DC0%0/1
$moves[$i] // "undef"
$moves[$i]
Truth table50%1/2
A
A= $moves[$i]
Aresult
11
00
153 . "') is not legal: " . $E->fault_reason($fault)) if $fault;
154 14844 ▶ Carp::croak("Game::Shogi::Notation: move " . ($i + 1) . " comes after the game ended")
Branch: 1 branch
BranchTrueFalseTotal
if $b->outcome 0 14844 14844
155 if $b->outcome;
156 14844 $b->do_move($moves[$i]);
157 }
158 102 ▶ return wantarray ? ($b, $b->outcome) : $b;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 102 0 102
159 }
160  
161 7 1;
162  
163 __END__
« blib/lib/Game/Shogi.pm ↑ Summary blib/lib/Game/Shogi/Result.pm »