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 13 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 13 ▶use Game::Reversi::Board;
Subroutine __ANON__: called CC=1 SCAR=0.0
7 13 ▶use Game::Reversi::Move;
Subroutine __ANON__: called CC=1 SCAR=0.0
8 13 ▶use Game::Reversi::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 13 our $VERSION = '0.02';
11  
12 13 our @CENTRE;
13 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 @CENTRE = map { Game::Reversi::Board->square_of(@$_) }
15 ([ 'd', 5 ], [ 'e', 5 ], [ 'd', 4 ], [ 'e', 4 ]);
16 }
17  
18 13 ▶use constant PLIES => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 13 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 479 ▶sub describes { my ($class, $v) = @_; return $VARIANT{ $v // '' } }
Subroutine describes: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$v // ""
l!l
4790
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 869 ▶sub is_centre {
Subroutine is_centre: called CC=2 SCAR=6.9
Pod documented
30 869 my ($class, $square) = @_;
31 869 ▶ return 0 unless defined $square;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square 2 867 869
32 867 return scalar grep { $_ == $square } @CENTRE;
33 }
34  
35 505 ▶sub board_for {
Subroutine board_for: called CC=4 SCAR=13.9
Pod documented
36 505 my ($class, $variant) = @_;
37 505 ▶ $variant = 'historic' unless defined $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variant 1 504 505
38 die "Game::Reversi::Opening: there is no variant '$variant'"
39 505 ▶ unless exists $VARIANT{$variant};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Game::Reversi::Opening::VARIANT{$variant} 1 504 505
40  
41 504 my $board = Game::Reversi::Board->empty;
42 504 ▶ return $board if $variant eq 'historic';
Branch: 1 branch
BranchTrueFalseTotal
if $variant eq "historic" 165 339 504
43  
44 339 ▶ my $at = sub { Game::Reversi::Board->square_of(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
45 339 $board->[ $at->('e', 4) ] = 'b';
46 339 $board->[ $at->('d', 5) ] = 'b';
47 339 $board->[ $at->('d', 4) ] = 'w';
48 339 $board->[ $at->('e', 5) ] = 'w';
49 339 return $board;
50 }
51  
52 486 ▶sub first { return 'b' }
Subroutine first: called CC=1 SCAR=0.0
Pod documented
53  
54 67789 ▶sub in_opening {
Subroutine in_opening: called CC=2 SCAR=6.9
Pod documented
55 67789 my ($class, $board) = @_;
56 67789 ▶ return (grep { !defined $board->[$_] } @CENTRE) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({not defined $board->[$_];} @Game::Reversi::Opening::CENTRE) ? : 1484 66305 67789
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 345 ▶sub legal {
Subroutine legal: called CC=2 SCAR=6.9
Pod documented
65 345 my ($class, $board, $colour) = @_;
66 345 ▶ return () unless $class->in_opening($board);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->in_opening($board) 1 344 345
67 907 return map { Game::Reversi::Move->place($_, $colour) }
68 344 grep { !defined $board->[$_] } @CENTRE;
69 }
70  
71 865 ▶sub check {
Subroutine check: called CC=3 SCAR=11.0
Pod documented
72 865 my ($class, $board, $square) = @_;
73 return Game::Reversi::Error->throw('not_centre',
74 865 ▶ legal => [ grep { !defined $board->[$_] } @CENTRE ])
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_centre($square) 11 854 865
75 unless $class->is_centre($square);
76 return Game::Reversi::Error->throw('square_taken',
77 854 ▶ legal => [ grep { !defined $board->[$_] } @CENTRE ])
Branch: 1 branch
BranchTrueFalseTotal
if defined $board->[$square] 3 851 854
78 if defined $board->[$square];
79 851 return undef;
80 }
81  
82 540 ▶sub apply {
Subroutine apply: called CC=2 SCAR=6.9
Pod documented
83 540 my ($class, $board, $square, $colour) = @_;
84 540 my $error = $class->check($board, $square);
85 540 ▶ die 'Game::Reversi::Opening: ' . $error->stringify if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 539 540
86  
87 539 my $after = [ @$board ];
88 539 $after->[$square] = $colour;
89 539 return $after;
90 }
91  
92 13 1;
93  
94 __END__
« Reversi/Error.pm ↑ Summary