blib/lib/Game/Xiangqi/Notation.pm

filter: Statementstmt 100% 49 / 49 Branchbran 90.0% 27 / 30 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 90.0% 100 / 111 CC 20 SCAR 22.4
CC
20
Cov
92%
wxf_of
15.1
iccs_of
3.3
move_of_iccs
3.0
CRAP 9.4
« blib/lib/Game/Xiangqi.pm ↑ Summary blib/lib/Game/Xiangqi/Bot.pm »
1 package Game::Xiangqi::Notation;
2  
3 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use Game::Xiangqi::Engine ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 our $VERSION = '0.03';
10  
11 12 my $E = 'Game::Xiangqi::Engine';
12  
13 12 my %LETTER = (
14 GENERAL() => 'K',
15 ADVISOR() => 'A',
16 ELEPHANT() => 'E',
17 CHARIOT() => 'R',
18 HORSE() => 'H',
19 CANNON() => 'C',
20 SOLDIER() => 'P',
21 );
22  
23 12 my %DIAGONAL = (ADVISOR() => 1, ELEPHANT() => 1, HORSE() => 1);
24  
25 1151 ▶sub iccs_of {
Subroutine iccs_of: called CC=3 SCAR=12.0
Pod documented
26 1151 my ($class, $mv) = @_;
27 1151 my ($from, $to) = ($E->move_from($mv), $E->move_to($mv));
28 1151 ▶ return undef unless $E->on_board($from) && $E->on_board($to);
Branch: 1 branch
BranchTrueFalseTotal
unless $E->on_board($from) and $E->on_board($to) 0 1151 1151
Condition33%1/3
$E->on_board($from) and $E->on_board($to)
!ll&&!rl&&r
001151
MC/DC0%0/2
$E->on_board($from) and $E->on_board($to)
$E->on_board($from) $E->on_board($to)
Truth table33%1/3
A and B
A= $E->on_board($from)B= $E->on_board($to)
ABresult
0X0
100
111
29 1151 return sprintf('%s%d%s%d',
30 ('a' .. 'i')[ $E->file_of($from) ], $E->rank_of($from),
31 ('a' .. 'i')[ $E->file_of($to) ], $E->rank_of($to));
32 }
33  
34 282 ▶sub move_of_iccs {
Subroutine move_of_iccs: called CC=3 SCAR=11.0
Pod documented
35 282 my ($class, $s) = @_;
36 282 ▶ return undef unless defined $s && $s =~ /\A([a-i])([0-9])([a-i])([0-9])\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and $s =~ /\A([a-i])([0-9])([a-i])([0-9])\z/ 16 266 282
Condition100%3/3
defined $s and $s =~ /\A([a-i])([0-9])([a-i])([0-9])\z/
!ll&&!rl&&r
115266
MC/DC100%2/2
defined $s and $s =~ /\A([a-i])([0-9])([a-i])([0-9])\z/
defined $s $s =~ /\A([a-i])([0-9])([a-i])([0-9])\z/
Truth table100%3/3
A and B
A= defined $sB= $s =~ /\A([a-i])([0-9])([a-i])([0-9])\z/
ABresult
0X0
100
111
37 266 my %f = map { ('a' .. 'i')[$_] => $_ } 0 .. 8;
38 266 return $E->move($E->point_of($f{$1}, $2), $E->point_of($f{$3}, $4));
39 }
40  
41 105 ▶sub wxf_file {
Subroutine wxf_file: called CC=2 SCAR=6.9
Pod documented
42 105 my ($class, $file, $colour) = @_;
43 105 ▶ return $colour == RED ? 9 - $file : $file + 1;
Branch: 1 branch
BranchTrueFalseTotal
$colour == 8 ? : 95 10 105
44 }
45  
46 74 ▶sub wxf_of {
Subroutine wxf_of: called CC=15 SCAR=27.1
Pod documented
47 74 my ($class, $board, $mv) = @_;
48 74 my ($from, $to) = ($E->move_from($mv), $E->move_to($mv));
49 74 my $piece = $board->at($from);
50 74 ▶ return undef unless $piece && $piece != BORDER;
Branch: 1 branch
BranchTrueFalseTotal
unless $piece and $piece != 32 0 74 74
Condition33%1/3
$piece and $piece != 32
!ll&&!rl&&r
0074
MC/DC0%0/2
$piece and $piece != 32
$piece $piece != 32
Truth table33%1/3
A and B
A= $pieceB= $piece != 32
ABresult
0X0
100
111
51  
52 74 my $colour = Game::Xiangqi::Engine::colour_of($piece);
53 74 my $kind = Game::Xiangqi::Engine::kind_of($piece);
54 74 ▶ my $letter = $LETTER{$kind} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $letter = $LETTER{$kind} or return undef 74 0 74
55  
56 74 my ($ff, $fr) = ($E->file_of($from), $E->rank_of($from));
57 74 my ($tf, $tr) = ($E->file_of($to), $E->rank_of($to));
58  
59 my @same = grep {
60 74 ▶ $board->at($_) == $piece && $E->file_of($_) == $ff
Condition100%3/3
$board->at($_) == $piece && $E->file_of($_) == $ff
!ll&&!rl&&r
64977489
MC/DC100%2/2
$board->at($_) == $piece && $E->file_of($_) == $ff
$board->at($_) == $piece $E->file_of($_) == $ff
Truth table100%3/3
A && B
A= $board->at($_) == $pieceB= $E->file_of($_) == $ff
ABresult
0X0
100
111
61 } $E->all_points;
62  
63 74 my $head;
64 74 ▶ if (@same > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@same > 1) { } 9 65 74
65 my @order = sort {
66 9 ▶ $colour == RED ? $E->rank_of($b) <=> $E->rank_of($a)
Branch: 1 branch
BranchTrueFalseTotal
$colour == 8 ? : 14 2 16
67 : $E->rank_of($a) <=> $E->rank_of($b)
68 } @same;
69 9 my ($idx) = grep { $order[$_] == $from } 0 .. $#order;
70 9 ▶ $head = @same == 2 ? ($idx == 0 ? "+$letter" : "-$letter")
Branch: 2 branches
BranchTrueFalseTotal
@same == 2 ? : 5 4 9
$idx == 0 ? : 3 2 5
71 : ($idx + 1) . $letter;
72 }
73 else {
74 65 $head = $letter . $class->wxf_file($ff, $colour);
75 }
76  
77 74 ▶ my $dir = $tr == $fr ? '.'
Branch: 4 branches
BranchTrueFalseTotal
$tr == $fr ? : 19 55 74
$colour == 8 ? : 50 5 55
$tr > $fr ? : 47 3 50
$tr < $fr ? : 4 1 5
78 : ($colour == RED ? ($tr > $fr ? '+' : '-')
79 : ($tr < $fr ? '+' : '-'));
80  
81 my $tail = $dir eq '.' ? $class->wxf_file($tf, $colour)
82 74 ▶ : $DIAGONAL{$kind} ? $class->wxf_file($tf, $colour)
Branch: 2 branches
BranchTrueFalseTotal
$dir eq '.' ? : 19 55 74
$DIAGONAL{$kind} ? : 17 38 55
83 : abs($tr - $fr);
84  
85 74 return "$head$dir$tail";
86 }
87  
88 12 1;
89  
90 __END__
« blib/lib/Game/Xiangqi.pm ↑ Summary blib/lib/Game/Xiangqi/Bot.pm »