Reversi/Board.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 87.5% 28 / 32 Conditioncond 65.9% 31 / 47 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 93.7% 15 / 16 Podpod 100% 12 / 12 totaltotal 82.6% 172 / 208 CC 37 SCAR 23.2
CC
37
Cov
86%
flips_for
20.3
name_of
7.1
square_of
7.0
CRAP 10.2
« Reversi/Move.pm ↑ Summary Reversi/Result.pm »
1 package Game::Reversi::Board;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 our $VERSION = '0.04';
7  
8 19 ▶use constant SIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use constant CELLS => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 my @RAY = ([-1,-1], [-1,0], [-1,1],
12 [ 0,-1], [ 0,1],
13 [ 1,-1], [ 1,0], [ 1,1]);
14  
15 1 ▶sub rays { return map { [ @$_ ] } @RAY }
Subroutine rays: called CC=1 SCAR=0.0
Pod documented
16  
17 615 ▶sub empty { return [ (undef) x CELLS ] }
Subroutine empty: called CC=1 SCAR=0.0
Pod documented
18  
19 0 ▶sub clone { my ($class, $board) = @_; return [ @$board ] }
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
20  
21 670244 ▶sub other { my ($class, $colour) = @_; return $colour eq 'b' ? 'w' : 'b' }
Subroutine other: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$colour eq 'b' ? : 332827 337417 670244
22  
23 2410 ▶sub square_of {
Subroutine square_of: called CC=5 SCAR=19.4
Pod documented
24 2410 my ($class, $file, $rank) = @_;
25 2410 ▶ return undef unless defined $file && defined $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $rank 0 2410 2410
Condition33%1/3
defined $file and defined $rank
!ll&&!rl&&r
002410
MC/DC0%0/2
defined $file and defined $rank
defined $file defined $rank
Truth table33%1/3
A and B
A= defined $fileB= defined $rank
ABresult
0X0
100
111
26 2410 ▶ return undef unless $file =~ /\A[a-h]\z/ && $rank =~ /\A[1-8]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\A[a-h]\z/ and $rank =~ /\A[1-8]\z/ 0 2410 2410
Condition33%1/3
$file =~ /\A[a-h]\z/ and $rank =~ /\A[1-8]\z/
!ll&&!rl&&r
002410
MC/DC0%0/2
$file =~ /\A[a-h]\z/ and $rank =~ /\A[1-8]\z/
$file =~ /\A[a-h]\z/ $rank =~ /\A[1-8]\z/
Truth table33%1/3
A and B
A= $file =~ /\A[a-h]\z/B= $rank =~ /\A[1-8]\z/
ABresult
0X0
100
111
27 2410 return (8 - $rank) * SIZE + (ord($file) - ord('a'));
28 }
29  
30 6816 ▶sub name_of {
Subroutine name_of: called CC=5 SCAR=19.6
Pod documented
31 6816 my ($class, $square) = @_;
32 6816 ▶ return undef unless defined $square && $square =~ /\A\d+\z/
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square and $square =~ /\A\d+\z/ and $square >= 0 and $square < 64 0 6816 6816
Condition33%3/9
defined $square and $square =~ /\A\d+\z/ and $square >= 0 and $square < 64
!ll&&!rl&&r
006816
defined $square and $square =~ /\A\d+\z/ and $square >= 0
!ll&&!rl&&r
006816
defined $square and $square =~ /\A\d+\z/
!ll&&!rl&&r
006816
MC/DC0%0/4
defined $square and $square =~ /\A\d+\z/ and $square >= 0 and $square < 64
defined $square $square =~ /\A\d+\z/ $square >= 0 $square < 64
Truth table20%1/5
A and B and C and D
A= defined $squareB= $square =~ /\A\d+\z/C= $square >= 0D= $square < 64
ABCDresult
0XXX0
10XX0
110X0
11100
11111
33 && $square >= 0 && $square < CELLS;
34 6816 my $row = int($square / SIZE);
35 6816 my $col = $square % SIZE;
36 6816 return chr(ord('a') + $col) . (8 - $row);
37 }
38  
39 11844589 ▶sub flips_for {
Subroutine flips_for: called CC=20 SCAR=30.1
Pod documented
40 11844589 my ($class, $board, $square, $colour) = @_;
41 11844589 ▶ return () unless defined $square && $square >= 0 && $square < CELLS;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square and $square >= 0 and $square < 64 0 11844589 11844589
Condition33%2/6
defined $square and $square >= 0 and $square < 64
!ll&&!rl&&r
0011844589
defined $square and $square >= 0
!ll&&!rl&&r
0011844589
MC/DC0%0/3
defined $square and $square >= 0 and $square < 64
defined $square $square >= 0 $square < 64
Truth table25%1/4
A and B and C
A= defined $squareB= $square >= 0C= $square < 64
ABCresult
0XX0
10X0
1100
1111
42 11844589 ▶ return () if defined $board->[$square];
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 2 11844587 11844589
43  
44 11844587 ▶ my $them = $colour eq 'b' ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$colour eq 'b' ? : 5673221 6171366 11844587
45 11844587 my $row = int($square / SIZE);
46 11844587 my $col = $square % SIZE;
47  
48 11844587 my @flips;
49 11844587 for my $ray (@RAY) {
50 94756696 my ($dr, $dc) = @$ray;
51 94756696 my ($r, $c) = ($row + $dr, $col + $dc);
52 94756696 my @run;
53  
54 94756696 ▶ while ($r >= 0 && $r < SIZE && $c >= 0 && $c < SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r >= 0 and $r < 8 and $c >= 0 and $c < 8) 84832215 22177385 107009600
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
!ll&&!rl&&r
17234504494288184832215
$r >= 0 and $r < 8 and $c >= 0
!ll&&!rl&&r
11640178559432689775096
$r >= 0 and $r < 8
!ll&&!rl&&r
5922809571736995369422
MC/DC100%4/4
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
$r >= 0 $r < 8 $c >= 0 $c < 8
Truth table100%5/5
A and B and C and D
A= $r >= 0B= $r < 8C= $c >= 0D= $c < 8
ABCDresult
0XXX0
10XX0
110X0
11100
11111
55 84832215 my $cell = $board->[ $r * SIZE + $c ];
56 84832215 ▶ last unless defined $cell && $cell eq $them;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell and $cell eq $them 72579311 12252904 84832215
Condition100%3/3
defined $cell and $cell eq $them
!ll&&!rl&&r
621305241044878712252904
MC/DC100%2/2
defined $cell and $cell eq $them
defined $cell $cell eq $them
Truth table100%3/3
A and B
A= defined $cellB= $cell eq $them
ABresult
0X0
100
111
57 12252904 push @run, $r * SIZE + $c;
58 12252904 ($r, $c) = ($r + $dr, $c + $dc);
59 }
60  
61 94756696 ▶ next unless @run;
Branch: 1 branch
BranchTrueFalseTotal
unless @run 86693462 8063234 94756696
62 8063234 ▶ next unless $r >= 0 && $r < SIZE && $c >= 0 && $c < SIZE;
Branch: 1 branch
BranchTrueFalseTotal
unless $r >= 0 and $r < 8 and $c >= 0 and $c < 8 789382 7273852 8063234
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
!ll&&!rl&&r
5938031955797273852
$r >= 0 and $r < 8 and $c >= 0
!ll&&!rl&&r
4493231444807469431
$r >= 0 and $r < 8
!ll&&!rl&&r
2861271631967613911
MC/DC100%4/4
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
$r >= 0 $r < 8 $c >= 0 $c < 8
Truth table100%5/5
A and B and C and D
A= $r >= 0B= $r < 8C= $c >= 0D= $c < 8
ABCDresult
0XXX0
10XX0
110X0
11100
11111
63 7273852 my $end = $board->[ $r * SIZE + $c ];
64 7273852 ▶ next unless defined $end && $end eq $colour;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end and $end eq $colour 3985228 3288624 7273852
Condition66%2/3
defined $end and $end eq $colour
!ll&&!rl&&r
398522803288624
MC/DC50%1/2
defined $end and $end eq $colour
defined $end $end eq $colour
Truth table66%2/3
A and B
A= defined $endB= $end eq $colour
ABresult
0X0
100
111
65  
66 3288624 push @flips, @run;
67 }
68  
69 11844587 return @flips;
70 }
71  
72 261373 ▶sub legal_moves {
Subroutine legal_moves: called CC=1 SCAR=0.0
Pod documented
73 261373 my ($class, $board, $colour) = @_;
74 11093119 return grep { scalar $class->flips_for($board, $_, $colour) }
75 261373 grep { !defined $board->[$_] } 0 .. CELLS - 1;
76 }
77  
78 23244 ▶sub has_move {
Subroutine has_move: called CC=4 SCAR=13.9
Pod documented
79 23244 my ($class, $board, $colour) = @_;
80 23244 for my $square (0 .. CELLS - 1) {
81 346835 ▶ next if defined $board->[$square];
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 192440 154395 346835
82 154395 ▶ return 1 if $class->flips_for($board, $square, $colour);
Branch: 1 branch
BranchTrueFalseTotal
if $class->flips_for($board, $square, $colour) 22245 132150 154395
83 }
84 999 return 0;
85 }
86  
87 394897 ▶sub apply {
Subroutine apply: called CC=4 SCAR=13.9
Pod documented
88 394897 my ($class, $board, $square, $colour) = @_;
89 394897 my @flips = $class->flips_for($board, $square, $colour);
90 394897 ▶ die "Game::Reversi::Board: $colour cannot play "
Branch: 1 branch
BranchTrueFalseTotal
unless @flips 1 394896 394897
Condition50%1/2
$class->name_of($square) // "that square"
l!l
10
MC/DC0%0/1
$class->name_of($square) // "that square"
$class->name_of($square)
Truth table50%1/2
A
A= $class->name_of($square)
Aresult
11
00
91 . ($class->name_of($square) // 'that square') . ", it outflanks nothing"
92 unless @flips;
93  
94 394896 my $after = [ @$board ];
95 394896 $after->[$square] = $colour;
96 394896 $after->[$_] = $colour for @flips;
97 394896 return $after;
98 }
99  
100 275970 ▶sub count {
Subroutine count: called CC=3 SCAR=11.0
Pod documented
101 275970 my ($class, $board) = @_;
102 275970 my %n = (b => 0, w => 0);
103 275970 for my $cell (@$board) {
104 17662080 ▶ $n{$cell}++ if defined $cell;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell 7101194 10560886 17662080
105 }
106 275970 return \%n;
107 }
108  
109 275209 ▶sub empties {
Subroutine empties: called CC=1 SCAR=0.0
Pod documented
110 275209 my ($class, $board) = @_;
111 275209 return scalar grep { !defined } @$board;
112 }
113  
114 19 1;
115  
116 __END__
« Reversi/Move.pm ↑ Summary Reversi/Result.pm »