Merrills/Board.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 172 CC 13 SCAR 30.8
CC
13
Cov
0%
BUILD
72.0
_value
12.0
set
6.0
CRAP 21.8
1 package Game::Merrills::Board;
2  
3 use strict;
4 use warnings;
5  
6 use Object::Proto::Sugar -types;
7 use Game::Merrills::Points;
8  
9 our $VERSION = '0.03';
10  
11 use constant {
12 EMPTY => 0,
13 WHITE => 1,
14 BLACK => -1,
15 MEN => 9,
16 };
17  
18 our (%VALUE, %SIDE);
19  
20 BEGIN {
21 %VALUE = (white => WHITE, black => BLACK);
22 %SIDE = (WHITE, 'white', BLACK, 'black');
23 }
24  
25 has cells => (
26 is => 'rw',
27 isa => ArrayRef,
28 default => sub { opening() }
29 );
30  
31 has hand => (
32 is => 'rw',
33 isa => HashRef,
34 default => sub { { white => MEN, black => MEN } }
35 );
36  
37 sub BUILD {
38 my ($self) = @_;
39 my $cells = $self->cells;
40 die 'cells must be an arrayref of 24 values'
41 unless ref $cells eq 'ARRAY' && @{$cells} == Game::Merrills::Points::POINTS;
42 for my $point (Game::Merrills::Points::all_points()) {
43 my $value = $cells->[$point];
44 die 'cell ' . Game::Merrills::Points::name($point) . ' must be -1, 0 or 1, got '
45 . (defined $value ? "'$value'" : 'undef')
46 unless defined $value && $value =~ m/^(?:-1|0|1)$/;
47 $cells->[$point] = $value + 0;
48 }
49 my $hand = $self->hand;
50 for my $side (qw/white black/) {
51 my $held = $hand->{$side};
52 die "hand of $side must be 0 .. 9, got " . (defined $held ? "'$held'" : 'undef')
53 unless defined $held && $held =~ m/^[0-9]$/;
54 $hand->{$side} = $held + 0;
55 die "$side has more than nine men"
56 if $self->count($side) + $held > MEN;
57 }
58 die 'hand must hold white and black and nothing else'
59 unless keys %{$hand} == 2;
60 return $self;
61 }
62  
63 sub _value {
64 my ($side) = @_;
65 die 'side must be white or black, got ' . (defined $side ? "'$side'" : 'undef')
66 unless defined $side && exists $VALUE{$side};
67 return $VALUE{$side};
68 }
69  
70 sub opening {
71 return [ (EMPTY) x Game::Merrills::Points::POINTS ];
72 }
73  
74 sub at {
75 return $_[0]->cells->[ Game::Merrills::Points::_check($_[1]) ];
76 }
77  
78 sub side_at {
79 my $value = $_[0]->at($_[1]) or return undef;
80 return $SIDE{$value};
81 }
82  
83 sub set {
84 my ($self, $point, $side) = @_;
85 Game::Merrills::Points::_check($point);
86 $self->cells->[$point] = defined $side ? _value($side) : EMPTY;
87 return $self;
88 }
89  
90 sub empty {
91 return $_[0]->at($_[1]) ? 0 : 1;
92 }
93  
94 sub points_of {
95 my ($self, $side) = @_;
96 my $value = _value($side);
97 my $cells = $self->cells;
98 return grep { $cells->[$_] == $value } Game::Merrills::Points::all_points();
99 }
100  
101 sub count {
102 my ($self, $side) = @_;
103 return scalar(() = $self->points_of($side));
104 }
105  
106 sub in_hand {
107 my ($self, $side) = @_;
108 _value($side);
109 return $self->hand->{$side};
110 }
111  
112 sub men {
113 my ($self, $side) = @_;
114 return $self->count($side) + $self->in_hand($side);
115 }
116  
117 sub in_mill {
118 my ($self, $point) = @_;
119 my $value = $self->at($point) or return 0;
120 my $cells = $self->cells;
121 for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$point] }) {
122 return 1 if $cells->[ $mill->[0] ] == $value
123 && $cells->[ $mill->[1] ] == $value
124 && $cells->[ $mill->[2] ] == $value;
125 }
126 return 0;
127 }
128  
129 sub clone {
130 my ($self) = @_;
131 return ref($self)->new(
132 cells => [ @{ $self->cells } ],
133 hand => { %{ $self->hand } }
134 );
135 }
136  
137 1;
138  
139 __END__