Reversi/Opening.pm

filter: Statementstmt 100% 68 / 68 Branchbran 100% 18 / 18 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 19 / 19 Podpod 100% 11 / 11 totaltotal 98.3% 117 / 119 CC 11 SCAR 6.8
CC
11
Cov
99%
board_for
4.0
check
3.0
describes
2.1
CRAP 2.0
« Reversi/Error.pm ↑ Summary
1 package Game::Reversi::Opening;
2  
3 17 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 17 ▶use Game::Reversi::Board;
Subroutine __ANON__: called CC=1 SCAR=0.0
7 17 ▶use Game::Reversi::Move;
Subroutine __ANON__: called CC=1 SCAR=0.0
8 17 ▶use Game::Reversi::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 17 our $VERSION = '0.04';
11  
12 17 our @CENTRE;
13 17 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 @CENTRE = map { Game::Reversi::Board->square_of(@$_) }
15 ([ 'd', 5 ], [ 'e', 5 ], [ 'd', 4 ], [ 'e', 4 ]);
16 }
17  
18 17 ▶use constant PLIES => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 17 our %VARIANT = (
21 historic => 'the players place the first four discs themselves',
22 othello => 'the four centre discs are already placed, on the diagonals',
23 );
24  
25 1 ▶sub variants { return sort keys %VARIANT }
Subroutine variants: called CC=1 SCAR=0.0
Pod documented
26 552 ▶sub describes { my ($class, $v) = @_; return $VARIANT{ $v // '' } }
Subroutine describes: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$v // ""
l!l
5520
MC/DC0%0/1
$v // ""
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
27 10 ▶sub centre { return @CENTRE }
Subroutine centre: called CC=1 SCAR=0.0
Pod documented
28  
29 1091 ▶sub is_centre {
Subroutine is_centre: called CC=2 SCAR=6.9
Pod documented
30 1091 my ($class, $square) = @_;
31 1091 ▶ return 0 unless defined $square;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square 2 1089 1091
32 1089 return scalar grep { $_ == $square } @CENTRE;
33 }
34  
35 561 ▶sub board_for {
Subroutine board_for: called CC=4 SCAR=13.9
Pod documented
36 561 my ($class, $variant) = @_;
37 561 ▶ $variant = 'historic' unless defined $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant 1 560 561
38 die "Game::Reversi::Opening: there is no variant '$variant'"
39 561 ▶ unless exists $VARIANT{$variant};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Game::Reversi::Opening::VARIANT{$variant} 1 560 561
40  
41 560 my $board = Game::Reversi::Board->empty;
42 560 ▶ return $board if $variant eq 'historic';
Branch: 1 branch
BranchTrueFalseTotal
if $variant eq "historic" 195 365 560
43  
44 365 ▶ my $at = sub { Game::Reversi::Board->square_of(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
45 365 $board->[ $at->('e', 4) ] = 'b';
46 365 $board->[ $at->('d', 5) ] = 'b';
47 365 $board->[ $at->('d', 4) ] = 'w';
48 365 $board->[ $at->('e', 5) ] = 'w';
49 365 return $board;
50 }
51  
52 801 ▶sub first { return 'b' }
Subroutine first: called CC=1 SCAR=0.0
Pod documented
53  
54 69810 ▶sub in_opening {
Subroutine in_opening: called CC=2 SCAR=6.9
Pod documented
55 69810 my ($class, $board) = @_;
56 69810 ▶ return (grep { !defined $board->[$_] } @CENTRE) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({not defined $board->[$_];} @Game::Reversi::Opening::CENTRE) ? : 1831 67979 69810
57 }
58  
59 8 ▶sub plies_left {
Subroutine plies_left: called CC=1 SCAR=0.0
Pod documented
60 8 my ($class, $board) = @_;
61 8 return scalar grep { !defined $board->[$_] } @CENTRE;
62 }
63  
64 390 ▶sub legal {
Subroutine legal: called CC=2 SCAR=6.9
Pod documented
65 390 my ($class, $board, $colour) = @_;
66 390 ▶ return () unless $class->in_opening($board);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->in_opening($board) 1 389 390
67 1021 return map { Game::Reversi::Move->place($_, $colour) }
68 389 grep { !defined $board->[$_] } @CENTRE;
69 }
70  
71 1087 ▶sub check {
Subroutine check: called CC=3 SCAR=11.0
Pod documented
72 1087 my ($class, $board, $square) = @_;
73 return Game::Reversi::Error->throw('not_centre',
74 1087 ▶ legal => [ grep { !defined $board->[$_] } @CENTRE ])
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_centre($square) 11 1076 1087
75 unless $class->is_centre($square);
76 return Game::Reversi::Error->throw('square_taken',
77 1076 ▶ legal => [ grep { !defined $board->[$_] } @CENTRE ])
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 3 1073 1076
78 if defined $board->[$square];
79 1073 return undef;
80 }
81  
82 651 ▶sub apply {
Subroutine apply: called CC=2 SCAR=6.9
Pod documented
83 651 my ($class, $board, $square, $colour) = @_;
84 651 my $error = $class->check($board, $square);
85 651 ▶ die 'Game::Reversi::Opening: ' . $error->stringify if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 650 651
86  
87 650 my $after = [ @$board ];
88 650 $after->[$square] = $colour;
89 650 return $after;
90 }
91  
92 17 1;
93  
94 __END__
« Reversi/Error.pm ↑ Summary