lib/Games/Sokoban/Controller.pm

filter: Statementstmt 10.8% 11 / 101 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 13.0% 3 / 23 Podpod 100% 14 / 14 totaltotal 14.6% 28 / 191 CC 22 SCAR 41.0
CC
22
Cov
8%
_move
182.0
_pos
30.0
_dump
20.0
CRAP 60.3
↑ Summary
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
BranchTrueFalseTotal
if ($x < 0) 0 0 0
63 0 ▶ if ($y < 0) { $y = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 0) 0 0 0
64 0 ▶ if ($x >= $w) { $x = $w - 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($x >= $w) 0 0 0
65 0 ▶ if ($y >= $h) { $y = $h - 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= $h) 0 0 0
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
l!l
00
$done || 0
l!l
00
MC/DC0%0/1
$left || 0
$left
MC/DC0%0/1
$done || 0
$done
Truth table0%0/2
A
A= $left
Aresult
11
00
Truth table0%0/2
A
A= $done
Aresult
11
00
78 "left: ", ($left || 0), "\n",
79 "done: ", ($done || 0), "\n";
80
81 0 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 0 0
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
BranchTrueFalseTotal
if ($dest eq ' ' or $dest eq '.') { } 0 0 0
elsif ($dest eq '$' or $dest eq '*') { } 0 0 0
Condition0%0/3
$dest eq " " or $dest eq "."
l!l&&r!l&&!r
000
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 "."
ABresult
1X1
011
000
102 0 ▶ $self->_replace([0, 0] => ($me eq '@' ? ' ' : '.'));
Branch: 1 branch
BranchTrueFalseTotal
$me eq '@' ? : 0 0 0
103 0 ▶ $self->_replace([$dx, $dy] => ($dest eq ' ' ? '@' : '+'));
Branch: 1 branch
BranchTrueFalseTotal
$dest eq ' ' ? : 0 0 0
104 0 $moved = 1;
105 }
106 ▶ elsif ($dest eq '$' or $dest eq '*') {
Condition0%0/3
$dest eq "\$" or $dest eq "*"
l!l&&r!l&&!r
000
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 "*"
ABresult
1X1
011
000
107 0 my $next = $self->_rel([$dx * 2, $dy * 2]);
108 0 ▶ if ($next eq ' ' or $next eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq ' ' or $next eq '.') { } 0 0 0
Condition0%0/3
$next eq " " or $next eq "."
l!l&&r!l&&!r
000
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 "."
ABresult
1X1
011
000
109 0 ▶ $self->_replace([0, 0] => ($me eq '@' ? ' ' : '.'));
Branch: 1 branch
BranchTrueFalseTotal
$me eq '@' ? : 0 0 0
110 0 ▶ $self->_replace([$dx, $dy] => ($dest eq '$' ? '@' : '+'));
Branch: 1 branch
BranchTrueFalseTotal
$dest eq '$' ? : 0 0 0
111 0 ▶ $self->_replace([$dx * 2, $dy * 2] => ($next eq ' ' ? '$' : '*'));
Branch: 1 branch
BranchTrueFalseTotal
$next eq ' ' ? : 0 0 0
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
BranchTrueFalseTotal
if ($moved) 0 0 0
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
BranchTrueFalseTotal
+(shift())->{"direction"} or "left" 0 0 0
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
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
145 }
146
147 1 1;
148
149 __END__
↑ Summary