blib/lib/Game/Fanorona/Variant.pm

filter: Statementstmt 100% 116 / 116 Branchbran 96.5% 56 / 58 Conditioncond 81.4% 22 / 27 MC/DCmcdc 68.7% 11 / 16 Subroutinesub 100% 19 / 19 Podpod 87.5% 7 / 8 totaltotal 94.6% 231 / 244 CC 43 SCAR 21.6
CC
43
Cov
97%
__ANON__
18.0
custom
9.0
from_string
8.0
CRAP 8.7
« blib/lib/Game/Fanorona/Rules.pm ↑ Summary blib/lib/Game/Fanorona/Bot.pm »
1 package Game::Fanorona::Variant;
2  
3 27 ▶use 5.014;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 27 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 27 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 27 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 27 ▶use Game::Fanorona::Engine ();
Subroutine __ANON__: called CC=1 SCAR=0.0
11 27 ▶use Game::Fanorona::Rules ();
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 27 our $VERSION = '0.02';
14  
15 27 our @NAMES;
16 27 our @FIELDS;
17 27 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 @NAMES = qw(sivy dimy telo);
19 27 @FIELDS = qw(cols rows quiet_limit repeat ply_cap start);
20 }
21  
22 27 has name => (is => 'ro');
23  
24 27 has [@FIELDS] => (is => 'ro');
25  
26 27 my $MAKING = 0;
27  
28 27 my %DEFAULT;
29  
30 1440 ▶my $defaults = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
31 1440 ▶ return \%DEFAULT if %DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if %DEFAULT 1422 18 1440
32 18 my $game = Game::Fanorona::Rules->new(board => 'telo');
33 18 %DEFAULT = (
34 quiet_limit => $game->quiet_limit,
35 repeat => $game->repeat,
36 ply_cap => $game->ply_cap,
37 );
38 18 return \%DEFAULT;
39 27 };
40  
41 1414 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
42 1414 my ($self) = @_;
43 1414 ▶ Carp::croak('Game::Fanorona::Variant: a variant is made by named, custom or from_string')
Branch: 1 branch
BranchTrueFalseTotal
unless $MAKING 1 1413 1414
44 unless $MAKING;
45 1413 return;
46 }
47  
48 7121 ▶my $whole = sub { defined $_[0] && !ref $_[0] && $_[0] =~ /\A(?:0|[1-9][0-9]{0,5})\z/ };
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and $_[0] =~ /\A(?:0|[1-9][0-9]{0,5})\z/u 7121 0 7121
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
007121
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
49  
50 1430 ▶my $make = sub {
Subroutine __ANON__: called CC=18 SCAR=28.9
51 1430 my ($class, %all) = @_;
52 1430 my ($cols, $rows) = @all{qw(cols rows)};
53 1430 ▶ Carp::croak('Game::Fanorona::Variant: a board is cols and rows')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cols and defined $rows 2 1428 1430
Condition100%3/3
defined $cols and defined $rows
!ll&&!rl&&r
111428
MC/DC100%2/2
defined $cols and defined $rows
defined $cols defined $rows
Truth table100%3/3
A and B
A= defined $colsB= defined $rows
ABresult
0X0
100
111
54 unless defined $cols && defined $rows;
55 1428 ▶ Carp::croak("Game::Fanorona::Variant: no board is $cols by $rows")
Branch: 1 branch
BranchTrueFalseTotal
unless &$whole($cols) and &$whole($rows) and "Game::Fanorona::Engine"->size_ok($cols, $rows) 4 1424 1428
Condition83%5/6
&$whole($cols) and &$whole($rows) and "Game::Fanorona::Engine"->size_ok($cols, $rows)
!ll&&!rl&&r
131424
&$whole($cols) and &$whole($rows)
!ll&&!rl&&r
101427
MC/DC66%2/3
&$whole($cols) and &$whole($rows) and "Game::Fanorona::Engine"->size_ok($cols, $rows)
&$whole($cols) &$whole($rows) "Game::Fanorona::Engine"->size_ok($cols, $rows)
Truth table75%3/4
A and B and C
A= &$whole($cols)B= &$whole($rows)C= "Game::Fanorona::Engine"->size_ok($cols, $rows)
ABCresult
0XX0
10X0
1100
1111
56 unless $whole->($cols) && $whole->($rows) && Game::Fanorona::Engine->size_ok($cols, $rows);
57  
58 1424 my $default = $defaults->();
59 1424 for my $limit (qw(quiet_limit repeat ply_cap)) {
60 4266 ▶ $all{$limit} = $default->{$limit} unless defined $all{$limit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $all{$limit} 2651 1615 4266
61 Carp::croak("Game::Fanorona::Variant: $limit is a whole number, not '$all{$limit}'")
62 4266 ▶ unless $whole->($all{$limit});
Branch: 1 branch
BranchTrueFalseTotal
unless &$whole($all{$limit}) 3 4263 4266
63 }
64 Carp::croak('Game::Fanorona::Variant: repeat is 0, for never, or 2 and up')
65 1421 ▶ if $all{repeat} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $all{"repeat"} == 1 2 1419 1421
66 Carp::croak('Game::Fanorona::Variant: ply_cap is 1 or more')
67 1419 ▶ if $all{ply_cap} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $all{"ply_cap"} < 1 1 1418 1419
68  
69 1418 my $setup = Game::Fanorona::Engine->new(cols => $cols, rows => $rows)->to_string;
70 1418 ▶ if (defined $all{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $all{'start'}) { } 564 854 1418
71 564 my ($board, $err) = Game::Fanorona::Engine->of_string($all{start});
72 564 ▶ Carp::croak("Game::Fanorona::Variant: '$all{start}' is not a position") unless $board;
Branch: 1 branch
BranchTrueFalseTotal
unless $board 3 561 564
73 561 ▶ Carp::croak("Game::Fanorona::Variant: the start is not a position of a board $cols by $rows")
Branch: 1 branch
BranchTrueFalseTotal
unless $board->cols == $cols and $board->rows == $rows 2 559 561
Condition66%2/3
$board->cols == $cols and $board->rows == $rows
!ll&&!rl&&r
20559
MC/DC50%1/2
$board->cols == $cols and $board->rows == $rows
$board->cols == $cols $board->rows == $rows
Truth table66%2/3
A and B
A= $board->cols == $colsB= $board->rows == $rows
ABresult
0X0
100
111
74 unless $board->cols == $cols && $board->rows == $rows;
75 559 $all{start} = $board->to_string;
76 }
77 else {
78 854 $all{start} = $setup;
79 }
80  
81 1413 ▶ my ($name) = grep { my @size = Game::Fanorona::Engine->size_of($_); $size[0] == $cols && $size[1] == $rows } @NAMES;
Condition100%3/3
$size[0] == $cols && $size[1] == $rows
!ll&&!rl&&r
2897331309
MC/DC100%2/2
$size[0] == $cols && $size[1] == $rows
$size[0] == $cols $size[1] == $rows
Truth table100%3/3
A && B
A= $size[0] == $colsB= $size[1] == $rows
ABresult
0X0
100
111
82 1413 ▶ $name = "${cols}x${rows}" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 104 1309 1413
83  
84 1413 $MAKING = 1;
85 1413 my $made = eval { $class->new(name => $name, map { $_ => $all{$_} } @FIELDS) };
86 1413 my $died = $@;
87 1413 $MAKING = 0;
88 1413 ▶ Carp::croak($died) unless $made;
Branch: 1 branch
BranchTrueFalseTotal
unless $made 0 1413 1413
89 1413 return $made;
90 27 };
91  
92 463 ▶sub named {
Subroutine named: called CC=3 SCAR=11.0
Pod documented
93 463 my ($class, $name) = @_;
94 463 ▶ $name = 'sivy' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 394 69 463
95 463 my ($cols, $rows) = Game::Fanorona::Engine->size_of($name);
96 463 ▶ Carp::croak("Game::Fanorona::Variant: no board is called '$name'") unless $cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols 1 462 463
97 462 return $make->($class, cols => $cols, rows => $rows);
98 }
99  
100 980 ▶sub custom {
Subroutine custom: called CC=9 SCAR=22.0
Pod documented
101 980 my ($class, @fields) = @_;
102 980 ▶ Carp::croak('Game::Fanorona::Variant: custom takes field => value pairs') if @fields % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @fields % 2 1 979 980
103 979 my %given = @fields;
104 979 my %known = map { $_ => 1 } @FIELDS, 'board';
105 979 for my $field (sort keys %given) {
106 4074 ▶ Carp::croak("Game::Fanorona::Variant: no field is called '$field'") unless $known{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$field} 2 4072 4074
107 4072 ▶ Carp::croak("Game::Fanorona::Variant: $field has no value") unless defined $given{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given{$field} 1 4071 4072
108 }
109 976 ▶ if (defined(my $board = delete $given{board})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $board = delete $given{"board"})) 60 916 976
110 Carp::croak('Game::Fanorona::Variant: give a board by name or by cols and rows, not both')
111 60 ▶ if defined $given{cols} || defined $given{rows};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"cols"} or defined $given{"rows"} 1 59 60
Condition66%2/3
defined $given{"cols"} or defined $given{"rows"}
l!l&&r!l&&!r
1059
MC/DC50%1/2
defined $given{"cols"} or defined $given{"rows"}
defined $given{"cols"} defined $given{"rows"}
Truth table66%2/3
A or B
A= defined $given{"cols"}B= defined $given{"rows"}
ABresult
1X1
011
000
112 59 my ($cols, $rows) = Game::Fanorona::Engine->size_of($board);
113 59 ▶ Carp::croak("Game::Fanorona::Variant: no board is called '$board'") unless $cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols 7 52 59
114 52 @given{qw(cols rows)} = ($cols, $rows);
115 }
116 968 return $make->($class, %given);
117 }
118  
119 48 ▶sub from_string {
Subroutine from_string: called CC=8 SCAR=20.8
Pod documented
120 48 my ($class, $string) = @_;
121 48 ▶ return undef unless defined $string && !ref $string && length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and not ref $string and length $string 3 45 48
Condition100%6/6
defined $string and not ref $string and length $string
!ll&&!rl&&r
2145
defined $string and not ref $string
!ll&&!rl&&r
1146
MC/DC100%3/3
defined $string and not ref $string and length $string
defined $string not ref $string length $string
Truth table100%4/4
A and not(B) and C
A= defined $stringB= not ref $stringC= length $string
ABCresult
0XX0
10X0
1100
1111
122 45 my ($board, @pairs) = split /,/, $string, -1;
123 45 my %given;
124 45 ▶ if ($board =~ /\A([0-9])x([0-9])\z/) { @given{qw(cols rows)} = ($1, $2) }
Branch: 1 branch
BranchTrueFalseTotal
if ($board =~ /\A([0-9])x([0-9])\z/u) { } 6 39 45
125 39 else { $given{board} = $board }
126 45 for my $pair (@pairs) {
127 20 ▶ return undef unless $pair =~ /\A(quiet_limit|repeat|ply_cap|start)=(.+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pair =~ /\A(quiet_limit|repeat|ply_cap|start)=(.+)\z/u 3 17 20
128 17 ▶ return undef if exists $given{$1};
Branch: 1 branch
BranchTrueFalseTotal
if exists $given{$1} 1 16 17
129 16 $given{$1} = $2;
130 }
131 41 my $made = eval { $class->custom(%given) };
132 41 return $made;
133 }
134  
135 16 ▶sub as_string {
Subroutine as_string: called CC=4 SCAR=13.9
Pod documented
136 16 my ($self) = @_;
137 16 my $default = $defaults->();
138 16 my $text = $self->name;
139 16 for my $limit (qw(quiet_limit repeat ply_cap)) {
140 48 ▶ $text .= ",$limit=" . $self->$limit unless $self->$limit == $default->{$limit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$limit == $default->{$limit} 7 41 48
141 }
142 16 ▶ $text .= ',start=' . $self->start unless $self->is_setup;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_setup 3 13 16
143 16 return $text;
144 }
145  
146 83 ▶sub is_setup {
Subroutine is_setup: called CC=2 SCAR=6.9
Pod documented
147 83 my ($self) = @_;
148 83 ▶ return $self->start eq Game::Fanorona::Engine->new(cols => $self->cols, rows => $self->rows)->to_string ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->start eq 'Game::Fanorona::Engine'->new('cols', $self->cols, 'rows', $self->rows)->to_string ? : 79 4 83
149 }
150  
151 1542 ▶sub limits {
Subroutine limits: called CC=1 SCAR=0.0
Pod documented
152 1542 my ($self) = @_;
153 1542 return { map { $_ => $self->$_ } qw(quiet_limit repeat ply_cap) };
154 }
155  
156 5 ▶sub names { @NAMES }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
157  
158 27 1;
159  
160 __END__
« blib/lib/Game/Fanorona/Rules.pm ↑ Summary blib/lib/Game/Fanorona/Bot.pm »