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.pm »
1 package Game::Reversi::Board;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = '0.02';
7  
8 15 ▶use constant SIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use constant CELLS => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 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 559 ▶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 621471 ▶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' ? : 308668 312803 621471
22  
23 1872 ▶sub square_of {
Subroutine square_of: called CC=5 SCAR=19.4
Pod documented
24 1872 my ($class, $file, $rank) = @_;
25 1872 ▶ return undef unless defined $file && defined $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $rank 0 1872 1872
Condition33%1/3
defined $file and defined $rank
!ll&&!rl&&r
001872
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 1872 ▶ 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 1872 1872
Condition33%1/3
$file =~ /\A[a-h]\z/ and $rank =~ /\A[1-8]\z/
!ll&&!rl&&r
001872
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 1872 return (8 - $rank) * SIZE + (ord($file) - ord('a'));
28 }
29  
30 1156 ▶sub name_of {
Subroutine name_of: called CC=5 SCAR=19.6
Pod documented
31 1156 my ($class, $square) = @_;
32 1156 ▶ 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 1156 1156
Condition33%3/9
defined $square and $square =~ /\A\d+\z/ and $square >= 0 and $square < 64
!ll&&!rl&&r
001156
defined $square and $square =~ /\A\d+\z/ and $square >= 0
!ll&&!rl&&r
001156
defined $square and $square =~ /\A\d+\z/
!ll&&!rl&&r
001156
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 1156 my $row = int($square / SIZE);
35 1156 my $col = $square % SIZE;
36 1156 return chr(ord('a') + $col) . (8 - $row);
37 }
38  
39 11592518 ▶sub flips_for {
Subroutine flips_for: called CC=20 SCAR=30.1
Pod documented
40 11592518 my ($class, $board, $square, $colour) = @_;
41 11592518 ▶ return () unless defined $square && $square >= 0 && $square < CELLS;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square and $square >= 0 and $square < 64 0 11592518 11592518
Condition33%2/6
defined $square and $square >= 0 and $square < 64
!ll&&!rl&&r
0011592518
defined $square and $square >= 0
!ll&&!rl&&r
0011592518
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 11592518 ▶ return () if defined $board->[$square];
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 2 11592516 11592518
43  
44 11592516 ▶ my $them = $colour eq 'b' ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$colour eq 'b' ? : 5539691 6052825 11592516
45 11592516 my $row = int($square / SIZE);
46 11592516 my $col = $square % SIZE;
47  
48 11592516 my @flips;
49 11592516 for my $ray (@RAY) {
50 92740128 my ($dr, $dc) = @$ray;
51 92740128 my ($r, $c) = ($row + $dr, $col + $dc);
52 92740128 my @run;
53  
54 92740128 ▶ 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) 82936025 21676585 104612610
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
!ll&&!rl&&r
16856126482045982936025
$r >= 0 and $r < 8 and $c >= 0
!ll&&!rl&&r
11385911547021587756484
$r >= 0 and $r < 8
!ll&&!rl&&r
5787264559864793226699
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 82936025 my $cell = $board->[ $r * SIZE + $c ];
56 82936025 ▶ last unless defined $cell && $cell eq $them;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell and $cell eq $them 71063543 11872482 82936025
Condition100%3/3
defined $cell and $cell eq $them
!ll&&!rl&&r
608943461016919711872482
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 11872482 push @run, $r * SIZE + $c;
58 11872482 ($r, $c) = ($r + $dr, $c + $dc);
59 }
60  
61 92740128 ▶ next unless @run;
Branch: 1 branch
BranchTrueFalseTotal
unless @run 84928374 7811754 92740128
62 7811754 ▶ 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 754496 7057258 7811754
Condition100%9/9
$r >= 0 and $r < 8 and $c >= 0 and $c < 8
!ll&&!rl&&r
5658671886297057258
$r >= 0 and $r < 8 and $c >= 0
!ll&&!rl&&r
4274931383747245887
$r >= 0 and $r < 8
!ll&&!rl&&r
2744011530927384261
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 7057258 my $end = $board->[ $r * SIZE + $c ];
64 7057258 ▶ next unless defined $end && $end eq $colour;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end and $end eq $colour 3868141 3189117 7057258
Condition66%2/3
defined $end and $end eq $colour
!ll&&!rl&&r
386814103189117
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 3189117 push @flips, @run;
67 }
68  
69 11592516 return @flips;
70 }
71  
72 255440 ▶sub legal_moves {
Subroutine legal_moves: called CC=1 SCAR=0.0
Pod documented
73 255440 my ($class, $board, $colour) = @_;
74 10878463 return grep { scalar $class->flips_for($board, $_, $colour) }
75 255440 grep { !defined $board->[$_] } 0 .. CELLS - 1;
76 }
77  
78 22581 ▶sub has_move {
Subroutine has_move: called CC=4 SCAR=13.9
Pod documented
79 22581 my ($class, $board, $colour) = @_;
80 22581 for my $square (0 .. CELLS - 1) {
81 335330 ▶ next if defined $board->[$square];
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 188760 146570 335330
82 146570 ▶ return 1 if $class->flips_for($board, $square, $colour);
Branch: 1 branch
BranchTrueFalseTotal
if $class->flips_for($board, $square, $colour) 21610 124960 146570
83 }
84 971 return 0;
85 }
86  
87 367480 ▶sub apply {
Subroutine apply: called CC=4 SCAR=13.9
Pod documented
88 367480 my ($class, $board, $square, $colour) = @_;
89 367480 my @flips = $class->flips_for($board, $square, $colour);
90 367480 ▶ die "Game::Reversi::Board: $colour cannot play "
Branch: 1 branch
BranchTrueFalseTotal
unless @flips 1 367479 367480
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 367479 my $after = [ @$board ];
95 367479 $after->[$square] = $colour;
96 367479 $after->[$_] = $colour for @flips;
97 367479 return $after;
98 }
99  
100 254633 ▶sub count {
Subroutine count: called CC=3 SCAR=11.0
Pod documented
101 254633 my ($class, $board) = @_;
102 254633 my %n = (b => 0, w => 0);
103 254633 for my $cell (@$board) {
104 16296512 ▶ $n{$cell}++ if defined $cell;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cell 6478044 9818468 16296512
105 }
106 254633 return \%n;
107 }
108  
109 254007 ▶sub empties {
Subroutine empties: called CC=1 SCAR=0.0
Pod documented
110 254007 my ($class, $board) = @_;
111 254007 return scalar grep { !defined } @$board;
112 }
113  
114 15 1;
115  
116 __END__
« Reversi/Move.pm ↑ Summary Reversi.pm »