| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Games::Board::Grid; | ||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||
| 6 | 3 | $Games::Board::Grid::VERSION = '1.013'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use parent qw(Games::Board); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: a grid-shaped gameboard | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub new { | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | croak "no size given to construct grid" unless $args{size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | $args{size} = [ ($args{size}) x 2 ] unless ref $args{size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | my $board = { size => $args{size} }; | |||||||||||||||||||||||||||
| 23 | 2 | bless $board => $class; | |||||||||||||||||||||||||||
| 24 | 2 | $board->init; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub init { | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | my $board = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | $board->{spaces} = {}; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | for my $x (0 .. ($board->{size}[0] - 1)) { | |||||||||||||||||||||||||||
| 34 | 16 | for my $y (0 .. ($board->{size}[1] - 1)) { | |||||||||||||||||||||||||||
| 35 | 128 | my $id = $board->index2id([$x,$y]); | |||||||||||||||||||||||||||
| 36 | 128 | $board->{spaces}{$id} = Games::Board::Grid::Space->new(id => $id, board => $board); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | $board; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 68 | ▶ | sub size { (shift)->{size} } | ||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 21 | ▶ | sub id2index { [ split(/ /,$_[1]) ] } | ||||||||||||||||||||||||||
|
Subroutine
id2index: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 79 | ▶ | sub index2id { join(q{ }, @{$_[1]}) } | ||||||||||||||||||||||||||
|
Subroutine
index2id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub space { | ||||||||||||||||||||||||||||
| 54 | 48 | ▶ | my $board = shift; | ||||||||||||||||||||||||||
|
Subroutine
space: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 48 | my $id = shift; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 48 | return $board->{spaces}{$id}; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub add_space { croak "spaces can't be added to grid board" } | ||||||||||||||||||||||||||
|
Subroutine
add_space: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | package Games::Board::Grid::Space; | ||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | 3 | $Games::Board::Grid::Space::VERSION = '1.013'; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 3 | ▶ | use parent qw(Games::Board::Space); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub dir_id { | ||||||||||||||||||||||||||||
| 71 | 42 | ▶ | my ($self, $dir) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dir_id: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 72 | 42 | ▶ | return unless ref $dir eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 42 | my $pos = $self->board->id2index($self->id); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 42 | my $newpos = [ | |||||||||||||||||||||||||||
| 77 | $pos->[0] + $dir->[0], | ||||||||||||||||||||||||||||
| 78 | $pos->[1] + $dir->[1] | ||||||||||||||||||||||||||||
| 79 | ]; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 42 | ▶ | return if $newpos->[0] < 0 or $newpos->[1] < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$newpos->[0] < 0 or $newpos->[1] < 0
MC/DC100%2/2
$newpos->[0] < 0 or $newpos->[1] < 0
$newpos->[0] < 0
$newpos->[1] < 0
Truth table100%3/3
A or B
A= $newpos->[0] < 0B= $newpos->[1] < 0
| |||||||||||||||||||||||||||||
| 82 | return | ||||||||||||||||||||||||||||
| 83 | 36 | ▶ | if $newpos->[0] >= $self->board->size->[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$newpos->[0] >= $self->board->size->[0] or $newpos->[1] >= $self->board->size->[1]
MC/DC100%2/2
$newpos->[0] >= $self->board->size->[0] or $newpos->[1] >= $self->board->size->[1]
$newpos->[0] >= $self->board->size->[0]
$newpos->[1] >= $self->board->size->[1]
Truth table100%3/3
A or B
A= $newpos->[0] >= $self->board->size->[0]B= $newpos->[1] >= $self->board->size->[1]
| |||||||||||||||||||||||||||||
| 84 | or $newpos->[1] >= $self->board->size->[1]; | ||||||||||||||||||||||||||||
| 85 | 30 | return $self->board->index2id($newpos); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | "Family fun night!"; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||