lib/TUI/Gadgets/PuzzleView.pm

filter: Statementstmt 27.2% 58 / 213 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 15 / 27 Podpod 81.8% 9 / 11 totaltotal 24.9% 82 / 329 CC 44 SCAR 41.6
CC
44
Cov
20%
moveKey
210.0
draw
56.0
scramble
56.0
CRAP 63.9
« lib/TUI/Views/View.pm ↑ Summary lib/TUI/StdDlg/FileDialog.pm »
1 package TUI::Gadgets::PuzzleView;
2 # ABSTRACT: view of a sliding tile puzzle
3
4 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 5 our $VERSION = '2.000003';
9 5 $VERSION =~ tr/_//d;
10 5 our $AUTHORITY = 'cpan:BRICKPOOL';
11
12 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 our @EXPORT = qw(
14 TPuzzleView
15 new_TPuzzleView
16 );
17
18 5 ▶use TUI::toolkit;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use TUI::toolkit::Types qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
20 :is
21 :types
22 5 );
23
24 5 ▶use TUI::Drivers::Const qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
25 :evXXXX
26 kbDown
27 kbLeft
28 kbRight
29 kbUp
30 5 );
31 5 ▶use TUI::Drivers::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use TUI::Gadgets::Const qw( cpPuzzlePalette );
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use TUI::Objects::Rect;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use TUI::Objects::Point;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use TUI::Views::Const qw( ofSelectable );
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use TUI::Views::DrawBuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 ▶use TUI::Views::Palette;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use TUI::Views::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
39
40 sub TPuzzleView() { __PACKAGE__ }
41 sub name() { 'TPuzzleView' }
42 0 ▶sub new_TPuzzleView { __PACKAGE__->from(@_) }
Subroutine new_TPuzzleView: not called CC=1 SCAR=6.9
Pod documented
43
44 5 extends TView;
45
46 5 my @boardStart = split //, 'ABCDEFGHIJKLMNO ';
47 5 my @solution = split //, 'ABCDEFGHIJKLMNO ';
48 5 my @map = ( 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 );
49
50 # private attributes
51 5 has board => ( is => 'bare' );
52 5 has moves => ( is => 'bare' );
53 5 has solved => ( is => 'bare' );
54
55 # private methods (anonymous subs, as in TEventViewer)
56 5 my ( $blankIndex );
57
58 0 ▶sub BUILD { # void (\%args)
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod documented
59 0 my ( $self, $args ) = @_;
60 0 ▶ assert ( @_ == 2 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
61 0 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 0 0
62
63 0 $self->{options} |= ofSelectable;
64 0 $self->{board} = [ @boardStart ];
65 0 $self->{moves} = 0;
66 0 $self->{solved} = 0;
67 0 $self->scramble();
68 0 return;
69 }
70
71 0 ▶sub getPalette { # $palette ()
Subroutine getPalette: not called CC=1 SCAR=6.9
Pod documented
72 0 state $sig = signature(
73 method => Object,
74 pos => [],
75 );
76 0 my ( $self ) = $sig->( @_ );
77 0 state $palette = TPalette->new(
78 data => cpPuzzlePalette,
79 size => length( cpPuzzlePalette ),
80 );
81 0 return $palette->clone();
82 }
83
84 0 ▶sub handleEvent { # void ($event)
Subroutine handleEvent: not called CC=6 SCAR=37.4
Pod documented
85 0 state $sig = signature(
86 method => Object,
87 pos => [Object],
88 );
89 0 my ( $self, $event ) = $sig->( @_ );
90 0 $self->SUPER::handleEvent( $event );
91
92 0 ▶ if ( $self->{solved} && ( $event->{what} & ( evKeyboard | evMouse ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"solved"} and $event->{"what"} & 31) 0 0 0
Condition0%0/3
$self->{"solved"} and $event->{"what"} & 31
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"solved"} and $event->{"what"} & 31
$self->{"solved"} $event->{"what"} & 31
Truth table0%0/3
A and B
A= $self->{"solved"}B= $event->{"what"} & 31
ABresult
0X0
100
111
93 0 $self->scramble();
94 0 $self->clearEvent( $event );
95 }
96
97 0 SWITCH: for ( $event->{what} ) {
98 0 ▶ evMouseDown == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { $self->moveTile($event->{"mouse"}{"where"}); $self->clearEvent($event); $self->winCheck; last } 0 0 0
99 0 $self->moveTile( $event->{mouse}{where} );
100 0 $self->clearEvent( $event );
101 0 $self->winCheck();
102 0 last;
103 };
104
105 0 ▶ evKeyDown == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
16 == $_ and do { $self->moveKey($event->{"keyDown"}{"keyCode"}); $self->clearEvent($event); $self->winCheck; last } 0 0 0
106 0 $self->moveKey( $event->{keyDown}{keyCode} );
107 0 $self->clearEvent( $event );
108 0 $self->winCheck();
109 0 last;
110 };
111 } #/ SWITCH: for ( $event->{what} )
112 0 return;
113 }
114
115 0 ▶sub draw { # void ()
Subroutine draw: not called CC=7 SCAR=40.3
Pod documented
116 0 state $sig = signature(
117 method => Object,
118 pos => [],
119 );
120 0 my ( $self ) = $sig->( @_ );
121
122 0 my $colorBack = $self->getColor( 1 );
123 0 ▶ my @color = ( $colorBack, $self->{solved} ? $colorBack : $self->getColor( 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'solved'} ? : 0 0 0
124 0 my $buf = TDrawBuffer->new();
125
126 0 for my $i ( 0 .. 3 ) {
127 0 $buf->moveChar( 0, ' ', $colorBack, 18 );
128 0 ▶ $buf->moveStr( 13, 'Move', $colorBack ) if $i == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 1 0 0 0
129 0 ▶ $buf->moveStr( 14, sprintf( '%d', $self->{moves} ), $colorBack ) if $i == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 2 0 0 0
130 0 for my $j ( 0 .. 3 ) {
131 0 my $tile = $self->{board}[ $i * 4 + $j ];
132 0 ▶ my $attr = $tile eq ' '
Branch: 1 branch
BranchTrueFalseTotal
$tile eq ' ' ? : 0 0 0
133 ? $color[0]
134 : $color[ $map[ ord( $tile ) - ord( 'A' ) ] ];
135 0 $buf->moveStr( $j * 3, " $tile ", $attr );
136 }
137 0 $self->writeLine( 0, $i, 18, 1, $buf );
138 }
139 0 return;
140 }
141
142 0 ▶$blankIndex = sub { # $index ()
Subroutine __ANON__: not called CC=5 SCAR=34.0
143 0 my ( $self ) = @_;
144 0 ▶ assert ( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
145 0 ▶ assert ( is_Object $self );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_Object($self) 0 0 0
146 0 my $i = 0;
147 0 ▶ $i++ while $i < 16 && $self->{board}[ $i ] ne ' ';
Branch: 1 branch
BranchTrueFalseTotal
$i < 16 and $self->{"board"}[$i] ne " " and do { ++$i } 0 0 0
Condition0%0/3
$i < 16 and $self->{"board"}[$i] ne " "
!ll&&!rl&&r
000
MC/DC0%0/2
$i < 16 and $self->{"board"}[$i] ne " "
$i < 16 $self->{"board"}[$i] ne " "
Truth table0%0/3
A and B
A= $i < 16B= $self->{"board"}[$i] ne " "
ABresult
0X0
100
111
148 0 return $i;
149 5 };
150
151 0 ▶sub moveKey { # void ($key)
Subroutine moveKey: not called CC=14 SCAR=53.5
Pod documented
152 0 state $sig = signature(
153 method => Object,
154 pos => [Int],
155 );
156 0 my ( $self, $key ) = $sig->( @_ );
157
158 0 my $board = $self->{board};
159 0 my $i = $blankIndex->( $self );
160 0 my $x = $i % 4;
161 0 my $y = int( $i / 4 );
162
163 0 SWITCH: for ( $key ) {
164 0 ▶ kbDown == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
20480 == $_ and do { if ($y > 0) { $board->[$i] = $$board[$i - 4]; $$board[$i - 4] = " "; ++$self->{"moves"} if $self->{"moves"} < 1000; } ; last } 0 0 0
165 0 ▶ if ( $y > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y > 0) 0 0 0
166 0 $board->[ $i ] = $board->[ $i - 4 ];
167 0 $board->[ $i - 4 ] = ' ';
168 0 ▶ $self->{moves}++ if $self->{moves} < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"moves"} < 1000 0 0 0
169 }
170 0 last;
171 };
172
173 0 ▶ kbUp == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
18432 == $_ and do { if ($y < 3) { $board->[$i] = $$board[$i + 4]; $$board[$i + 4] = " "; ++$self->{"moves"} if $self->{"moves"} < 1000; } ; last } 0 0 0
174 0 ▶ if ( $y < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y < 3) 0 0 0
175 0 $board->[ $i ] = $board->[ $i + 4 ];
176 0 $board->[ $i + 4 ] = ' ';
177 0 ▶ $self->{moves}++ if $self->{moves} < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"moves"} < 1000 0 0 0
178 }
179 0 last;
180 };
181
182 0 ▶ kbRight == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
19712 == $_ and do { if ($x > 0) { $board->[$i] = $$board[$i - 1]; $$board[$i - 1] = " "; ++$self->{"moves"} if $self->{"moves"} < 1000; } ; last } 0 0 0
183 0 ▶ if ( $x > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) 0 0 0
184 0 $board->[ $i ] = $board->[ $i - 1 ];
185 0 $board->[ $i - 1 ] = ' ';
186 0 ▶ $self->{moves}++ if $self->{moves} < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"moves"} < 1000 0 0 0
187 }
188 0 last;
189 };
190
191 0 ▶ kbLeft == $_ and do {
Branch: 1 branch
BranchTrueFalseTotal
19200 == $_ and do { if ($x < 3) { $board->[$i] = $$board[$i + 1]; $$board[$i + 1] = " "; ++$self->{"moves"} if $self->{"moves"} < 1000; } ; last } 0 0 0
192 0 ▶ if ( $x < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 3) 0 0 0
193 0 $board->[ $i ] = $board->[ $i + 1 ];
194 0 $board->[ $i + 1 ] = ' ';
195 0 ▶ $self->{moves}++ if $self->{moves} < 1000;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"moves"} < 1000 0 0 0
196 }
197 0 last;
198 };
199 } #/ SWITCH: for ( $key )
200
201 0 $self->drawView();
202 0 return;
203 }
204
205 0 ▶sub moveTile { # void ($point)
Subroutine moveTile: not called CC=6 SCAR=37.4
Pod documented
206 0 state $sig = signature(
207 method => Object,
208 pos => [Object],
209 );
210 0 my ( $self, $point ) = $sig->( @_ );
211
212 0 my $p = $self->makeLocal( $point );
213 0 my $i = $blankIndex->( $self );
214 0 my $x = int( $p->{x} / 3 );
215 0 my $y = $p->{y};
216
217 0 SWITCH: for ( $y * 4 + $x - $i ) {
218 0 ▶ -4 == $_ and do { # piece moves down
Branch: 1 branch
BranchTrueFalseTotal
-4 == $_ and do { $self->moveKey(20480); last } 0 0 0
219 0 $self->moveKey( kbDown );
220 0 last;
221 };
222
223 0 ▶ -1 == $_ and do { # piece moves right
Branch: 1 branch
BranchTrueFalseTotal
-1 == $_ and do { $self->moveKey(19712); last } 0 0 0
224 0 $self->moveKey( kbRight );
225 0 last;
226 };
227
228 0 ▶ 1 == $_ and do { # piece moves left
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { $self->moveKey(19200); last } 0 0 0
229 0 $self->moveKey( kbLeft );
230 0 last;
231 };
232
233 0 ▶ 4 == $_ and do { # piece moves up
Branch: 1 branch
BranchTrueFalseTotal
4 == $_ and do { $self->moveKey(18432); last } 0 0 0
234 0 $self->moveKey( kbUp );
235 0 last;
236 };
237 } #/ SWITCH: for ( $y * 4 + $x - $i )
238
239 0 $self->drawView();
240 0 return;
241 }
242
243 0 ▶sub scramble { # void ()
Subroutine scramble: not called CC=7 SCAR=40.3
Pod documented
244 0 state $sig = signature(
245 method => Object,
246 pos => [],
247 );
248 0 my ( $self ) = $sig->( @_ );
249
250 0 $self->{moves} = 0;
251 0 $self->{solved} = 0;
252 do {
253 0 SWITCH: for ( int( rand 4 ) ) {
254 0 ▶ 0 == $_ and do { $self->moveKey( kbUp ); last; };
Branch: 1 branch
BranchTrueFalseTotal
0 == $_ and do { $self->moveKey(18432); last } 0 0 0
255 0 ▶ 1 == $_ and do { $self->moveKey( kbDown ); last; };
Branch: 1 branch
BranchTrueFalseTotal
1 == $_ and do { $self->moveKey(20480); last } 0 0 0
256 0 ▶ 2 == $_ and do { $self->moveKey( kbRight ); last; };
Branch: 1 branch
BranchTrueFalseTotal
2 == $_ and do { $self->moveKey(19712); last } 0 0 0
257 0 ▶ 3 == $_ and do { $self->moveKey( kbLeft ); last; };
Branch: 1 branch
BranchTrueFalseTotal
3 == $_ and do { $self->moveKey(19200); last } 0 0 0
258 }
259 0 ▶ } while ( $self->{moves}++ <= 500 );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"moves"}++ <= 500 and do { do { SWITCH: foreach $_ (int rand 4) { 0 == $_ and do { $self->moveKey(18432); last }; 1 == $_ and do { $self->moveKey(20480); last }; 2 == $_ and do { $self->moveKey(19712); last }; 3 == $_ and do { $self->moveKey(19200); last }; } } } 0 0 0
260
261 0 $self->{moves} = 0;
262 0 $self->drawView();
263 0 return;
264 }
265
266 0 ▶sub winCheck { # void ()
Subroutine winCheck: not called CC=3 SCAR=24.8
Pod documented
267 0 state $sig = signature(
268 method => Object,
269 pos => [],
270 );
271 0 my ( $self ) = $sig->( @_ );
272
273 0 my $solved = 1;
274 0 for my $i ( 0 .. 15 ) {
275 0 ▶ if ( $self->{board}[ $i ] ne $solution[ $i ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"board"}[$i] ne $solution[$i]) 0 0 0
276 0 $solved = 0;
277 0 last;
278 }
279 }
280 0 $self->{solved} = $solved;
281 0 $self->drawView();
282 0 return;
283 }
284
285 0 ▶sub write :method { # void ($os)
Subroutine write: not called CC=1 SCAR=6.9
Pod undocumented
286 0 state $sig = signature(
287 method => Object,
288 pos => [Object],
289 );
290 0 my ( $self, $os ) = $sig->( @_ );
291 0 $self->SUPER::write( $os );
292 $os->put(
293 0 join( '', @{ $self->{board} } ),
294 $self->{moves},
295 $self->{solved},
296 0 );
297 0 return;
298 }
299
300 0 ▶sub read :method { # $self ($is)
Subroutine read: not called CC=1 SCAR=6.9
Pod undocumented
301 0 state $sig = signature(
302 method => Object,
303 pos => [Object],
304 );
305 0 my ( $self, $is ) = $sig->( @_ );
306 0 $self->SUPER::read( $is );
307 0 $self->{board} = [ split //, $is->get() ];
308 0 $self->{moves} = $is->get();
309 0 $self->{solved} = $is->get();
310 0 return $self;
311 }
312
313 5 1
314
315 __END__
« lib/TUI/Views/View.pm ↑ Summary lib/TUI/StdDlg/FileDialog.pm »