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.pm »
1 package Game::Fanorona::Variant;
2  
3 30 ▶use 5.014;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 30 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 30 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 30 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 30 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 30 ▶use Game::Fanorona::Engine ();
Subroutine __ANON__: called CC=1 SCAR=0.0
11 30 ▶use Game::Fanorona::Rules ();
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 30 our $VERSION = '0.03';
14  
15 30 our @NAMES;
16 30 our @FIELDS;
17 30 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 30 @NAMES = qw(sivy dimy telo);
19 30 @FIELDS = qw(cols rows quiet_limit repeat ply_cap start);
20 }
21  
22 30 has name => (is => 'ro');
23  
24 30 has [@FIELDS] => (is => 'ro');
25  
26 30 my $MAKING = 0;
27  
28 30 my %DEFAULT;
29  
30 1982 ▶my $defaults = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
31 1982 ▶ return \%DEFAULT if %DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if %DEFAULT 1962 20 1982
32 20 my $game = Game::Fanorona::Rules->new(board => 'telo');
33 20 %DEFAULT = (
34 quiet_limit => $game->quiet_limit,
35 repeat => $game->repeat,
36 ply_cap => $game->ply_cap,
37 );
38 20 return \%DEFAULT;
39 30 };
40  
41 1956 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
42 1956 my ($self) = @_;
43 1956 ▶ Carp::croak('Game::Fanorona::Variant: a variant is made by named, custom or from_string')
Branch: 1 branch
BranchTrueFalseTotal
unless $MAKING 1 1955 1956
44 unless $MAKING;
45 1955 return;
46 }
47  
48 9831 ▶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 9831 0 9831
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
009831
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 1972 ▶my $make = sub {
Subroutine __ANON__: called CC=18 SCAR=28.9
51 1972 my ($class, %all) = @_;
52 1972 my ($cols, $rows) = @all{qw(cols rows)};
53 1972 ▶ Carp::croak('Game::Fanorona::Variant: a board is cols and rows')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cols and defined $rows 2 1970 1972
Condition100%3/3
defined $cols and defined $rows
!ll&&!rl&&r
111970
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 1970 ▶ 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 1966 1970
Condition83%5/6
&$whole($cols) and &$whole($rows) and "Game::Fanorona::Engine"->size_ok($cols, $rows)
!ll&&!rl&&r
131966
&$whole($cols) and &$whole($rows)
!ll&&!rl&&r
101969
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 1966 my $default = $defaults->();
59 1966 for my $limit (qw(quiet_limit repeat ply_cap)) {
60 5892 ▶ $all{$limit} = $default->{$limit} unless defined $all{$limit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $all{$limit} 4235 1657 5892
61 Carp::croak("Game::Fanorona::Variant: $limit is a whole number, not '$all{$limit}'")
62 5892 ▶ unless $whole->($all{$limit});
Branch: 1 branch
BranchTrueFalseTotal
unless &$whole($all{$limit}) 3 5889 5892
63 }
64 Carp::croak('Game::Fanorona::Variant: repeat is 0, for never, or 2 and up')
65 1963 ▶ if $all{repeat} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $all{"repeat"} == 1 2 1961 1963
66 Carp::croak('Game::Fanorona::Variant: ply_cap is 1 or more')
67 1961 ▶ if $all{ply_cap} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $all{"ply_cap"} < 1 1 1960 1961
68  
69 1960 my $setup = Game::Fanorona::Engine->new(cols => $cols, rows => $rows)->to_string;
70 1960 ▶ if (defined $all{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $all{'start'}) { } 581 1379 1960
71 581 my ($board, $err) = Game::Fanorona::Engine->of_string($all{start});
72 581 ▶ Carp::croak("Game::Fanorona::Variant: '$all{start}' is not a position") unless $board;
Branch: 1 branch
BranchTrueFalseTotal
unless $board 3 578 581
73 578 ▶ 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 576 578
Condition66%2/3
$board->cols == $cols and $board->rows == $rows
!ll&&!rl&&r
20576
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 576 $all{start} = $board->to_string;
76 }
77 else {
78 1379 $all{start} = $setup;
79 }
80  
81 1955 ▶ 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
3981331851
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 1955 ▶ $name = "${cols}x${rows}" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 104 1851 1955
83  
84 1955 $MAKING = 1;
85 1955 my $made = eval { $class->new(name => $name, map { $_ => $all{$_} } @FIELDS) };
86 1955 my $died = $@;
87 1955 $MAKING = 0;
88 1955 ▶ Carp::croak($died) unless $made;
Branch: 1 branch
BranchTrueFalseTotal
unless $made 0 1955 1955
89 1955 return $made;
90 30 };
91  
92 952 ▶sub named {
Subroutine named: called CC=3 SCAR=11.0
Pod documented
93 952 my ($class, $name) = @_;
94 952 ▶ $name = 'sivy' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 417 535 952
95 952 my ($cols, $rows) = Game::Fanorona::Engine->size_of($name);
96 952 ▶ Carp::croak("Game::Fanorona::Variant: no board is called '$name'") unless $cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols 1 951 952
97 951 return $make->($class, cols => $cols, rows => $rows);
98 }
99  
100 1033 ▶sub custom {
Subroutine custom: called CC=9 SCAR=22.0
Pod documented
101 1033 my ($class, @fields) = @_;
102 1033 ▶ Carp::croak('Game::Fanorona::Variant: custom takes field => value pairs') if @fields % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @fields % 2 1 1032 1033
103 1032 my %given = @fields;
104 1032 my %known = map { $_ => 1 } @FIELDS, 'board';
105 1032 for my $field (sort keys %given) {
106 4238 ▶ Carp::croak("Game::Fanorona::Variant: no field is called '$field'") unless $known{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$field} 2 4236 4238
107 4236 ▶ Carp::croak("Game::Fanorona::Variant: $field has no value") unless defined $given{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given{$field} 1 4235 4236
108 }
109 1029 ▶ if (defined(my $board = delete $given{board})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $board = delete $given{"board"})) 61 968 1029
110 Carp::croak('Game::Fanorona::Variant: give a board by name or by cols and rows, not both')
111 61 ▶ if defined $given{cols} || defined $given{rows};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given{"cols"} or defined $given{"rows"} 1 60 61
Condition66%2/3
defined $given{"cols"} or defined $given{"rows"}
l!l&&r!l&&!r
1060
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 60 my ($cols, $rows) = Game::Fanorona::Engine->size_of($board);
113 60 ▶ Carp::croak("Game::Fanorona::Variant: no board is called '$board'") unless $cols;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols 7 53 60
114 53 @given{qw(cols rows)} = ($cols, $rows);
115 }
116 1021 return $make->($class, %given);
117 }
118  
119 49 ▶sub from_string {
Subroutine from_string: called CC=8 SCAR=20.8
Pod documented
120 49 my ($class, $string) = @_;
121 49 ▶ return undef unless defined $string && !ref $string && length $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string and not ref $string and length $string 3 46 49
Condition100%6/6
defined $string and not ref $string and length $string
!ll&&!rl&&r
2146
defined $string and not ref $string
!ll&&!rl&&r
1147
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 46 my ($board, @pairs) = split /,/, $string, -1;
123 46 my %given;
124 46 ▶ 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 40 46
125 40 else { $given{board} = $board }
126 46 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 42 my $made = eval { $class->custom(%given) };
132 42 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 549 ▶sub is_setup {
Subroutine is_setup: called CC=2 SCAR=6.9
Pod documented
147 549 my ($self) = @_;
148 549 ▶ 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 ? : 400 149 549
149 }
150  
151 2108 ▶sub limits {
Subroutine limits: called CC=1 SCAR=0.0
Pod documented
152 2108 my ($self) = @_;
153 2108 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 30 1;
159  
160 __END__
« blib/lib/Game/Fanorona/Rules.pm ↑ Summary blib/lib/Game/Fanorona.pm »