| 1 | package Games::Sokoban::Controller; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Games::Sokoban; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 10 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||
| 11 | 0 | bless { model => Games::Sokoban->new, @_ }, $class; | |||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub set_data { | ||||||||||||||||||||||||||||||
|
Subroutine
set_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 15 | 0 | my ($self, $data, $format) = @_; | |||||||||||||||||||||||||||||||
| 16 | 0 | $self->{model}->data($data, $format); | |||||||||||||||||||||||||||||||
| 17 | 0 | $self->{level_id} = $self->{model}->normalise; | |||||||||||||||||||||||||||||||
| 18 | 0 | $self->{model}->data($data, $format); | |||||||||||||||||||||||||||||||
| 19 | 0 | $self->reset; | |||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub level_id { shift->{level_id} } | ||||||||||||||||||||||||||||||
|
Subroutine
level_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub reset { | ||||||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 26 | 0 | my $model = $self->{model}; | |||||||||||||||||||||||||||||||
| 27 | 0 | $self->{data} = $model->data; | |||||||||||||||||||||||||||||||
| 28 | 0 | $self->{size} = [$model->{w}, $model->{h}]; | |||||||||||||||||||||||||||||||
| 29 | 0 | $self->{pos} = [$model->start]; | |||||||||||||||||||||||||||||||
| 30 | 0 | $self->{replaced} = []; | |||||||||||||||||||||||||||||||
| 31 | 0 | $self->{step} = 0; | |||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub size { @{shift->{size}} } | ||||||||||||||||||||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub width { shift->{size}[0] } | ||||||||||||||||||||||||||||||
|
Subroutine
width: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub height { shift->{size}[1] } | ||||||||||||||||||||||||||||||
|
Subroutine
height: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 39 | 0 | my ($self, $pos) = @_; | |||||||||||||||||||||||||||||||
| 40 | 0 | substr($self->{data}, $self->_pos($pos), 1); | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub _rel { | ||||||||||||||||||||||||||||||
|
Subroutine
_rel: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 44 | 0 | my ($self, $rel_pos) = @_; | |||||||||||||||||||||||||||||||
| 45 | 0 | $rel_pos->[0] += $self->{pos}[0]; | |||||||||||||||||||||||||||||||
| 46 | 0 | $rel_pos->[1] += $self->{pos}[1]; | |||||||||||||||||||||||||||||||
| 47 | 0 | $self->get($rel_pos); | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _replace { | ||||||||||||||||||||||||||||||
|
Subroutine
_replace: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 51 | 0 | my ($self, $pos, $char) = @_; | |||||||||||||||||||||||||||||||
| 52 | 0 | $pos->[0] += $self->{pos}[0]; | |||||||||||||||||||||||||||||||
| 53 | 0 | $pos->[1] += $self->{pos}[1]; | |||||||||||||||||||||||||||||||
| 54 | 0 | substr($self->{data}, $self->_pos($pos), 1, $char); | |||||||||||||||||||||||||||||||
| 55 | 0 | push @{$self->{replaced}}, $pos; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub _pos { | ||||||||||||||||||||||||||||||
|
Subroutine
_pos: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 59 | 0 | my ($self, $pos) = @_; | |||||||||||||||||||||||||||||||
| 60 | 0 | my ($x, $y) = @$pos; | |||||||||||||||||||||||||||||||
| 61 | 0 | my ($w, $h) = @{$self->{size}}; | |||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($x < 0) { $x = 0; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($y < 0) { $y = 0; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($x >= $w) { $x = $w - 1; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($y >= $h) { $y = $h - 1; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 0 | $x + $y * ($w + 1); # +1 for "\n" | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub _dump { | ||||||||||||||||||||||||||||||
|
Subroutine
_dump: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 71 | 0 | my $data = $self->{data}; | |||||||||||||||||||||||||||||||
| 72 | 0 | my $left = $data =~ /([\.\+])/; | |||||||||||||||||||||||||||||||
| 73 | 0 | my $done = $data =~ /(\*)/; | |||||||||||||||||||||||||||||||
| 74 | my $dump = join "", | ||||||||||||||||||||||||||||||||
| 75 | $data, "\n", | ||||||||||||||||||||||||||||||||
| 76 | "pos: (", $self->{pos}[0], ", ", $self->{pos}[1], ")\n", | ||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | "step: ", $self->{step}, "\n", | ||||||||||||||||||||||||||||||
|
Condition0%0/4
$left || 0
$done || 0
MC/DC0%0/1
$left || 0
$left
MC/DC0%0/1
$done || 0
$done
Truth table0%0/2
A
A= $left
Truth table0%0/2
A
A= $done
| |||||||||||||||||||||||||||||||||
| 78 | "left: ", ($left || 0), "\n", | ||||||||||||||||||||||||||||||||
| 79 | "done: ", ($done || 0), "\n"; | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 0 | return $dump; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||||||
| 85 | 0 | print STDERR "\n", $dump; | |||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _move { | ||||||||||||||||||||||||||||||
|
Subroutine
_move: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||||
| 90 | 0 | my ($self, $delta, $direction) = @_; | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 0 | my ($x, $y) = @{$self->{pos}}; | |||||||||||||||||||||||||||||||
| 93 | 0 | my ($dx, $dy) = @$delta; | |||||||||||||||||||||||||||||||
| 94 | 0 | my $me = $self->_rel([0, 0]); | |||||||||||||||||||||||||||||||
| 95 | 0 | my $dest = $self->_rel([$dx, $dy]); | |||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 0 | $self->{direction} = $direction; | |||||||||||||||||||||||||||||||
| 98 | 0 | @{$self->{replaced}} = (); | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 0 | my $moved; | |||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($dest eq ' ' or $dest eq '.') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$dest eq " " or $dest eq "."
MC/DC0%0/2
$dest eq " " or $dest eq "."
$dest eq " "
$dest eq "."
Truth table0%0/3
A or B
A= $dest eq " "B= $dest eq "."
| |||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $self->_replace([0, 0] => ($me eq '@' ? ' ' : '.')); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $self->_replace([$dx, $dy] => ($dest eq ' ' ? '@' : '+')); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 104 | 0 | $moved = 1; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | ▶ | elsif ($dest eq '$' or $dest eq '*') { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$dest eq "\$" or $dest eq "*"
MC/DC0%0/2
$dest eq "\$" or $dest eq "*"
$dest eq "\$"
$dest eq "*"
Truth table0%0/3
A or B
A= $dest eq "\$"B= $dest eq "*"
| |||||||||||||||||||||||||||||||||
| 107 | 0 | my $next = $self->_rel([$dx * 2, $dy * 2]); | |||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ($next eq ' ' or $next eq '.') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$next eq " " or $next eq "."
MC/DC0%0/2
$next eq " " or $next eq "."
$next eq " "
$next eq "."
Truth table0%0/3
A or B
A= $next eq " "B= $next eq "."
| |||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $self->_replace([0, 0] => ($me eq '@' ? ' ' : '.')); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $self->_replace([$dx, $dy] => ($dest eq '$' ? '@' : '+')); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $self->_replace([$dx * 2, $dy * 2] => ($next eq ' ' ? '$' : '*')); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | 0 | $moved = 1; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||
| 115 | 0 | $self->_debug("blocked"); | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||||||
| 119 | 0 | $self->_debug("wall"); | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ($moved) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | 0 | $self->{step}++; | |||||||||||||||||||||||||||||||
| 123 | 0 | $self->{pos} = [$x + $dx, $y + $dy]; | |||||||||||||||||||||||||||||||
| 124 | 0 | return @{$self->{replaced}}; | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | 0 | return; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub go_right { shift->_move([1, 0], 'right') } | ||||||||||||||||||||||||||||||
|
Subroutine
go_right: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub go_left { shift->_move([-1, 0], 'left') } | ||||||||||||||||||||||||||||||
|
Subroutine
go_left: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub go_up { shift->_move([0, -1], 'up') } | ||||||||||||||||||||||||||||||
|
Subroutine
go_up: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub go_down { shift->_move([0, 1], 'down') } | ||||||||||||||||||||||||||||||
|
Subroutine
go_down: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub direction { shift->{direction} || 'left' } | ||||||||||||||||||||||||||||||
|
Subroutine
direction: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub solved { | ||||||||||||||||||||||||||||||
|
Subroutine
solved: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 138 | 0 | my $left = $self->{data} =~ /([\.\+\$])/; | |||||||||||||||||||||||||||||||
| 139 | 0 | !$left; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub _debug { | ||||||||||||||||||||||||||||||
|
Subroutine
_debug: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 143 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | print STDERR @_, "\n" if $self->{debug}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 1 | 1; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||