| 1 | package Game::Checkers::Board; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 28 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Game::Checkers::Piece; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use Game::Checkers::Notation; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 28 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 28 | ▶ | use constant { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 28 | EMPTY => 0, | |||||||||||||||||||||||||||||||
| 14 | BLACK_MAN => 1, | ||||||||||||||||||||||||||||||||
| 15 | BLACK_KING => 2, | ||||||||||||||||||||||||||||||||
| 16 | WHITE_MAN => -1, | ||||||||||||||||||||||||||||||||
| 17 | WHITE_KING => -2, | ||||||||||||||||||||||||||||||||
| 18 | 28 | }; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | has position => ( | ||||||||||||||||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 22 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||
| 23 | 122 | ▶ | default => sub { __PACKAGE__->opening_position } | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 28 | ); | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 207 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 27 | 207 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 28 | 207 | my $position = $self->position; | |||||||||||||||||||||||||||||||
| 29 | die 'position must be an arrayref of 33 values, index 0 unused' | ||||||||||||||||||||||||||||||||
| 30 | 207 | ▶ | unless ref $position eq 'ARRAY' && @{$position} == 33; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $position eq "ARRAY" and @{$position;} == 33
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
| |||||||||||||||||||||||||||||||||
| 31 | 205 | $position->[0] = EMPTY; | |||||||||||||||||||||||||||||||
| 32 | 205 | for my $square (1 .. 32) { | |||||||||||||||||||||||||||||||
| 33 | 6560 | my $value = $position->[$square]; | |||||||||||||||||||||||||||||||
| 34 | 6560 | ▶ | die "position square $square must be -2 .. 2, got " | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $value and $value =~ /^-?[0-2]$/
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]$/
| |||||||||||||||||||||||||||||||||
| 35 | . (defined $value ? "'$value'" : 'undef') | ||||||||||||||||||||||||||||||||
| 36 | unless defined $value && $value =~ m/^-?[0-2]$/; | ||||||||||||||||||||||||||||||||
| 37 | 6558 | $position->[$square] = $value + 0; | |||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | 203 | return $self; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 122 | ▶ | sub opening_position { | ||||||||||||||||||||||||||||||
|
Subroutine
opening_position: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 43 | 122 | my @position = (EMPTY) x 33; | |||||||||||||||||||||||||||||||
| 44 | 122 | $position[$_] = BLACK_MAN for 1 .. 12; | |||||||||||||||||||||||||||||||
| 45 | 122 | $position[$_] = WHITE_MAN for 21 .. 32; | |||||||||||||||||||||||||||||||
| 46 | 122 | return \@position; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 3463 | ▶ | sub at { | ||||||||||||||||||||||||||||||
|
Subroutine
at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 50 | 3463 | 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | return $value > 0 ? 'black' : 'white'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 74 | 4 | ▶ | return abs($value) == 2 ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | next if $side && ($value > 0 ? 'black' : 'white') ne $side; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$side and ($value > 0 ? "black" : "white") ne $side
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
| |||||||||||||||||||||||||||||||||
| 89 | 0 | push @pieces, Game::Checkers::Piece->from_value($value, $square); | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | 0 | return \@pieces; | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 119 | ▶ | sub count { | ||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 95 | 119 | my ($self, $side) = @_; | |||||||||||||||||||||||||||||||
| 96 | 119 | my $position = $self->position; | |||||||||||||||||||||||||||||||
| 97 | 119 | my %count = (men => 0, kings => 0, total => 0); | |||||||||||||||||||||||||||||||
| 98 | 119 | for my $square (1 .. 32) { | |||||||||||||||||||||||||||||||
| 99 | 3808 | ▶ | my $value = $position->[$square] or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | 2604 | ▶ | next if $side && ($value > 0 ? 'black' : 'white') ne $side; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$side and ($value > 0 ? "black" : "white") ne $side
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
| |||||||||||||||||||||||||||||||||
| 101 | 1313 | ▶ | $count{abs($value) == 2 ? 'kings' : 'men'}++; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 1313 | $count{total}++; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | 119 | return \%count; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 16 | ▶ | sub clone { | ||||||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 108 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 109 | 16 | return ref($self)->new(position => [@{$self->position}]); | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 1998 | ▶ | sub to_fen { | ||||||||||||||||||||||||||||||
|
Subroutine
to_fen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 113 | 1998 | my ($self, $turn) = @_; | |||||||||||||||||||||||||||||||
| 114 | 1998 | return Game::Checkers::Notation::fen_from_position($self->position, $turn); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 64 | ▶ | sub from_fen { | ||||||||||||||||||||||||||||||
|
Subroutine
from_fen: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 118 | 64 | my ($class, $fen, %opt) = @_; | |||||||||||||||||||||||||||||||
| 119 | 64 | my ($position, $turn) = Game::Checkers::Notation::position_from_fen($fen, %opt); | |||||||||||||||||||||||||||||||
| 120 | 64 | my $board = $class->new(position => $position); | |||||||||||||||||||||||||||||||
| 121 | 64 | ▶ | return wantarray ? ($board, $turn) : $board; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 28 | 1; | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||||||