blib/lib/Game/Checkers/Board.pm

filter: Statementstmt 89.1% 66 / 74 Branchbran 67.6% 23 / 34 Conditioncond 66.6% 8 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 95.2% 20 / 21 Podpod 92.8% 13 / 14 totaltotal 82.8% 135 / 163 CC 27 SCAR 21.5
CC
27
Cov
81%
pieces
42.0
BUILD
7.0
count
7.0
CRAP 8.6
« blib/lib/Game/Checkers/Rules.pm ↑ Summary blib/lib/Game/Checkers.pm »
1 package Game::Checkers::Board;
2  
3 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 31 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 31 ▶use Game::Checkers::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use Game::Checkers::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 31 our $VERSION = '0.04';
11  
12 31 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 EMPTY => 0,
14 BLACK_MAN => 1,
15 BLACK_KING => 2,
16 WHITE_MAN => -1,
17 WHITE_KING => -2,
18 31 };
19  
20 has position => (
21 is => 'rw',
22 isa => ArrayRef,
23 384 ▶ default => sub { __PACKAGE__->opening_position }
Subroutine __ANON__: called CC=1 SCAR=0.0
24 31 );
25  
26 483 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
27 483 my ($self) = @_;
28 483 my $position = $self->position;
29 die 'position must be an arrayref of 33 values, index 0 unused'
30 483 ▶ unless ref $position eq 'ARRAY' && @{$position} == 33;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $position eq "ARRAY" and @{$position;} == 33 2 481 483
Condition66%2/3
ref $position eq "ARRAY" and @{$position;} == 33
!ll&&!rl&&r
02481
MC/DC50%1/2
ref $position eq "ARRAY" and @{$position;} == 33
ref $position eq "ARRAY" @{$position;} == 33
Truth table66%2/3
A and B
A= ref $position eq "ARRAY"B= @{$position;} == 33
ABresult
0X0
100
111
31 481 $position->[0] = EMPTY;
32 481 for my $square (1 .. 32) {
33 15392 my $value = $position->[$square];
34 15392 ▶ die "position square $square must be -2 .. 2, got "
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value and $value =~ /^-?[0-2]$/ 2 15390 15392
defined $value ? : 1 1 2
Condition100%3/3
defined $value and $value =~ /^-?[0-2]$/
!ll&&!rl&&r
1115390
MC/DC100%2/2
defined $value and $value =~ /^-?[0-2]$/
defined $value $value =~ /^-?[0-2]$/
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /^-?[0-2]$/
ABresult
0X0
100
111
35 . (defined $value ? "'$value'" : 'undef')
36 unless defined $value && $value =~ m/^-?[0-2]$/;
37 15390 $position->[$square] = $value + 0;
38 }
39 479 return $self;
40 }
41  
42 384 ▶sub opening_position {
Subroutine opening_position: called CC=3 SCAR=11.0
Pod documented
43 384 my @position = (EMPTY) x 33;
44 384 $position[$_] = BLACK_MAN for 1 .. 12;
45 384 $position[$_] = WHITE_MAN for 21 .. 32;
46 384 return \@position;
47 }
48  
49 6462 ▶sub at {
Subroutine at: called CC=1 SCAR=0.0
Pod documented
50 6462 return $_[0]->position->[$_[1]];
51 }
52  
53 8 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
54 8 my ($self, $square, $value) = @_;
55 8 $self->position->[$square] = $value;
56 8 return $self;
57 }
58  
59 1 ▶sub occupied {
Subroutine occupied: called CC=2 SCAR=7.6
Pod documented
60 1 ▶ return $_[0]->position->[$_[1]] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->position->[$_[1]] ? : 1 0 1
61 }
62  
63 1 ▶sub empty {
Subroutine empty: called CC=2 SCAR=7.6
Pod documented
64 1 ▶ return $_[0]->position->[$_[1]] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->position->[$_[1]] ? : 0 1 1
65 }
66  
67 1 ▶sub colour_at {
Subroutine colour_at: called CC=3 SCAR=12.0
Pod documented
68 1 ▶ my $value = $_[0]->position->[$_[1]] or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $_[0]->position->[$_[1]] or return undef 1 0 1
69 1 ▶ return $value > 0 ? 'black' : 'white';
Branch: 1 branch
BranchTrueFalseTotal
$value > 0 ? : 1 0 1
70 }
71  
72 4 ▶sub king_at {
Subroutine king_at: called CC=3 SCAR=11.1
Pod documented
73 4 ▶ my $value = $_[0]->position->[$_[1]] or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $_[0]->position->[$_[1]] or return 0 4 0 4
74 4 ▶ return abs($value) == 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 3 1 4
75 }
76  
77 9 ▶sub piece {
Subroutine piece: called CC=1 SCAR=0.0
Pod documented
78 9 my ($self, $square) = @_;
79 9 return Game::Checkers::Piece->from_value($self->position->[$square], $square);
80 }
81  
82 0 ▶sub pieces {
Subroutine pieces: not called CC=6 SCAR=37.4
Pod documented
83 0 my ($self, $side) = @_;
84 0 my $position = $self->position;
85 0 my @pieces;
86 0 for my $square (1 .. 32) {
87 0 ▶ my $value = $position->[$square] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$square] or next 0 0 0
88 0 ▶ next if $side && ($value > 0 ? 'black' : 'white') ne $side;
Branch: 2 branches
BranchTrueFalseTotal
if $side and ($value > 0 ? "black" : "white") ne $side 0 0 0
$value > 0 ? : 0 0 0
Condition0%0/3
$side and ($value > 0 ? "black" : "white") ne $side
!ll&&!rl&&r
000
MC/DC0%0/2
$side and ($value > 0 ? "black" : "white") ne $side
$side ($value > 0 ? "black" : "white") ne $side
Truth table0%0/3
A and B
A= $sideB= ($value > 0 ? "black" : "white") ne $side
ABresult
0X0
100
111
89 0 push @pieces, Game::Checkers::Piece->from_value($value, $square);
90 }
91 0 return \@pieces;
92 }
93  
94 211 ▶sub count {
Subroutine count: called CC=7 SCAR=19.5
Pod documented
95 211 my ($self, $side) = @_;
96 211 my $position = $self->position;
97 211 my %count = (men => 0, kings => 0, total => 0);
98 211 for my $square (1 .. 32) {
99 6752 ▶ my $value = $position->[$square] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$square] or next 4740 2012 6752
100 4740 ▶ next if $side && ($value > 0 ? 'black' : 'white') ne $side;
Branch: 2 branches
BranchTrueFalseTotal
if $side and ($value > 0 ? "black" : "white") ne $side 2359 2381 4740
$value > 0 ? : 2360 2356 4716
Condition100%3/3
$side and ($value > 0 ? "black" : "white") ne $side
!ll&&!rl&&r
2423572359
MC/DC100%2/2
$side and ($value > 0 ? "black" : "white") ne $side
$side ($value > 0 ? "black" : "white") ne $side
Truth table100%3/3
A and B
A= $sideB= ($value > 0 ? "black" : "white") ne $side
ABresult
0X0
100
111
101 2381 ▶ $count{abs($value) == 2 ? 'kings' : 'men'}++;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 6 2375 2381
102 2381 $count{total}++;
103 }
104 211 return \%count;
105 }
106  
107 25 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
108 25 my ($self) = @_;
109 25 return ref($self)->new(position => [@{$self->position}]);
110 }
111  
112 3858 ▶sub to_fen {
Subroutine to_fen: called CC=1 SCAR=0.0
Pod documented
113 3858 my ($self, $turn) = @_;
114 3858 return Game::Checkers::Notation::fen_from_position($self->position, $turn);
115 }
116  
117 69 ▶sub from_fen {
Subroutine from_fen: called CC=2 SCAR=6.9
Pod documented
118 69 my ($class, $fen, %opt) = @_;
119 69 my ($position, $turn) = Game::Checkers::Notation::position_from_fen($fen, %opt);
120 69 my $board = $class->new(position => $position);
121 69 ▶ return wantarray ? ($board, $turn) : $board;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 68 1 69
122 }
123  
124 31 1;
125  
126 __END__
« blib/lib/Game/Checkers/Rules.pm ↑ Summary blib/lib/Game/Checkers.pm »