blib/lib/Game/Merrills/Board.pm

filter: Statementstmt 100% 76 / 76 Branchbran 100% 28 / 28 Conditioncond 94.4% 17 / 18 MC/DCmcdc 90.9% 10 / 11 Subroutinesub 100% 21 / 21 Podpod 91.6% 11 / 12 totaltotal 98.1% 163 / 166 CC 24 SCAR 17.3
CC
24
Cov
99%
BUILD
13.0
in_mill
6.0
_value
4.0
CRAP 5.6
« blib/lib/Game/Merrills.pm ↑ Summary blib/lib/Game/Merrills/Move.pm »
1 package Game::Merrills::Board;
2  
3 85 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 85 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 85 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
7 85 ▶use Game::Merrills::Points;
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 85 our $VERSION = '0.02';
10  
11 85 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
12 85 EMPTY => 0,
13 WHITE => 1,
14 BLACK => -1,
15 MEN => 9,
16 85 };
17  
18 85 our (%VALUE, %SIDE);
19  
20 85 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 85 %VALUE = (white => WHITE, black => BLACK);
22 85 %SIDE = (WHITE, 'white', BLACK, 'black');
23 }
24  
25 has cells => (
26 is => 'rw',
27 isa => ArrayRef,
28 475 ▶ default => sub { opening() }
Subroutine __ANON__: called CC=1 SCAR=0.0
29 85 );
30  
31 has hand => (
32 is => 'rw',
33 isa => HashRef,
34 282 ▶ default => sub { { white => MEN, black => MEN } }
Subroutine __ANON__: called CC=1 SCAR=0.0
35 85 );
36  
37 5525 ▶sub BUILD {
Subroutine BUILD: called CC=13 SCAR=25.7
Pod undocumented
38 5525 my ($self) = @_;
39 5525 my $cells = $self->cells;
40 die 'cells must be an arrayref of 24 values'
41 5525 ▶ unless ref $cells eq 'ARRAY' && @{$cells} == Game::Merrills::Points::POINTS;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cells eq "ARRAY" and @{$cells;} == 24 1 5524 5525
Condition66%2/3
ref $cells eq "ARRAY" and @{$cells;} == 24
!ll&&!rl&&r
015524
MC/DC50%1/2
ref $cells eq "ARRAY" and @{$cells;} == 24
ref $cells eq "ARRAY" @{$cells;} == 24
Truth table66%2/3
A and B
A= ref $cells eq "ARRAY"B= @{$cells;} == 24
ABresult
0X0
100
111
42 5524 for my $point (Game::Merrills::Points::all_points()) {
43 132576 my $value = $cells->[$point];
44 132576 ▶ die 'cell ' . Game::Merrills::Points::name($point) . ' must be -1, 0 or 1, got '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value and $value =~ /^(?:-1|0|1)$/ 2 132574 132576
defined $value ? : 1 1 2
Condition100%3/3
defined $value and $value =~ /^(?:-1|0|1)$/
!ll&&!rl&&r
11132574
MC/DC100%2/2
defined $value and $value =~ /^(?:-1|0|1)$/
defined $value $value =~ /^(?:-1|0|1)$/
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /^(?:-1|0|1)$/
ABresult
0X0
100
111
45 . (defined $value ? "'$value'" : 'undef')
46 unless defined $value && $value =~ m/^(?:-1|0|1)$/;
47 132574 $cells->[$point] = $value + 0;
48 }
49 5522 my $hand = $self->hand;
50 5522 for my $side (qw/white black/) {
51 11042 my $held = $hand->{$side};
52 11042 ▶ die "hand of $side must be 0 .. 9, got " . (defined $held ? "'$held'" : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless defined $held and $held =~ /^[0-9]$/ 2 11040 11042
defined $held ? : 1 1 2
Condition100%3/3
defined $held and $held =~ /^[0-9]$/
!ll&&!rl&&r
1111040
MC/DC100%2/2
defined $held and $held =~ /^[0-9]$/
defined $held $held =~ /^[0-9]$/
Truth table100%3/3
A and B
A= defined $heldB= $held =~ /^[0-9]$/
ABresult
0X0
100
111
53 unless defined $held && $held =~ m/^[0-9]$/;
54 11040 $hand->{$side} = $held + 0;
55 11040 ▶ die "$side has more than nine men"
Branch: 1 branch
BranchTrueFalseTotal
if $self->count($side) + $held > 9 1 11039 11040
56 if $self->count($side) + $held > MEN;
57 }
58 die 'hand must hold white and black and nothing else'
59 5519 ▶ unless keys %{$hand} == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$hand;} == 2 1 5518 5519
60 5518 return $self;
61 }
62  
63 18689 ▶sub _value {
Subroutine _value: called CC=4 SCAR=13.9
64 18689 my ($side) = @_;
65 die 'side must be white or black, got ' . (defined $side ? "'$side'" : 'undef')
66 18689 ▶ unless defined $side && exists $VALUE{$side};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $side and exists $Game::Merrills::Board::VALUE{$side} 9 18680 18689
defined $side ? : 5 4 9
Condition100%3/3
defined $side and exists $Game::Merrills::Board::VALUE{$side}
!ll&&!rl&&r
4518680
MC/DC100%2/2
defined $side and exists $Game::Merrills::Board::VALUE{$side}
defined $side exists $Game::Merrills::Board::VALUE{$side}
Truth table100%3/3
A and B
A= defined $sideB= exists $Game::Merrills::Board::VALUE{$side}
ABresult
0X0
100
111
67 18680 return $VALUE{$side};
68 }
69  
70 481 ▶sub opening {
Subroutine opening: called CC=1 SCAR=0.0
Pod documented
71 481 return [ (EMPTY) x Game::Merrills::Points::POINTS ];
72 }
73  
74 91232 ▶sub at {
Subroutine at: called CC=1 SCAR=0.0
Pod documented
75 91232 return $_[0]->cells->[ Game::Merrills::Points::_check($_[1]) ];
76 }
77  
78 81081 ▶sub side_at {
Subroutine side_at: called CC=2 SCAR=6.9
Pod documented
79 81081 ▶ my $value = $_[0]->at($_[1]) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $_[0]->at($_[1]) or return undef 26268 54812 81080
80 26268 return $SIDE{$value};
81 }
82  
83 685 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
84 685 my ($self, $point, $side) = @_;
85 685 Game::Merrills::Points::_check($point);
86 684 ▶ $self->cells->[$point] = defined $side ? _value($side) : EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
defined $side ? : 643 41 684
87 683 return $self;
88 }
89  
90 5310 ▶sub empty {
Subroutine empty: called CC=2 SCAR=6.9
Pod documented
91 5310 ▶ return $_[0]->at($_[1]) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->at($_[1]) ? : 3047 2262 5309
92 }
93  
94 11360 ▶sub points_of {
Subroutine points_of: called CC=1 SCAR=0.0
Pod documented
95 11360 my ($self, $side) = @_;
96 11360 my $value = _value($side);
97 11354 my $cells = $self->cells;
98 11354 return grep { $cells->[$_] == $value } Game::Merrills::Points::all_points();
99 }
100  
101 11053 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
102 11053 my ($self, $side) = @_;
103 11053 return scalar(() = $self->points_of($side));
104 }
105  
106 6686 ▶sub in_hand {
Subroutine in_hand: called CC=1 SCAR=0.0
Pod documented
107 6686 my ($self, $side) = @_;
108 6686 _value($side);
109 6684 return $self->hand->{$side};
110 }
111  
112 7 ▶sub men {
Subroutine men: called CC=1 SCAR=0.0
Pod documented
113 7 my ($self, $side) = @_;
114 7 return $self->count($side) + $self->in_hand($side);
115 }
116  
117 4837 ▶sub in_mill {
Subroutine in_mill: called CC=6 SCAR=17.9
Pod documented
118 4837 my ($self, $point) = @_;
119 4837 ▶ my $value = $self->at($point) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $self->at($point) or return 0 4787 49 4836
120 4787 my $cells = $self->cells;
121 4787 for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$point] }) {
122 9081 ▶ return 1 if $cells->[ $mill->[0] ] == $value
Branch: 1 branch
BranchTrueFalseTotal
if $$cells[$mill->[0]] == $value and $$cells[$mill->[1]] == $value and $$cells[$mill->[2]] == $value 798 8283 9081
Condition100%6/6
$$cells[$mill->[0]] == $value and $$cells[$mill->[1]] == $value and $$cells[$mill->[2]] == $value
!ll&&!rl&&r
68731410798
$$cells[$mill->[0]] == $value and $$cells[$mill->[1]] == $value
!ll&&!rl&&r
439224812208
MC/DC100%3/3
$$cells[$mill->[0]] == $value and $$cells[$mill->[1]] == $value and $$cells[$mill->[2]] == $value
$$cells[$mill->[0]] == $value $$cells[$mill->[1]] == $value $$cells[$mill->[2]] == $value
Truth table100%4/4
A and B and C
A= $$cells[$mill->[0]] == $valueB= $$cells[$mill->[1]] == $valueC= $$cells[$mill->[2]] == $value
ABCresult
0XX0
10X0
1100
1111
123 && $cells->[ $mill->[1] ] == $value
124 && $cells->[ $mill->[2] ] == $value;
125 }
126 3989 return 0;
127 }
128  
129 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
130 1 my ($self) = @_;
131 return ref($self)->new(
132 1 cells => [ @{ $self->cells } ],
133 1 hand => { %{ $self->hand } }
134 );
135 }
136  
137 85 1;
138  
139 __END__
« blib/lib/Game/Merrills.pm ↑ Summary blib/lib/Game/Merrills/Move.pm »