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 30 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 30 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Game::Shogi::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Game::Shogi::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 30 our $VERSION = '0.02';
13  
14 30 my $E = 'Game::Shogi::Engine';
15  
16 30 my @FILE = (undef, qw(1 2 3 4 5 6 7 8 9));
17 30 my @RANK = (undef, qw(一 二 三 四 五 六 七 八 九));
18 30 my %NAME = (
19 P => '歩', L => '香', N => '桂', S => '銀', G => '金', B => '角', R => '飛', K => '玉',
20 '+P' => 'と', '+L' => '成香', '+N' => '成桂', '+S' => '成銀', '+B' => '馬', '+R' => '龍',
21 );
22  
23 30 my %STRAIGHT = map { $_ => 1 } qw(S G +P +L +N +S);
24  
25 203 ▶sub _legal_set { my ($b) = @_; return { map { $_ => 1 } $b->legal } }
Subroutine _legal_set: called CC=1 SCAR=0.0
26  
27 166 ▶sub _rivals {
Subroutine _rivals: called CC=8 SCAR=20.8
28 166 my ($b, $legal, $token, $to, $skip) = @_;
29 166 my @from;
30 166 for my $sq (0 .. 80) {
31 13446 my $t = $b->at($sq);
32 13446 ▶ next unless defined $t && $t eq $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and $t eq $token 12905 541 13446
Condition100%3/3
defined $t and $t eq $token
!ll&&!rl&&r
97513154541
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 541 my $name = $E->square_name($sq);
34 541 ▶ next if defined $skip && $name eq $skip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $skip and $name eq $skip 161 380 541
Condition100%3/3
defined $skip and $name eq $skip
!ll&&!rl&&r
3377161
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 380 ▶ push @from, $name if $legal->{"$name$to"} || $legal->{"$name$to+"};
Branch: 1 branch
BranchTrueFalseTotal
if $$legal{"$name$to"} or $$legal{"$name$to+"} 75 305 380
Condition66%2/3
$$legal{"$name$to"} or $$legal{"$name$to+"}
l!l&&r!l&&!r
750305
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 166 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 128 ▶sub hodges {
Subroutine hodges: called CC=8 SCAR=20.8
Pod documented
43 128 my ($class, $b, $usi) = @_;
44 128 ▶ my $m = Game::Shogi::Move->of($usi) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $m = "Game::Shogi::Move"->of($usi) or return undef 127 1 128
45 127 my $legal = _legal_set($b);
46 127 ▶ return undef unless $legal->{$usi};
Branch: 1 branch
BranchTrueFalseTotal
unless $legal->{$usi} 1 126 127
47 126 ▶ return $m->drop . '*' . $m->to if $m->is_drop;
Branch: 1 branch
BranchTrueFalseTotal
if $m->is_drop 35 91 126
48  
49 91 my $token = $b->at($E->square($m->from));
50 91 my $letter = uc $token;
51 91 ▶ 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 67 91
52 91 ▶ my $kind = defined $b->at($E->square($m->to)) ? 'x' : '-';
Branch: 1 branch
BranchTrueFalseTotal
defined $b->at($E->square($m->to)) ? : 13 78 91
53 91 my $plain = $m->from . $m->to;
54 91 ▶ my $status = $m->promote ? '+' : $legal->{"$plain+"} ? '=' : '';
Branch: 2 branches
BranchTrueFalseTotal
$m->promote ? : 11 80 91
$$legal{"$plain+"} ? : 9 71 80
55 91 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 76 ▶sub japanese {
Subroutine japanese: called CC=13 SCAR=25.7
Pod documented
93 76 my ($class, $b, $usi, %opt) = @_;
94 76 ▶ my $m = Game::Shogi::Move->of($usi) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $m = "Game::Shogi::Move"->of($usi) or return undef 76 0 76
95 76 my $legal = _legal_set($b);
96 76 ▶ return undef unless $legal->{$usi};
Branch: 1 branch
BranchTrueFalseTotal
unless $legal->{$usi} 1 75 76
97  
98 75 my $side = $b->side_to_move;
99 75 ▶ my $mark = $opt{marks} ? ($side eq 'b' ? '▲' : '△') : '';
Branch: 2 branches
BranchTrueFalseTotal
$opt{'marks'} ? : 28 47 75
$side eq 'b' ? : 16 12 28
100 75 my $to = $m->to;
101 75 ▶ my $where = defined $opt{previous} && $opt{previous} eq $to
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'previous'} && $opt{'previous'} eq $to ? : 7 68 75
Condition100%3/3
defined $opt{"previous"} and $opt{"previous"} eq $to
!ll&&!rl&&r
57117
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 75 ▶ if ($m->is_drop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->is_drop) 5 70 75
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 70 my $token = $b->at($E->square($m->from));
112 70 my @rivals = _rivals($b, $legal, $token, $to, $m->from);
113 70 ▶ my $how = @rivals ? _resolve($side, $token, $m->from, $to, @rivals) : '';
Branch: 1 branch
BranchTrueFalseTotal
@rivals ? : 24 46 70
114 70 my $plain = $m->from . $to;
115 70 ▶ my $status = $m->promote ? '成' : $legal->{"$plain+"} ? '不成' : '';
Branch: 2 branches
BranchTrueFalseTotal
$m->promote ? : 7 63 70
$$legal{"$plain+"} ? : 3 60 63
116 70 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 30 1;
162  
163 __END__
« blib/lib/Game/Shogi.pm ↑ Summary blib/lib/Game/Shogi/Result.pm »