blib/lib/Game/Senet/Variant.pm

filter: Statementstmt 100% 81 / 81 Branchbran 92.8% 26 / 28 Conditioncond 54.8% 17 / 31 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 100% 19 / 19 Podpod 90.9% 10 / 11 totaltotal 84.4% 157 / 186 CC 31 SCAR 15.0
CC
31
Cov
89%
named
7.5
BUILD
6.7
of
6.1
CRAP 4.5
« blib/lib/Game/Senet/Result.pm ↑ Summary blib/lib/Game/Senet/Bot.pm »
1 package Game::Senet::Variant;
2  
3 83 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 83 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 83 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 83 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 83 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 83 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 83 ▶use Game::Senet::Engine ();
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 83 our $VERSION = '0.03';
14  
15 83 my (@FIELDS, @IDENTITY, %NAMED, %ALLOWED);
16 83 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 83 @FIELDS = qw(exits pieces blockade protect backward reach26 throw_on_26);
18 83 @IDENTITY = grep { $_ ne 'pieces' } @FIELDS;
19 83 %NAMED = (
20 kendall => { exits => 'kendall', pieces => 5, blockade => 3, protect => 'run',
21 backward => 1, reach26 => 'exact', throw_on_26 => 0 },
22 skyruk => { exits => 'skyruk', pieces => 5, blockade => 2, protect => 'ends',
23 backward => 1, reach26 => 'any', throw_on_26 => 1 },
24 );
25 83 %ALLOWED = (
26 exits => qr/\A(?:kendall|skyruk)\z/,
27 pieces => qr/\A[57]\z/,
28 blockade => qr/\A[023]\z/,
29 protect => qr/\A(?:run|ends|none)\z/,
30 backward => qr/\A[01]\z/,
31 reach26 => qr/\A(?:exact|any)\z/,
32 throw_on_26 => qr/\A[01]\z/,
33 );
34 }
35  
36 83 has exits => (is => 'ro', default => 'kendall');
37  
38 83 has pieces => (is => 'ro', default => 5);
39  
40 83 has blockade => (is => 'ro', default => 3);
41  
42 83 has protect => (is => 'ro', default => 'run');
43  
44 83 has backward => (is => 'ro', default => 1);
45  
46 83 has reach26 => (is => 'ro', default => 'exact');
47  
48 83 has throw_on_26 => (is => 'ro', default => 0);
49  
50 7306 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=19.0
Pod undocumented
51 7306 my ($self) = @_;
52 7306 for my $field (@FIELDS) {
53 51112 my $value = $self->$field;
54 Carp::croak("Game::Senet::Variant: $field may not be '" . ($value // 'undef') . "'")
55 51112 ▶ unless defined $value && !ref $value && $value =~ $ALLOWED{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /$ALLOWED{$field}/ 9 51103 51112
Condition50%4/8
defined $value and not ref $value and $value =~ /$ALLOWED{$field}/
!ll&&!rl&&r
0951103
defined $value and not ref $value
!ll&&!rl&&r
0051112
$value // "undef"
l!l
90
MC/DC33%1/3
defined $value and not ref $value and $value =~ /$ALLOWED{$field}/
defined $value not ref $value $value =~ /$ALLOWED{$field}/
MC/DC0%0/1
$value // "undef"
$value
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /$ALLOWED{$field}/
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $value
Aresult
11
00
56 }
57 7297 return;
58 }
59  
60 7223 ▶sub fields { @FIELDS }
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
61  
62 15531 ▶sub names { sort keys %NAMED }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
63  
64 1639 ▶sub named {
Subroutine named: called CC=7 SCAR=20.2
Pod documented
65 1639 my ($class, $name, %override) = @_;
66 Carp::croak("Game::Senet::Variant: no rule set is called '" . ($name // 'undef')
67 . "'; the names are " . join(', ', $class->names))
68 1639 ▶ unless defined $name && !ref $name && exists $NAMED{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and exists $NAMED{$name} 2 1637 1639
Condition50%4/8
defined $name and not ref $name and exists $NAMED{$name}
!ll&&!rl&&r
021637
defined $name and not ref $name
!ll&&!rl&&r
001639
$name // "undef"
l!l
20
MC/DC33%1/3
defined $name and not ref $name and exists $NAMED{$name}
defined $name not ref $name exists $NAMED{$name}
MC/DC0%0/1
$name // "undef"
$name
Truth table50%2/4
A and not(B) and C
A= defined $nameB= not ref $nameC= exists $NAMED{$name}
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $name
Aresult
11
00
69 1637 for my $key (sort keys %override) {
70 144 ▶ Carp::croak("Game::Senet::Variant: a named set takes pieces and nothing else, not '$key'")
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq "pieces" 1 143 144
71 unless $key eq 'pieces';
72 }
73 1636 return $class->new(%{ $NAMED{$name} }, %override);
74 }
75  
76 5671 ▶sub custom {
Subroutine custom: called CC=3 SCAR=11.0
Pod documented
77 5671 my ($class, %given) = @_;
78 5671 for my $key (sort keys %given) {
79 39615 ▶ Carp::croak("Game::Senet::Variant: no rule is called '$key'") unless exists $ALLOWED{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ALLOWED{$key} 1 39614 39615
80 }
81 5670 return $class->new(%{ $NAMED{kendall} }, %given);
82 }
83  
84 90067 ▶sub of {
Subroutine of: called CC=6 SCAR=18.0
Pod documented
85 90067 my ($class, $rules) = @_;
86 90067 ▶ return $class->named('kendall') unless defined $rules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rules 104 89963 90067
87 89963 ▶ return $rules if Scalar::Util::blessed($rules) && $rules->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($rules) and $rules->isa($class) 88579 1384 89963
Condition66%2/3
builtin::blessed($rules) and $rules->isa($class)
!ll&&!rl&&r
1384088579
MC/DC50%1/2
builtin::blessed($rules) and $rules->isa($class)
builtin::blessed($rules) $rules->isa($class)
Truth table66%2/3
A and B
A= builtin::blessed($rules)B= $rules->isa($class)
ABresult
0X0
100
111
88 1384 ▶ return $class->custom(%$rules) if ref $rules eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules eq "HASH" 1 1383 1384
89 1383 ▶ Carp::croak('Game::Senet::Variant: a rule set is a name, a hash reference or a variant') if ref $rules;
Branch: 1 branch
BranchTrueFalseTotal
if ref $rules 0 1383 1383
90 1383 return $class->named($rules);
91 }
92  
93 167902 ▶sub as_hash {
Subroutine as_hash: called CC=1 SCAR=0.0
Pod documented
94 167902 my ($self) = @_;
95 167902 return { map { $_ => $self->$_ } @FIELDS };
96 }
97  
98 2 ▶sub equals {
Subroutine equals: called CC=5 SCAR=16.8
Pod documented
99 2 my ($self, $other) = @_;
100 2 ▶ return 0 unless Scalar::Util::blessed($other) && $other->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa(__PACKAGE__) 0 2 2
Condition33%1/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
101 2 for my $field (@FIELDS) {
102 9 ▶ return 0 unless $self->$field eq $other->$field;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$field eq $other->$field 1 8 9
103 }
104 1 return 1;
105 }
106  
107 9855 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
108 9855 my ($self) = @_;
109 9855 for my $name ($self->names) {
110 15134 ▶ return $name unless grep { $self->$_ ne $NAMED{$name}{$_} } @IDENTITY;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->$_ ne $NAMED{$name}{$_};} @IDENTITY 9849 5285 15134
111 }
112 6 return undef;
113 }
114  
115 9843 ▶sub describe {
Subroutine describe: called CC=3 SCAR=11.0
Pod documented
116 9843 my ($self) = @_;
117 9843 my $name = $self->name;
118 9843 ▶ return join ' ', map { $_ . '=' . $self->$_ } @FIELDS unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 3 9840 9843
119 9840 ▶ return $self->pieces == 5 ? $name : "$name pieces=" . $self->pieces;
Branch: 1 branch
BranchTrueFalseTotal
$self->pieces == 5 ? : 7270 2570 9840
120 }
121  
122 53 ▶sub exit_for {
Subroutine exit_for: called CC=5 SCAR=16.5
Pod documented
123 53 my ($self, $house, $roll) = @_;
124 53 ▶ Carp::croak('Game::Senet::Variant: an exit is asked of a house from 26 to 30 and a roll from 1 to 5')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll and $roll =~ /\A[1-5]\z/ 1 52 53
Condition66%6/9
defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll and $roll =~ /\A[1-5]\z/
!ll&&!rl&&r
1052
defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll
!ll&&!rl&&r
1052
defined $house and $house =~ /\A(?:2[6-9]|30)\z/
!ll&&!rl&&r
0152
MC/DC25%1/4
defined $house and $house =~ /\A(?:2[6-9]|30)\z/ and defined $roll and $roll =~ /\A[1-5]\z/
defined $house $house =~ /\A(?:2[6-9]|30)\z/ defined $roll $roll =~ /\A[1-5]\z/
Truth table40%2/5
A and B and C and D
A= defined $houseB= $house =~ /\A(?:2[6-9]|30)\z/C= defined $rollD= $roll =~ /\A[1-5]\z/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
125 unless defined $house && $house =~ /\A(?:2[6-9]|30)\z/ && defined $roll && $roll =~ /\A[1-5]\z/;
126 52 return [ @{ Game::Senet::Engine->rules($self->as_hash)->{exits}{$house}{$roll} } ];
127 }
128  
129 83 1;
130  
131 __END__
« blib/lib/Game/Senet/Result.pm ↑ Summary blib/lib/Game/Senet/Bot.pm »