blib/lib/Game/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
« blib/lib/Game/Reversi/Move.pm ↑ Summary blib/lib/Game/Reversi/Result.pm »
1 package Game::Reversi::Board;
2  
3 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 37 our $VERSION = '0.05';
7  
8 37 ▶use constant SIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use constant CELLS => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 37 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 635 ▶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 670387 ▶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' ? : 332906 337481 670387
22  
23 2604 ▶sub square_of {
Subroutine square_of: called CC=5 SCAR=19.4
Pod documented
24 2604 my ($class, $file, $rank) = @_;
25 2604 ▶ return undef unless defined $file && defined $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $rank 0 2604 2604
Condition33%1/3
defined $file and defined $rank
!ll&&!rl&&r
002604
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 2604 ▶ 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 2604 2604
Condition33%1/3
$file =~ /\A[a-h]\z/ and $rank =~ /\A[1-8]\z/
!ll&&!rl&&r
002604
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 2604 return (8 - $rank) * SIZE + (ord($file) - ord('a'));
28 }
29  
30 6943 ▶sub name_of {
Subroutine name_of: called CC=5 SCAR=19.6
Pod documented
31 6943 my ($class, $square) = @_;
32 6943 ▶ 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 6943 6943
Condition33%3/9
defined $square and $square =~ /\A\d+\z/ and $square >= 0 and $square < 64
!ll&&!rl&&r
006943
defined $square and $square =~ /\A\d+\z/ and $square >= 0
!ll&&!rl&&r
006943
defined $square and $square =~ /\A\d+\z/
!ll&&!rl&&r
006943
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 6943 my $row = int($square / SIZE);
35 6943 my $col = $square % SIZE;
36 6943 return chr(ord('a') + $col) . (8 - $row);
37 }
38  
39 11849467 ▶sub flips_for {
Subroutine flips_for: called CC=20 SCAR=30.1
Pod documented
40 11849467 my ($class, $board, $square, $colour) = @_;
41 11849467 ▶ return () unless defined $square && $square >= 0 && $square < CELLS;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square and $square >= 0 and $square < 64 0 11849467 11849467
Condition33%2/6
defined $square and $square >= 0 and $square < 64
!ll&&!rl&&r
0011849467
defined $square and $square >= 0
!ll&&!rl&&r
0011849467
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 11849467 ▶ return () if defined $board->[$square];
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 2 11849465 11849467
43  
44 11849465 ▶ my $them = $colour eq 'b' ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$colour eq 'b' ? : 5676717 6172748 11849465
45 11849465 my $row = int($square / SIZE);
46 11849465 my $col = $square % SIZE;
47  
48 11849465 my @flips;
49 11849465 for my $ray (@RAY) {
50 94795720 my ($dr, $dc) = @$ray;
51 94795720 my ($r, $c) = ($row + $dr, $col + $dc);
52 94795720 my @run;
53  
54 94795720 ▶ 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) 84865812 22184819 107050631
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
!ll&&!rl&&r
17240420494439984865812
$r >= 0 and $r < 8 and $c >= 0
!ll&&!rl&&r
11644354559606689810211
$r >= 0 and $r < 8
!ll&&!rl&&r
5925785571856995406277
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 84865812 my $cell = $board->[ $r * SIZE + $c ];
56 84865812 ▶ last unless defined $cell && $cell eq $them;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell and $cell eq $them 72610901 12254911 84865812
Condition100%3/3
defined $cell and $cell eq $them
!ll&&!rl&&r
621604121045048912254911
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 12254911 push @run, $r * SIZE + $c;
58 12254911 ($r, $c) = ($r + $dr, $c + $dc);
59 }
60  
61 94795720 ▶ next unless @run;
Branch: 1 branch
BranchTrueFalseTotal
unless @run 86730895 8064825 94795720
62 8064825 ▶ 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 7275443 8064825
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
!ll&&!rl&&r
5938031955797275443
$r >= 0 and $r < 8 and $c >= 0
!ll&&!rl&&r
4493231444807471022
$r >= 0 and $r < 8
!ll&&!rl&&r
2861271631967615502
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 7275443 my $end = $board->[ $r * SIZE + $c ];
64 7275443 ▶ next unless defined $end && $end eq $colour;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end and $end eq $colour 3986203 3289240 7275443
Condition66%2/3
defined $end and $end eq $colour
!ll&&!rl&&r
398620303289240
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 3289240 push @flips, @run;
67 }
68  
69 11849465 return @flips;
70 }
71  
72 261423 ▶sub legal_moves {
Subroutine legal_moves: called CC=1 SCAR=0.0
Pod documented
73 261423 my ($class, $board, $colour) = @_;
74 11096015 return grep { scalar $class->flips_for($board, $_, $colour) }
75 261423 grep { !defined $board->[$_] } 0 .. CELLS - 1;
76 }
77  
78 23318 ▶sub has_move {
Subroutine has_move: called CC=4 SCAR=13.9
Pod documented
79 23318 my ($class, $board, $colour) = @_;
80 23318 for my $square (0 .. CELLS - 1) {
81 348493 ▶ next if defined $board->[$square];
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 192460 156033 348493
82 156033 ▶ return 1 if $class->flips_for($board, $square, $colour);
Branch: 1 branch
BranchTrueFalseTotal
if $class->flips_for($board, $square, $colour) 22319 133714 156033
83 }
84 999 return 0;
85 }
86  
87 394989 ▶sub apply {
Subroutine apply: called CC=4 SCAR=13.9
Pod documented
88 394989 my ($class, $board, $square, $colour) = @_;
89 394989 my @flips = $class->flips_for($board, $square, $colour);
90 394989 ▶ die "Game::Reversi::Board: $colour cannot play "
Branch: 1 branch
BranchTrueFalseTotal
unless @flips 1 394988 394989
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 394988 my $after = [ @$board ];
95 394988 $after->[$square] = $colour;
96 394988 $after->[$_] = $colour for @flips;
97 394988 return $after;
98 }
99  
100 276064 ▶sub count {
Subroutine count: called CC=3 SCAR=11.0
Pod documented
101 276064 my ($class, $board) = @_;
102 276064 my %n = (b => 0, w => 0);
103 276064 for my $cell (@$board) {
104 17668096 ▶ $n{$cell}++ if defined $cell;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell 7101814 10566282 17668096
105 }
106 276064 return \%n;
107 }
108  
109 275247 ▶sub empties {
Subroutine empties: called CC=1 SCAR=0.0
Pod documented
110 275247 my ($class, $board) = @_;
111 275247 return scalar grep { !defined } @$board;
112 }
113  
114 37 1;
115  
116 __END__
« blib/lib/Game/Reversi/Move.pm ↑ Summary blib/lib/Game/Reversi/Result.pm »