blib/lib/Game/Oware/Board.pm

filter: Statementstmt 97.4% 76 / 78 Branchbran 92.3% 24 / 26 Conditioncond 80.0% 12 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 94.7% 18 / 19 Podpod 100% 11 / 11 totaltotal 91.7% 145 / 158 CC 21 SCAR 11.0
CC
21
Cov
94%
capture_chain
5.0
store_of
4.1
houses_of
4.1
CRAP 3.0
« blib/lib/Game/Oware/Rules.pm ↑ Summary blib/lib/Game/Oware/Error.pm »
1 package Game::Oware::Board;
2  
3 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 45 our $VERSION = '0.05';
7  
8 45 ▶use constant HOUSES => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 45 ▶use constant CELLS => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use constant SEEDS => 48;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 45 ▶use constant PER_HOUSE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use constant P1_STORE => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 45 ▶use constant P2_STORE => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 700 ▶sub opening { return [ (PER_HOUSE) x HOUSES, 0, 0 ] }
Subroutine opening: called CC=1 SCAR=0.0
Pod documented
16  
17 0 ▶sub clone { my ($class, $board) = @_; return [ @$board ] }
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
18  
19 122 ▶sub total {
Subroutine total: called CC=2 SCAR=6.9
Pod documented
20 122 my ($class, $board) = @_;
21 122 my $total = 0;
22 122 $total += $_ for @$board;
23 122 return $total;
24 }
25  
26 1890172 ▶sub other { my ($class, $seat) = @_; return $seat eq 'p1' ? 'p2' : 'p1' }
Subroutine other: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$seat eq 'p1' ? : 947343 942829 1890172
27  
28 525419 ▶sub store_of {
Subroutine store_of: called CC=4 SCAR=14.2
Pod documented
29 525419 my ($class, $seat) = @_;
30 525419 ▶ die "Game::Oware::Board: there is no seat '$seat'"
Branch: 1 branch
BranchTrueFalseTotal
unless $seat eq "p1" or $seat eq "p2" 0 525419 525419
Condition66%2/3
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
2595472658720
MC/DC0%0/2
$seat eq "p1" or $seat eq "p2"
$seat eq "p1" $seat eq "p2"
Truth table66%2/3
A or B
A= $seat eq "p1"B= $seat eq "p2"
ABresult
1X1
011
000
31 unless $seat eq 'p1' || $seat eq 'p2';
32 525419 ▶ return $seat eq 'p1' ? P1_STORE : P2_STORE;
Branch: 1 branch
BranchTrueFalseTotal
$seat eq 'p1' ? : 259547 265872 525419
33 }
34  
35 1227374 ▶sub owner_of {
Subroutine owner_of: called CC=2 SCAR=6.9
Pod documented
36 1227374 my ($class, $house) = @_;
37 1227374 $class->assert_house($house);
38 1227372 ▶ return $house < 6 ? 'p1' : 'p2';
Branch: 1 branch
BranchTrueFalseTotal
$house < 6 ? : 611842 615530 1227372
39 }
40  
41 1483819 ▶sub houses_of {
Subroutine houses_of: called CC=4 SCAR=14.2
Pod documented
42 1483819 my ($class, $seat) = @_;
43 1483819 ▶ die "Game::Oware::Board: there is no seat '$seat'"
Branch: 1 branch
BranchTrueFalseTotal
unless $seat eq "p1" or $seat eq "p2" 0 1483819 1483819
Condition66%2/3
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
7442597395600
MC/DC0%0/2
$seat eq "p1" or $seat eq "p2"
$seat eq "p1" $seat eq "p2"
Truth table66%2/3
A or B
A= $seat eq "p1"B= $seat eq "p2"
ABresult
1X1
011
000
44 unless $seat eq 'p1' || $seat eq 'p2';
45 1483819 ▶ return $seat eq 'p1' ? (0 .. 5) : (6 .. 11);
Branch: 1 branch
BranchTrueFalseTotal
$seat eq 'p1' ? : 744259 739560 1483819
46 }
47  
48 784541 ▶sub seeds_on_side {
Subroutine seeds_on_side: called CC=2 SCAR=6.9
Pod documented
49 784541 my ($class, $board, $seat) = @_;
50 784541 my $seeds = 0;
51 784541 $seeds += $board->[$_] for $class->houses_of($seat);
52 784541 return $seeds;
53 }
54  
55 3360561 ▶sub assert_house {
Subroutine assert_house: called CC=4 SCAR=13.9
Pod documented
56 3360561 my ($class, $house) = @_;
57 3360561 ▶ die 'Game::Oware::Board: a house is 0 to 11'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $house and $house =~ /\A\d+\z/ and $house < 12 8 3360553 3360561
Condition83%5/6
defined $house and $house =~ /\A\d+\z/ and $house < 12
!ll&&!rl&&r
263360553
defined $house and $house =~ /\A\d+\z/
!ll&&!rl&&r
023360559
MC/DC66%2/3
defined $house and $house =~ /\A\d+\z/ and $house < 12
defined $house $house =~ /\A\d+\z/ $house < 12
Truth table75%3/4
A and B and C
A= defined $houseB= $house =~ /\A\d+\z/C= $house < 12
ABCresult
0XX0
10X0
1100
1111
58 unless defined $house && $house =~ /\A\d+\z/ && $house < HOUSES;
59 3360553 return $house;
60 }
61  
62 525510 ▶sub sow {
Subroutine sow: called CC=4 SCAR=13.9
Pod documented
63 525510 my ($class, $board, $house) = @_;
64 525510 $class->assert_house($house);
65  
66 525508 my $hand = $board->[$house];
67 525508 ▶ die "Game::Oware::Board: house $house is empty, so there is nothing to sow"
Branch: 1 branch
BranchTrueFalseTotal
unless $hand 1 525507 525508
68 unless $hand;
69  
70 525507 my $next = [ @$board ];
71 525507 $next->[$house] = 0;
72  
73 525507 my $i = $house;
74 525507 my $last = $house;
75 525507 ▶ while ($hand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hand) 2038512 525507 2564019
76 2038512 $i = ($i + 1) % HOUSES;
77 2038512 ▶ next if $i == $house;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $house 4881 2033631 2038512
78 2033631 $next->[$i]++;
79 2033631 $last = $i;
80 2033631 $hand--;
81 }
82  
83 525507 return ($next, $last, $board->[$house]);
84 }
85  
86 525508 ▶sub capture_chain {
Subroutine capture_chain: called CC=5 SCAR=16.1
Pod documented
87 525508 my ($class, $board, $last, $sown, $seat) = @_;
88 525508 $class->assert_house($last);
89  
90 525507 my $foe = $class->other($seat);
91  
92 525507 my @chain;
93 525507 my $i = $last;
94 525507 ▶ while (@chain < $sown) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chain < $sown) 606320 7572 613892
95 606320 ▶ last unless $class->owner_of($i) eq $foe;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->owner_of($i) eq $foe 260863 345457 606320
96  
97 345457 my $count = $board->[$i];
98 345457 ▶ last unless $count == 2 || $count == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $count == 2 or $count == 3 257072 88385 345457
Condition100%3/3
$count == 2 or $count == 3
l!l&&r!l&&!r
5718931196257072
MC/DC100%2/2
$count == 2 or $count == 3
$count == 2 $count == 3
Truth table100%3/3
A or B
A= $count == 2B= $count == 3
ABresult
1X1
011
000
99  
100 88385 push @chain, $i;
101 88385 $i = ($i - 1) % HOUSES;
102 }
103  
104 525507 return @chain;
105 }
106  
107 45 1;
108  
109 __END__
« blib/lib/Game/Oware/Rules.pm ↑ Summary blib/lib/Game/Oware/Error.pm »