blib/lib/Game/Sternhalma/Notation.pm

filter: Statementstmt 99.5% 210 / 211 Branchbran 89.3% 134 / 150 Conditioncond 64.7% 77 / 119 MC/DCmcdc 40.3% 27 / 67 Subroutinesub 100% 21 / 21 Podpod 100% 9 / 9 totaltotal 82.8% 478 / 577 CC 130 SCAR 30.4
CC
130
Cov
88%
parse_game
32.7
_read_game
30.1
parse_position
28.2
CRAP 20.8
« blib/lib/Game/Sternhalma/Terminal.pm ↑ Summary blib/lib/Game/Sternhalma/Bot.pm »
1 package Game::Sternhalma::Notation;
2  
3 55 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 55 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 55 our $VERSION = '0.01';
10  
11 55 our @EXPORT_OK = qw(
12 HOLES SETUP_TWO
13 name_of hole_of row_len
14 parse_move format_move
15 parse_position format_position
16 format_game parse_game
17 );
18 55 our %EXPORT_TAGS = (all => \@EXPORT_OK);
19  
20 55 ▶use constant HOLES => 121;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 55 ▶use constant SETUP_TWO => '0/00/000/0000/[13]/[12]/[11]/[10]/[9]/[10]/[11]/[12]/[13]/1111/111/11/1 0 2 10';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 55 my @ROW_LEN = (1, 2, 3, 4, 13, 12, 11, 10, 9, 10, 11, 12, 13, 4, 3, 2, 1);
24 55 my @LETTER = ('a' .. 'q');
25 55 my (@ROW_START, @NAME, %HOLE);
26 {
27 55 my $h = 0;
28 55 for my $r (0 .. $#ROW_LEN) {
29 935 $ROW_START[$r] = $h;
30 935 for my $c (1 .. $ROW_LEN[$r]) {
31 6655 $NAME[$h] = $LETTER[$r] . $c;
32 6655 $HOLE{ $NAME[$h] } = $h;
33 6655 $h++;
34 }
35 }
36 }
37  
38 55 my %LAYOUT = (1 => 1, 2 => 1, 3 => 1, 4 => 1, 6 => 1);
39  
40 333973 ▶sub _is_int { defined $_[0] && $_[0] =~ /\A[0-9]+\z/ }
Subroutine _is_int: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u 333973 0 333973
41  
42 18 ▶sub row_len { _is_int($_[0]) && $_[0] < @ROW_LEN ? $ROW_LEN[ $_[0] ] : undef }
Subroutine row_len: called CC=3 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_int($_[0]) && $_[0] < @ROW_LEN ? : 18 0 18
Condition33%1/3
_is_int($_[0]) and $_[0] < @ROW_LEN
!ll&&!rl&&r
0018
MC/DC0%0/2
_is_int($_[0]) and $_[0] < @ROW_LEN
_is_int($_[0]) $_[0] < @ROW_LEN
Truth table33%1/3
A and B
A= _is_int($_[0])B= $_[0] < @ROW_LEN
ABresult
0X0
100
111
43 333954 ▶sub name_of { _is_int($_[0]) && $_[0] < HOLES ? $NAME[ $_[0] ] : undef }
Subroutine name_of: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_int($_[0]) && $_[0] < 121 ? : 333951 3 333954
Condition100%3/3
_is_int($_[0]) and $_[0] < 121
!ll&&!rl&&r
21333951
MC/DC100%2/2
_is_int($_[0]) and $_[0] < 121
_is_int($_[0]) $_[0] < 121
Truth table100%3/3
A and B
A= _is_int($_[0])B= $_[0] < 121
ABresult
0X0
100
111
44 308683 ▶sub hole_of { defined $_[0] ? $HOLE{ $_[0] } : undef }
Subroutine hole_of: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 308682 1 308683
45  
46 # ---- moves --------------------------------------------------------------------------
47  
48 106992 ▶sub parse_move {
Subroutine parse_move: called CC=11 SCAR=24.0
Pod documented
49 106992 my ($text) = @_;
50 106992 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 106992 106992
51 106992 ▶ return { kind => 'pass' } if $text eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $text eq "pass" 5 106987 106992
52 106987 ▶ return { kind => 'resign', seat => $1 + 0 } if $text =~ /\Aresign ([0-5])\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\Aresign ([0-5])\z/u 391 106596 106987
53 106596 ▶ if ($text =~ /\A([a-q][0-9]{1,2})-([a-q][0-9]{1,2})\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A([a-q][0-9]{1,2})-([a-q][0-9]{1,2})\z/u) 63696 42900 106596
54 63696 my ($f, $t) = (hole_of($1), hole_of($2));
55 63696 ▶ return undef unless defined $f && defined $t && $f != $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f and defined $t and $f != $t 2 63694 63696
Condition83%5/6
defined $f and defined $t and $f != $t
!ll&&!rl&&r
1163694
defined $f and defined $t
!ll&&!rl&&r
1063695
MC/DC66%2/3
defined $f and defined $t and $f != $t
defined $f defined $t $f != $t
Truth table75%3/4
A and B and C
A= defined $fB= defined $tC= $f != $t
ABCresult
0XX0
10X0
1100
1111
56 63694 return { kind => 'step', holes => [ $f, $t ] };
57 }
58 42900 my @names = split /:/, $text, -1;
59 42900 ▶ return undef unless @names >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @names >= 2 18 42882 42900
60 42882 my @holes = map { hole_of($_) } @names;
61 42882 ▶ return undef if grep { !defined } @holes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @holes 1 42881 42882
62 42881 ▶ return undef if $holes[0] == $holes[-1];
Branch: 1 branch
BranchTrueFalseTotal
if $holes[0] == $holes[-1] 1 42880 42881
63 42880 return { kind => 'chain', holes => \@holes };
64 }
65  
66 137446 ▶sub format_move {
Subroutine format_move: called CC=12 SCAR=27.4
Pod documented
67 137446 my ($m) = @_;
68 137446 ▶ return undef unless ref $m eq 'HASH' && defined $m->{kind};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $m eq "HASH" and defined $m->{"kind"} 0 137446 137446
Condition33%1/3
ref $m eq "HASH" and defined $m->{"kind"}
!ll&&!rl&&r
00137446
MC/DC0%0/2
ref $m eq "HASH" and defined $m->{"kind"}
ref $m eq "HASH" defined $m->{"kind"}
Truth table33%1/3
A and B
A= ref $m eq "HASH"B= defined $m->{"kind"}
ABresult
0X0
100
111
69 137446 ▶ return 'pass' if $m->{kind} eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "pass" 1 137445 137446
70 137445 ▶ return "resign $m->{seat}" if $m->{kind} eq 'resign' && _is_int($m->{seat});
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "resign" and _is_int($m->{"seat"}) 1 137444 137445
Condition66%2/3
$m->{"kind"} eq "resign" and _is_int($m->{"seat"})
!ll&&!rl&&r
13744401
MC/DC50%1/2
$m->{"kind"} eq "resign" and _is_int($m->{"seat"})
$m->{"kind"} eq "resign" _is_int($m->{"seat"})
Truth table66%2/3
A and B
A= $m->{"kind"} eq "resign"B= _is_int($m->{"seat"})
ABresult
0X0
100
111
71 137444 ▶ my @names = map { name_of($_) } @{ $m->{holes} || [] };
Condition50%1/2
$m->{'holes'} || []
l!l
1374440
MC/DC0%0/1
$m->{'holes'} || []
$m->{'holes'}
Truth table50%1/2
A
A= $m->{'holes'}
Aresult
11
00
72 137444 ▶ return undef if @names < 2 || grep { !defined } @names;
Branch: 1 branch
BranchTrueFalseTotal
if @names < 2 or grep {not defined $_;} @names 0 137444 137444
Condition33%1/3
@names < 2 or grep {not defined $_;} @names
l!l&&r!l&&!r
00137444
MC/DC0%0/2
@names < 2 or grep {not defined $_;} @names
@names < 2 grep {not defined $_;} @names
Truth table33%1/3
A or B
A= @names < 2B= grep {not defined $_;} @names
ABresult
1X1
011
000
73 137444 ▶ return join '-', @names if $m->{kind} eq 'step' && @names == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "step" and @names == 2 79364 58080 137444
Condition66%2/3
$m->{"kind"} eq "step" and @names == 2
!ll&&!rl&&r
58080079364
MC/DC50%1/2
$m->{"kind"} eq "step" and @names == 2
$m->{"kind"} eq "step" @names == 2
Truth table66%2/3
A and B
A= $m->{"kind"} eq "step"B= @names == 2
ABresult
0X0
100
111
74 58080 ▶ return join ':', @names if $m->{kind} eq 'chain';
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "chain" 58080 0 58080
75 0 return undef;
76 }
77  
78 # ---- the position string ----------------------------------------------------------------
79 #
80 # The same grammar and the same refusal codes as the engine, written apart from
81 # it: t/22-notation-no-xs.t holds the two to agreeing.
82  
83 55 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
84 55 POS_OK => 0, POS_NULL => 1, POS_LONG => 2, POS_LETTER => 3, POS_ROWS => 4,
85 POS_WIDTH => 5, POS_FIELDS => 6, POS_SEAT => 7, POS_COUNT => 8, POS_GAME => 9,
86 55 };
87  
88 25099 ▶sub _race_over {
Subroutine _race_over: called CC=3 SCAR=11.3
89 25099 my ($p) = @_;
90 25099 my $n = _active_count($p);
91 25099 ▶ return $n == 0 || ($p->{seats} > 1 && $n == 1);
Condition66%4/6
$n == 0 || $p->{'seats'} > 1 && $n == 1
l!l&&r!l&&!r
02525074
$p->{"seats"} > 1 and $n == 1
!ll&&!rl&&r
02507425
MC/DC33%1/3
$n == 0 || $p->{'seats'} > 1 && $n == 1
$n == 0 $p->{"seats"} > 1 $n == 1
Truth table50%2/4
A || B and C
A= $n == 0B= $p->{"seats"} > 1C= $n == 1
ABCresult
1XX1
0111
00X0
0100
92 }
93  
94 215147 ▶sub _active {
Subroutine _active: called CC=1 SCAR=0.0
95 215147 my ($p, $s) = @_;
96 215147 return !grep { $_ == $s } @{ $p->{finished} }, @{ $p->{resigned} };
97 }
98  
99 50031 ▶sub _active_count {
Subroutine _active_count: called CC=1 SCAR=0.0
100 50031 my ($p) = @_;
101 50031 return scalar grep { _active($p, $_) } 0 .. $p->{seats} - 1;
102 }
103  
104 27118 ▶sub parse_position {
Subroutine parse_position: called CC=27 SCAR=33.4
Pod documented
105 27118 my ($str) = @_;
106 27118 ▶ return (undef, POS_NULL) unless defined $str && length $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and length $str 0 27118 27118
Condition33%1/3
defined $str and length $str
!ll&&!rl&&r
0027118
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
107 27118 ▶ return (undef, POS_LONG) if length($str) >= 512;
Branch: 1 branch
BranchTrueFalseTotal
if length $str >= 512 0 27118 27118
108 27118 my ($rows, $fields) = $str =~ /\A([^ ]*)(.*)\z/s;
109 27118 my $tmp = $rows;
110 27118 ▶ while (length $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tmp) 2124222 25932 2150154
111 2124222 ▶ next if $tmp =~ s/\A[\/0-5]//;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp =~ s[\A[/0-5]][]u 1430178 694044 2124222
112 694044 ▶ next if $tmp =~ s/\A\[([1-9][0-9]?)\]// && $1 <= 13;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp =~ s/\A\[([1-9][0-9]?)\]//u and $1 <= 13 692858 1186 694044
Condition100%3/3
$tmp =~ s/\A\[([1-9][0-9]?)\]//u and $1 <= 13
!ll&&!rl&&r
1049137692858
MC/DC100%2/2
$tmp =~ s/\A\[([1-9][0-9]?)\]//u and $1 <= 13
$tmp =~ s/\A\[([1-9][0-9]?)\]//u $1 <= 13
Truth table100%3/3
A and B
A= $tmp =~ s/\A\[([1-9][0-9]?)\]//uB= $1 <= 13
ABresult
0X0
100
111
113 1186 return (undef, POS_LETTER);
114 }
115 25932 my @rows = split /\//, $rows, -1;
116 25932 ▶ return (undef, POS_ROWS) unless @rows == 17;
Branch: 1 branch
BranchTrueFalseTotal
unless @rows == 17 267 25665 25932
117 25665 my (@cells, %count, $seat_max);
118 25665 $seat_max = -1;
119 25665 for my $r (0 .. 16) {
120 434178 my $width = 0;
121 434178 my @row;
122 434178 for my $tok ($rows[$r] =~ /(\[\d+\]|[0-5])/g) {
123 1654589 ▶ if ($tok =~ /\[(\d+)\]/) { push @row, (undef) x $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($tok =~ /\[(\d+)\]/u) { } 683940 970649 1654589
124 970649 ▶ else { push @row, $tok + 0; $seat_max = $tok if $tok > $seat_max }
Branch: 1 branch
BranchTrueFalseTotal
if $tok > $seat_max 63105 907544 970649
125 }
126 434178 ▶ return (undef, POS_WIDTH) unless @row == $ROW_LEN[$r];
Branch: 1 branch
BranchTrueFalseTotal
unless @row == $ROW_LEN[$r] 284 433894 434178
127 433894 push @cells, @row;
128 }
129 25381 ▶ my ($to_move, $seats, $pieces, $game) = $fields =~ /\A ([0-5]) ([1-6]) ([1-9][0-9]?)(?:;(.*))?\z/s
Branch: 1 branch
BranchTrueFalseTotal
() = $fields =~ /\A ([0-5]) ([1-6]) ([1-9][0-9]?)(?:;(.*))?\z/su or return undef, 6 25258 123 25381
130 or return (undef, POS_FIELDS);
131 return (undef, POS_FIELDS)
132 25258 ▶ unless $LAYOUT{$seats} && ($pieces == 10 || ($pieces == 15 && $seats <= 3)) && $to_move < $seats;
Branch: 1 branch
BranchTrueFalseTotal
unless $LAYOUT{$seats} and $pieces == 10 || $pieces == 15 && $seats <= 3 and $to_move < $seats 8 25250 25258
Condition58%7/12
$LAYOUT{$seats} and $pieces == 10 || $pieces == 15 && $seats <= 3 and $to_move < $seats
!ll&&!rl&&r
8025250
$LAYOUT{$seats} and $pieces == 10 || $pieces == 15 && $seats <= 3
!ll&&!rl&&r
0825250
$pieces == 10 or $pieces == 15 and $seats <= 3
l!l&&r!l&&!r
2525008
$pieces == 15 and $seats <= 3
!ll&&!rl&&r
800
MC/DC20%1/5
$LAYOUT{$seats} and $pieces == 10 || $pieces == 15 && $seats <= 3 and $to_move < $seats
$LAYOUT{$seats} $pieces == 10 $pieces == 15 $seats <= 3 $to_move < $seats
Truth table28%2/7
A and B or C and D and E
A= $LAYOUT{$seats}B= $pieces == 10C= $pieces == 15D= $seats <= 3E= $to_move < $seats
ABCDEresult
0XXXX0
100XX0
1010X0
11XX00
101100
11XX11
101111
133 25250 ▶ return (undef, POS_SEAT) if $seat_max >= $seats;
Branch: 1 branch
BranchTrueFalseTotal
if $seat_max >= $seats 14 25236 25250
134 25236 $count{$_}++ for grep { defined } @cells;
135 25236 for my $s (0 .. $seats - 1) {
136 95850 ▶ return (undef, POS_COUNT) unless ($count{$s} || 0) == $pieces;
Branch: 1 branch
BranchTrueFalseTotal
unless ($count{$s} || 0) == $pieces 40 95810 95850
Condition50%1/2
$count{$s} || 0
l!l
958500
MC/DC0%0/1
$count{$s} || 0
$count{$s}
Truth table50%1/2
A
A= $count{$s}
Aresult
11
00
137 }
138 25196 my %p = (cells => \@cells, to_move => $to_move + 0, seats => $seats + 0, pieces => $pieces + 0,
139 finished => [], resigned => [], ply => 0, pass_run => 0, cap => 1000 * $seats, how => 0);
140 25196 ▶ if (defined $game) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $game) 25110 86 25196
141 25110 ▶ _read_game(\%p, $game) or return (undef, POS_GAME);
Branch: 1 branch
BranchTrueFalseTotal
_read_game(\%p, $game) or return undef, 9 25093 17 25110
142 }
143 25179 return (\%p, POS_OK);
144 }
145  
146 25110 ▶sub _read_game {
Subroutine _read_game: called CC=30 SCAR=34.1
147 25110 my ($p, $game) = @_;
148 25110 ▶ return 0 unless length $game;
Branch: 1 branch
BranchTrueFalseTotal
unless length $game 1 25109 25110
149 25109 my $at = 0;
150 25109 my $order = 'frpkco';
151 25109 my $num = qr/(?:0|[1-9][0-9]{0,8})/;
152 25109 for my $field (split / /, $game, -1) {
153 59449 ▶ my ($k, $v) = $field =~ /\A([a-z])=(.*)\z/ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = $field =~ /\A([a-z])=(.*)\z/u or return 0 59448 1 59449
154 59448 my $i = index($order, $k);
155 59448 ▶ return 0 if $i < $at;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $at 3 59445 59448
156 59445 $at = $i + 1;
157 59445 ▶ if ($k eq 'f' || $k eq 'r') {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'f' or $k eq 'r') { } 9247 50198 59445
elsif ($k eq 'o') { } 169 50029 50198
Condition100%3/3
$k eq "f" or $k eq "r"
l!l&&r!l&&!r
153909450198
MC/DC100%2/2
$k eq "f" or $k eq "r"
$k eq "f" $k eq "r"
Truth table100%3/3
A or B
A= $k eq "f"B= $k eq "r"
ABresult
1X1
011
000
158 9247 ▶ return 0 unless $v =~ /\A[0-5](?:,[0-5])*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\A[0-5](?:,[0-5])*\z/u 0 9247 9247
159 9247 my @s = split /,/, $v;
160 9247 my %seen = map { $_ => 1 } @{ $p->{finished} }, @{ $p->{resigned} };
161 9247 ▶ for (@s) { return 0 if $_ >= $p->{seats} || $seen{$_}++ }
Branch: 1 branch
BranchTrueFalseTotal
if $_ >= $p->{"seats"} or $seen{$_}++ 3 12670 12673
Condition100%3/3
$_ >= $p->{"seats"} or $seen{$_}++
l!l&&r!l&&!r
1212670
MC/DC100%2/2
$_ >= $p->{"seats"} or $seen{$_}++
$_ >= $p->{"seats"} $seen{$_}++
Truth table100%3/3
A or B
A= $_ >= $p->{"seats"}B= $seen{$_}++
ABresult
1X1
011
000
162 9244 ▶ $p->{ $k eq 'f' ? 'finished' : 'resigned' } = [ map { $_ + 0 } @s ];
Branch: 1 branch
BranchTrueFalseTotal
$k eq 'f' ? : 151 9093 9244
163 }
164 elsif ($k eq 'o') {
165 169 ▶ return 0 unless $v =~ /\A[1-4]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\A[1-4]\z/u 1 168 169
166 168 $p->{how} = $v + 0;
167 }
168 else {
169 50029 ▶ return 0 unless $v =~ /\A$num\z/ && $v > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\A$num\z/u and $v > 0 2 50027 50029
Condition100%3/3
$v =~ /\A$num\z/u and $v > 0
!ll&&!rl&&r
1150027
MC/DC100%2/2
$v =~ /\A$num\z/u and $v > 0
$v =~ /\A$num\z/u $v > 0
Truth table100%3/3
A and B
A= $v =~ /\A$num\z/uB= $v > 0
ABresult
0X0
100
111
170 50027 $p->{ { p => 'ply', k => 'pass_run', c => 'cap' }->{$k} } = $v + 0;
171 }
172 }
173 25099 my $how = $p->{how};
174 25099 ▶ if ($how == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($how == 0) { } 24931 168 25099
elsif ($how == 1 or $how == 4) { } 25 143 168
elsif ($how == 2) { } 141 2 143
elsif ($how == 3) { } 2 0 2
175 24931 ▶ return 0 if _race_over($p) || $p->{pass_run} >= _active_count($p) || $p->{ply} >= $p->{cap};
Branch: 1 branch
BranchTrueFalseTotal
if _race_over($p) or $p->{"pass_run"} >= _active_count($p) or $p->{"ply"} >= $p->{"cap"} 2 24929 24931
Condition83%5/6
_race_over($p) or $p->{"pass_run"} >= _active_count($p) or $p->{"ply"} >= $p->{"cap"}
l!l&&r!l&&!r
1124929
_race_over($p) or $p->{"pass_run"} >= _active_count($p)
l!l&&r!l&&!r
1024930
MC/DC66%2/3
_race_over($p) or $p->{"pass_run"} >= _active_count($p) or $p->{"ply"} >= $p->{"cap"}
_race_over($p) $p->{"pass_run"} >= _active_count($p) $p->{"ply"} >= $p->{"cap"}
Truth table75%3/4
A or B or C
A= _race_over($p)B= $p->{"pass_run"} >= _active_count($p)C= $p->{"ply"} >= $p->{"cap"}
ABCresult
1XX1
01X1
0011
0000
176 24929 ▶ return 0 unless _active($p, $p->{to_move});
Branch: 1 branch
BranchTrueFalseTotal
unless _active($p, $p->{"to_move"}) 1 24928 24929
177 }
178 25 ▶ elsif ($how == 1 || $how == 4) { return 0 unless _race_over($p) }
Branch: 1 branch
BranchTrueFalseTotal
unless _race_over($p) 1 24 25
Condition100%3/3
$how == 1 or $how == 4
l!l&&r!l&&!r
223143
MC/DC100%2/2
$how == 1 or $how == 4
$how == 1 $how == 4
Truth table100%3/3
A or B
A= $how == 1B= $how == 4
ABresult
1X1
011
000
179 141 ▶ elsif ($how == 2) { return 0 if _race_over($p) || $p->{ply} < $p->{cap} }
Branch: 1 branch
BranchTrueFalseTotal
if _race_over($p) or $p->{"ply"} < $p->{"cap"} 1 140 141
Condition66%2/3
_race_over($p) or $p->{"ply"} < $p->{"cap"}
l!l&&r!l&&!r
01140
MC/DC50%1/2
_race_over($p) or $p->{"ply"} < $p->{"cap"}
_race_over($p) $p->{"ply"} < $p->{"cap"}
Truth table66%2/3
A or B
A= _race_over($p)B= $p->{"ply"} < $p->{"cap"}
ABresult
1X1
011
000
180 2 ▶ elsif ($how == 3) { return 0 if _race_over($p) || $p->{pass_run} < _active_count($p) }
Branch: 1 branch
BranchTrueFalseTotal
if _race_over($p) or $p->{"pass_run"} < _active_count($p) 1 1 2
Condition66%2/3
_race_over($p) or $p->{"pass_run"} < _active_count($p)
l!l&&r!l&&!r
011
MC/DC50%1/2
_race_over($p) or $p->{"pass_run"} < _active_count($p)
_race_over($p) $p->{"pass_run"} < _active_count($p)
Truth table66%2/3
A or B
A= _race_over($p)B= $p->{"pass_run"} < _active_count($p)
ABresult
1X1
011
000
181 25093 return 1;
182 }
183  
184 25091 ▶sub format_position {
Subroutine format_position: called CC=16 SCAR=27.8
Pod documented
185 25091 my ($p) = @_;
186 25091 my @rows;
187 25091 for my $r (0 .. 16) {
188 426547 my ($s, $run) = ('', 0);
189 426547 for my $h ($ROW_START[$r] .. $ROW_START[$r] + $ROW_LEN[$r] - 1) {
190 3036011 my $c = $p->{cells}[$h];
191 3036011 ▶ if (!defined $c) { $run++; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 2082021 953990 3036011
192 953990 ▶ $s .= "[$run]" if $run;
Branch: 1 branch
BranchTrueFalseTotal
if $run 430864 523126 953990
193 953990 $run = 0;
194 953990 $s .= $c;
195 }
196 426547 ▶ $s .= "[$run]" if $run;
Branch: 1 branch
BranchTrueFalseTotal
if $run 248527 178020 426547
197 426547 push @rows, $s;
198 }
199 25091 my $str = join('/', @rows) . " $p->{to_move} $p->{seats} $p->{pieces}";
200 25091 my @game;
201 25091 ▶ push @game, 'f=' . join(',', @{ $p->{finished} }) if @{ $p->{finished} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$p->{"finished"} or [];} 147 24944 25091
Condition50%1/2
$p->{'finished'} || []
l!l
250910
MC/DC0%0/1
$p->{'finished'} || []
$p->{'finished'}
Truth table50%1/2
A
A= $p->{'finished'}
Aresult
11
00
202 25091 ▶ push @game, 'r=' . join(',', @{ $p->{resigned} }) if @{ $p->{resigned} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$p->{"resigned"} or [];} 9092 15999 25091
Condition50%1/2
$p->{'resigned'} || []
l!l
250910
MC/DC0%0/1
$p->{'resigned'} || []
$p->{'resigned'}
Truth table50%1/2
A
A= $p->{'resigned'}
Aresult
11
00
203 25091 ▶ push @game, "p=$p->{ply}" if $p->{ply};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"ply"} 24928 163 25091
204 25091 ▶ push @game, "k=$p->{pass_run}" if $p->{pass_run};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"pass_run"} 1 25090 25091
205 25091 ▶ push @game, "c=$p->{cap}" if defined $p->{cap} && $p->{cap} != 1000 * $p->{seats};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"cap"} and $p->{"cap"} != 1000 * $p->{"seats"} 25084 7 25091
Condition66%2/3
defined $p->{"cap"} and $p->{"cap"} != 1000 * $p->{"seats"}
!ll&&!rl&&r
0725084
MC/DC50%1/2
defined $p->{"cap"} and $p->{"cap"} != 1000 * $p->{"seats"}
defined $p->{"cap"} $p->{"cap"} != 1000 * $p->{"seats"}
Truth table66%2/3
A and B
A= defined $p->{"cap"}B= $p->{"cap"} != 1000 * $p->{"seats"}
ABresult
0X0
100
111
206 25091 ▶ push @game, "o=$p->{how}" if $p->{how};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"how"} 163 24928 25091
207 25091 ▶ $str .= ';' . join(' ', @game) if @game;
Branch: 1 branch
BranchTrueFalseTotal
if @game 25088 3 25091
208 25091 return $str;
209 }
210  
211 # ---- a game as text ----------------------------------------------------------------------
212  
213 754 ▶sub format_game {
Subroutine format_game: called CC=8 SCAR=21.0
Pod documented
214 754 my ($g) = @_;
215 754 ▶ my $seats = $g->{seats} // 2;
Condition50%1/2
$g->{"seats"} // 2
l!l
7540
MC/DC0%0/1
$g->{"seats"} // 2
$g->{"seats"}
Truth table50%1/2
A
A= $g->{"seats"}
Aresult
11
00
216 754 ▶ my $pieces = $g->{pieces} // 10;
Condition50%1/2
$g->{"pieces"} // 10
l!l
7540
MC/DC0%0/1
$g->{"pieces"} // 10
$g->{"pieces"}
Truth table50%1/2
A
A= $g->{"pieces"}
Aresult
11
00
217 754 ▶ my $cap = $g->{cap} // 1000 * $seats;
Condition100%2/2
$g->{"cap"} // 1000 * $seats
l!l
73321
MC/DC100%1/1
$g->{"cap"} // 1000 * $seats
$g->{"cap"}
Truth table100%2/2
A
A= $g->{"cap"}
Aresult
11
00
218 754 ▶ my $first = $g->{first} // 0;
Condition100%2/2
$g->{"first"} // 0
l!l
233521
MC/DC100%1/1
$g->{"first"} // 0
$g->{"first"}
Truth table100%2/2
A
A= $g->{"first"}
Aresult
11
00
219 return join '', "sternhalma 1\n", "seats $seats\n", "pieces $pieces\n", "cap $cap\n",
220 ($first ? "first $first\n" : ()), (defined $g->{start} ? "start $g->{start}\n" : ()), "moves\n",
221 754 ▶ map { "$_\n" } @{ $g->{moves} || [] };
Branch: 2 branches
BranchTrueFalseTotal
$first ? : 2 752 754
defined $g->{'start'} ? : 27 727 754
Condition50%1/2
$g->{'moves'} || []
l!l
7540
MC/DC0%0/1
$g->{'moves'} || []
$g->{'moves'}
Truth table50%1/2
A
A= $g->{'moves'}
Aresult
11
00
222 }
223  
224 543 ▶sub parse_game {
Subroutine parse_game: called CC=24 SCAR=34.9
Pod documented
225 543 my ($text) = @_;
226 543 ▶ return (undef, 'no text') unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 543 543
227 543 my @lines = split /\n/, $text;
228 543 ▶ return (undef, 'not a sternhalma game') unless @lines && $lines[0] eq 'sternhalma 1';
Branch: 1 branch
BranchTrueFalseTotal
unless @lines and $lines[0] eq "sternhalma 1" 2 541 543
Condition66%2/3
@lines and $lines[0] eq "sternhalma 1"
!ll&&!rl&&r
02541
MC/DC50%1/2
@lines and $lines[0] eq "sternhalma 1"
@lines $lines[0] eq "sternhalma 1"
Truth table66%2/3
A and B
A= @linesB= $lines[0] eq "sternhalma 1"
ABresult
0X0
100
111
229 541 my %g = (moves => []);
230 541 my $i = 1;
231 541 for my $key (qw(seats pieces cap)) {
232 1623 ▶ my ($k, $v) = ($lines[$i] // '') =~ /\A(\w+) ([0-9]+)\z/;
Condition50%1/2
$lines[$i] // ""
l!l
16230
MC/DC0%0/1
$lines[$i] // ""
$lines[$i]
Truth table50%1/2
A
A= $lines[$i]
Aresult
11
00
233 1623 ▶ return (undef, "line " . ($i + 1) . " should be $key") unless defined $k && $k eq $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and $k eq $key 0 1623 1623
Condition33%1/3
defined $k and $k eq $key
!ll&&!rl&&r
001623
MC/DC0%0/2
defined $k and $k eq $key
defined $k $k eq $key
Truth table33%1/3
A and B
A= defined $kB= $k eq $key
ABresult
0X0
100
111
234 1623 $g{$key} = $v + 0;
235 1623 $i++;
236 }
237 541 $g{first} = 0;
238 541 ▶ if (($lines[$i] // '') =~ /\Afirst ([1-5])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($lines[$i] // "") =~ /\Afirst ([1-5])\z/u) 1 540 541
Condition50%1/2
$lines[$i] // ""
l!l
5410
MC/DC0%0/1
$lines[$i] // ""
$lines[$i]
Truth table50%1/2
A
A= $lines[$i]
Aresult
11
00
239 1 $g{first} = $1 + 0;
240 1 $i++;
241 }
242 541 ▶ return (undef, "first is seat $g{first}, and there are $g{seats} seats") if $g{first} >= $g{seats};
Branch: 1 branch
BranchTrueFalseTotal
if $g{"first"} >= $g{"seats"} 0 541 541
243 541 ▶ if (($lines[$i] // '') =~ /\Astart (.+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($lines[$i] // "") =~ /\Astart (.+)\z/u) 4 537 541
Condition50%1/2
$lines[$i] // ""
l!l
5410
MC/DC0%0/1
$lines[$i] // ""
$lines[$i]
Truth table50%1/2
A
A= $lines[$i]
Aresult
11
00
244 4 my ($p, $code) = parse_position($1);
245 4 ▶ return (undef, 'line ' . ($i + 1) . " is not a position (code $code)") unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 4 4
246 return (undef, 'the start position is not of the seats and pieces the header says')
247 4 ▶ unless $p->{seats} == $g{seats} && $p->{pieces} == $g{pieces};
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"seats"} == $g{"seats"} and $p->{"pieces"} == $g{"pieces"} 0 4 4
Condition33%1/3
$p->{"seats"} == $g{"seats"} and $p->{"pieces"} == $g{"pieces"}
!ll&&!rl&&r
004
MC/DC0%0/2
$p->{"seats"} == $g{"seats"} and $p->{"pieces"} == $g{"pieces"}
$p->{"seats"} == $g{"seats"} $p->{"pieces"} == $g{"pieces"}
Truth table33%1/3
A and B
A= $p->{"seats"} == $g{"seats"}B= $p->{"pieces"} == $g{"pieces"}
ABresult
0X0
100
111
248 4 $g{start} = $1;
249 4 $i++;
250 }
251 return (undef, 'no game is ' . "$g{seats} seats of $g{pieces} pieces")
252 541 ▶ unless $LAYOUT{ $g{seats} } && ($g{pieces} == 10 || ($g{pieces} == 15 && $g{seats} <= 3));
Branch: 1 branch
BranchTrueFalseTotal
unless $LAYOUT{$g{"seats"}} and $g{"pieces"} == 10 || $g{"pieces"} == 15 && $g{"seats"} <= 3 1 540 541
Condition55%5/9
$LAYOUT{$g{"seats"}} and $g{"pieces"} == 10 || $g{"pieces"} == 15 && $g{"seats"} <= 3
!ll&&!rl&&r
10540
$g{"pieces"} == 10 or $g{"pieces"} == 15 and $g{"seats"} <= 3
l!l&&r!l&&!r
490500
$g{"pieces"} == 15 and $g{"seats"} <= 3
!ll&&!rl&&r
0050
MC/DC25%1/4
$LAYOUT{$g{"seats"}} and $g{"pieces"} == 10 || $g{"pieces"} == 15 && $g{"seats"} <= 3
$LAYOUT{$g{"seats"}} $g{"pieces"} == 10 $g{"pieces"} == 15 $g{"seats"} <= 3
Truth table60%3/5
A and B or C and D
A= $LAYOUT{$g{"seats"}}B= $g{"pieces"} == 10C= $g{"pieces"} == 15D= $g{"seats"} <= 3
ABCDresult
0XXX0
100X0
10100
11XX1
10111
253 540 ▶ return (undef, 'line ' . ($i + 1) . ' should be moves') unless ($lines[$i] // '') eq 'moves';
Branch: 1 branch
BranchTrueFalseTotal
unless ($lines[$i] // "") eq "moves" 0 540 540
Condition50%1/2
$lines[$i] // ""
l!l
5400
MC/DC0%0/1
$lines[$i] // ""
$lines[$i]
Truth table50%1/2
A
A= $lines[$i]
Aresult
11
00
254 540 for my $n ($i + 1 .. $#lines) {
255 52882 ▶ return (undef, 'line ' . ($n + 1) . " is not a move: $lines[$n]") unless parse_move($lines[$n]);
Branch: 1 branch
BranchTrueFalseTotal
unless parse_move($lines[$n]) 1 52881 52882
256 52881 push @{ $g{moves} }, $lines[$n];
257 }
258 539 return (\%g, undef);
259 }
260  
261 55 1;
262  
263 __END__
« blib/lib/Game/Sternhalma/Terminal.pm ↑ Summary blib/lib/Game/Sternhalma/Bot.pm »