-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsokosolver.pl
executable file
·868 lines (743 loc) · 29 KB
/
sokosolver.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
#!/usr/local/bin/perl
# Sokoban Solver
#
# Author: Stephen Riehm
# Date: 2008-10-20
# Usage:
# Regression Tests: sokosolver.pl
# All puzzles in a file: sokosolver.pl filename
# Selected puzzles in a file: sokosolver.pl filename <range>
#
# Range can be any combination of:
# comma separated numbers
# ranges of the form {begin}-{end}
#
# eg: 1,3-5,8 would solve puzzles 1, 3, 4, 5 and 8
#
# Known limitations:
# it's not necessarily very quick
# the solution can't have more than about 80 moves (deep recursion)
# Puzzle Map Definition:
#
# map characters:
# <space> empty
# # wall
# . goal
# $ box
# * box on goal
# @ mover
# + mover on goal
use strict;
use warnings;
use FindBin qw( $RealBin $Script ); # $Script is the name of the program
use lib "$RealBin/../lib"; # where to find private libraries
# use YAML; # VERY useful for debugging
my $opposite = {
'up' => 'down',
'down' => 'up',
'left' => 'right',
'right' => 'left',
};
test_suite::run() unless @ARGV;
my @range_specs = ();
for( my $arg = 0; $arg < @ARGV; $arg++ )
{
if( $ARGV[$arg] =~ /^[\d,-]+$/ )
{
push( @range_specs, splice( @ARGV, $arg, 1 ) );
}
}
my $puzzles = importer::import_puzzles( <> );
my @puzzle_range = ();
foreach my $range_spec ( @range_specs )
{
foreach my $group ( split( /,/, $range_spec ) )
{
if( $group =~ /^(\d*)-(\d*)$/ )
{
my $start = $1 || 1;
my $end = $2 || @{$puzzles};
push( @puzzle_range, $start-1 .. $end-1 );
}
else
{
push( @puzzle_range, $group-1 );
}
}
}
@puzzle_range = ( 0 .. $#{$puzzles} ) unless @puzzle_range;
foreach my $puzzle ( @{$puzzles}[@puzzle_range] )
{
print "\n";
printf "Solving Puzzle Number %d\n", $puzzle->{'id'};
$puzzle->dump();
my $start = time();
$puzzle->{'required_steps'} = 0;
$puzzle->{'previous_score'} = 0;
my @solution = $puzzle->solve();
print "\n";
my $duration = time() - $start;
$puzzle->replay( @solution );
printf "Solution took %d seconds and %d tries to find a %d step solution\n", $duration, $puzzle->{'required_steps'}, scalar( @solution );
}
exit 0;
package importer;
#
# import all the puzzles in a puzzle file for solving
#
sub import_puzzles
{
my $puzzles = [];
my $row = 0;
my $column = 0;
my $puzzle = undef
my $mover = undef;
foreach my $line ( @_ )
{
chomp( $line );
# skip non-sokoban map lines :-)
if( $line !~ /^\s*#(\s*[#\.\$\*\@\+]*\s*)*#\s*$/ )
{
if( $puzzle )
{
push( @{$puzzles}, $puzzle );
$puzzle = undef;
}
next;
}
if( ! $puzzle )
{
$puzzle = puzzle->new(
'id' => @{$puzzles} + 1,
'width' => 0,
'score' => 0,
'table' => [],
'blocks' => [],
'mover' => undef,
);
}
$puzzle->push_row();
foreach my $import_cell ( split( '', $line ) )
{
my $cell_display = ' ';
$cell_display = '#' if $import_cell =~ /#/;
$cell_display = '.' if $import_cell =~ /[\.\*\+]/;
my $cell = cell->new( 'display' => $cell_display, );
$puzzle->push_cell( $cell );
if( $import_cell =~ /[\$*]/ )
{
my $block = block->new( 'id' => scalar( @{$puzzle->{'blocks'}} ), );
push( @{$puzzle->{'blocks'}}, $block );
$puzzle->move_block_to( $block => $cell );
}
if( $import_cell =~ /[\@\+]/ )
{
my $mover = mover->new();
$puzzle->{'mover'} = $mover;
$puzzle->place_mover_in( $cell );
}
}
}
push( @{$puzzles}, $puzzle ) if $puzzle;
$_->setup() foreach @{$puzzles};
return $puzzles;
}
package puzzle;
sub new
{
my $class = shift;
my $puzzle = bless { @_ }, $class;
$puzzle->{'table'} = [];
$puzzle->{'cells'} = [];
$puzzle->{'blocks'} = [];
$puzzle->{'last_dump'} = time();
return $puzzle;
}
sub push_row
{
my $puzzle = shift;
push( @{$puzzle->{'table'}}, [] );
}
sub push_cell
{
my $puzzle = shift;
my $cell = shift;
my $table = $puzzle->{'table'};
push( @{$table->[-1]}, $cell );
if( ! $cell->is_wall() )
{
$cell->{'id'} = @{$puzzle->{'cells'}};
push( @{$puzzle->{'cells'}}, $cell );
}
my $row = $#{$table};
my $column = $#{$table->[$row]};
$puzzle->{'width'} = $column if $column > $puzzle->{'width'};
$cell->connect(
'up' => $row ? $table->[$row-1][$column] : undef,
'left' => $column ? $table->[$row][$column-1] : undef,
);
}
sub setup
{
my $puzzle = shift;
$puzzle->{'top_score'} = ( 1 << scalar( @{$puzzle->{'blocks'}} ) ) - 1;
$puzzle->mark_corners();
$puzzle->setup_edge_row_groups();
$puzzle->{'mover'}->{'cell'}->set_active();
$puzzle->{'original_state'} = $puzzle->state();
}
# Corners are bad new - once you've pushed a box into one, you can never get it out again
# Identify and mark corners so that they can be avoided
# Note: the is_bad routine has an extra check for a goal being in a corner
sub mark_corners
{
my $puzzle = shift;
foreach my $row ( @{$puzzle->{'table'}} )
{
foreach my $cell ( @{$row} )
{
next unless $cell->{'up'} and $cell->{'down'} and $cell->{'left'} and $cell->{'right'};
$cell->{'is_corner'} = ( $cell->{'up'}->is_wall() || $cell->{'down'}->is_wall() )
&& ( $cell->{'left'}->is_wall() || $cell->{'right'}->is_wall() );
}
}
}
# optimisation:
# edge-rows are almost as bad as corners - boxes can't be moved away from such rows.
# This routine identifies groups of cells which can trap boxes, and limits
# the number of boxes in that row to the number of goals.
sub setup_edge_row_groups
{
my $puzzle = shift;
foreach my $start_cell ( @{$puzzle->{'cells'}} )
{
next unless $start_cell->{'is_corner'};
next if $start_cell->{'down'}->is_wall() and $start_cell->{'right'}->is_wall();
DIRECTION:
foreach my $direction ( qw( down right ) )
{
my $check_cell = $start_cell;
my @wall_sides = ( $direction eq 'down' ) ? qw( left right ) : qw( up down );
my $group = {};
$group->{'cells'} = [];
$group->{'goals'} = 0;
while( $check_cell and not $check_cell->is_wall() )
{
next DIRECTION unless $check_cell->{$wall_sides[0]} and $check_cell->{$wall_sides[1]};
next DIRECTION unless $check_cell->{$wall_sides[0]}->is_wall() or $check_cell->{$wall_sides[1]}->is_wall();
$group->{'goals'}++ if $check_cell->is_goal();
push( @{$group->{'cells'}}, $check_cell );
$check_cell = $check_cell->{$direction};
}
# at no point was there a way to get a box away from this wall
# if it's more than 2 cells long, store it as a edge-group
if( @{$group->{'cells'}} > 2 )
{
push( @{$_->{'edge_groups'}}, $group ) foreach @{$group->{'cells'}};
}
}
}
}
sub move_block_to
{
my $puzzle = shift;
my $block = shift;
my $target = shift;
$puzzle->{'score'} &= ~$block->mask();
$block->move_to( $target );
$puzzle->{'score'} |= $block->mask() if $block->is_home();
}
sub move_block
{
my $puzzle = shift;
my $block = shift;
my $direction = shift;
my $old_block_cell = $block->{'cell'};
$puzzle->lift_mover();
$puzzle->{'score'} &= ~$block->mask();
$block->move( $direction );
$puzzle->{'score'} |= $block->mask() if $block->is_home();
$puzzle->place_mover_in( $old_block_cell );
}
sub possible_moves
{
my $puzzle = shift;
my @moves = ();
foreach my $block ( @{$puzzle->{'blocks'}} )
{
foreach my $direction ( $block->possible_moves() )
{
push( @moves, {
'block_id' => $block->{'id'},
'direction' => $direction,
}
);
}
}
return @moves;
}
sub lift_mover
{
my $puzzle = shift;
$puzzle->{'mover'}->lift_up();
}
sub place_mover_in
{
my $puzzle = shift;
my $target_cell = shift;
$puzzle->{'mover'}->move_to( $target_cell );
}
sub is_solved
{
my $puzzle = shift; return $puzzle->{'score'} == $puzzle->{'top_score'};
}
sub solve
{
my $puzzle = shift;
my $level = shift || 0;
my $state = $puzzle->state(); # used for recovering the exact state
my $state_id = $puzzle->state_id(); # state_id doesn't specify exactly where the mover is
my @trace = ();
return( 'solved' ) if $puzzle->is_solved();
return if $level > 80; # prevent deep recursion - solution must have less than 80 moves
return if $puzzle->{'has_tried'}{$state_id}++;
$puzzle->{'required_steps'}++;
if( $puzzle->{'previous_score'} != $puzzle->{'score'} )
{
printf "%7d %s\r",
$puzzle->{required_steps},
join( '', map { $_ ? '#' : '-' } reverse sort split( '', sprintf( "%0*b", scalar @{$puzzle->{'blocks'}}, $puzzle->{'score'} ) ) )
;
}
my @possible_moves = $puzzle->possible_moves();
my $movement = undef;
while( ! $puzzle->is_solved()
and $movement = shift( @possible_moves )
)
{
my $block_id = $movement->{'block_id'};
my $direction = $movement->{'direction'};
my $block = $puzzle->{'blocks'}[$block_id];
$puzzle->move_block( $block => $direction );
my @successful_trace = $puzzle->solve( $level + 1 );
if( @successful_trace )
{
@trace = ( {
'block_id' => $block->{'id'},
'block_display' => $block->{'display'},
'direction' => $direction,
},
@successful_trace
);
}
else
{
$puzzle->restore( $state );
}
}
return @trace;
}
sub replay
{
my $puzzle = shift;
my @moves = @_;
$puzzle->restore( $puzzle->{'original_state'} );
pop( @moves ); # 'solved' marker
foreach my $move ( @moves )
{
printf( "Move '%s' %s\n", $move->{'block_display'}, $move->{'direction'} );
$puzzle->move_block( $puzzle->{'blocks'}[$move->{'block_id'}], $move->{'direction'} );
# $puzzle->dump();
}
$puzzle->dump();
}
sub state_id
{
my $puzzle = shift;
return join( '', map { $_->has_block() ? '$' : ( $_->is_active() ? '+' : '-' ) } @{$puzzle->{'cells'}} );
}
sub state
{
my $puzzle = shift;
return join( ':', $puzzle->{'mover'}->{'cell'}{'id'}, map { $_->{'cell'}{'id'} } @{$puzzle->{'blocks'}} );
}
sub clear_cells
{
my $puzzle = shift;
foreach my $cell ( @{$puzzle->{'cells'}} )
{
if( $cell->{'contents'} )
{
$cell->{'contents'}{'cell'} = undef;
$cell->{'contents'} = undef;
}
$cell->{'is_active'} = 0;
# TODO: track edge_group status numerically - instead of traversing the lists over and over and over....
# if( $cell->{'edge_groups'} )
# {
# foreach my $edge_group ( @{$cell->{'edge_groups'}} )
# {
# $edge_group->{'blocks'} = 0;
# }
# }
}
}
sub restore
{
my $puzzle = shift;
my $recover_state = shift;
$puzzle->{'score'} = 0;
my @state = split( /:/, $recover_state );
$puzzle->clear_cells();
$puzzle->place_mover_in( $puzzle->{'cells'}[shift( @state )] );
$puzzle->move_block_to( $_ => $puzzle->{'cells'}[shift( @state )] ) foreach @{$puzzle->{'blocks'}};
}
sub dump
{
my $puzzle = shift;
my $long = shift || 0;
# printf "Puzzle: %d\n", $puzzle->{'id'};
foreach my $row ( @{$puzzle->{'table'}} )
{
# printf "%s\n", join( "", map { $_->display() } @{$row} );
# printf "%s\n", join( "", map { sprintf "%s%s%s%s%s", ( $_->{'left'} ? '<' : ' ' ), ( $_->{'up'} ? '^' : ' ' ), $_->display(), ( $_->{'down'} ? 'v' : ' ' ), ( $_->{'right'} ? '>' : ' ' ), } @{$row} );
my $cell = $row->[0];
do
{
if( $long )
{
printf( "%s%s%s[%2d]%s%s",
( $cell->{'left'} ? '<' : ' ' ),
( $cell->{'up'} ? '^' : ' ' ),
$cell->display(),
$cell->{'id'},
( $cell->{'down'} ? 'v' : ' ' ),
( $cell->{'right'} ? '>' : ' ' ),
);
}
else
{
printf( "%s", $cell->display() );
}
} while( $cell = $cell->{'right'} );
print "\n";
}
printf "Score: %s ( score: %d of %d )\n", ( $puzzle->is_solved() ? 'SOLVED' : 'unsolved' ), $puzzle->{'score'}, $puzzle->{'top_score'};
}
package cell;
sub new { my $class = shift; return bless { @_ }, $class; }
sub connect
{
my $cell = shift;
my $opts = { @_ };
my $up = $opts->{'up'} || undef;
my $left = $opts->{'left'} || undef;
$cell->{'up'} = $up if $up;
$up->{'down'} = $cell if $up;
$cell->{'left'} = $left if $left;
$left->{'right'} = $cell if $left;
}
sub set_active
{
my $cell = shift;
return if $cell->is_wall();
if( $cell->has_block() )
{
$cell->{'is_active'} = 0;
return;
}
return if $cell->is_active();
$cell->{'is_active'} = 1;
$cell->{$_}->set_active() foreach qw( up down left right );
}
sub clear_active
{
my $cell = shift;
return unless $cell->is_active();
return if $cell->is_wall();
$cell->{'is_active'} = 0;
return if $cell->has_block();
$cell->{$_}->clear_active() foreach qw( up down left right );
}
sub is_active { my $cell = shift; $cell->{'is_active'}; }
sub has_block { my $cell = shift; ref( $cell->{'contents'} ) =~ /block/; }
sub contents { my $cell = shift; $cell->{'contents'}; }
sub up { my $cell = shift; $cell->{'up'}; }
sub down { my $cell = shift; $cell->{'down'}; }
sub left { my $cell = shift; $cell->{'left'}; }
sub right { my $cell = shift; $cell->{'right'}; }
sub is_goal { my $cell = shift; $cell->{'display'} eq '.'; }
sub is_wall { my $cell = shift; $cell->{'display'} eq '#'; }
sub is_free { my $cell = shift; my $ref_block = shift; not ( $cell->has_block() or $cell->is_wall() or $cell->is_bad( $ref_block ) ); }
sub is_bad
{
my $cell = shift;
my $ref_block = shift;
return 1 if $cell->{'is_corner'} and not $cell->is_goal();
return 0 unless $cell->{'edge_groups'};
my $is_bad = 0;
foreach my $group ( @{$cell->{'edge_groups'}} )
{
my $block_count = 0;
foreach my $group_cell ( @{$group->{'cells'}} )
{
# count the number of blocks in this edge_row - but discount this block if it's already in the row
# printf "group cell check: %s block, %d <=> %d\n", ( $group_cell->has_block() ? 'has' : 'no' ), $group_cell->{'contents'}{'id'}, $ref_block->{'id'};
$block_count++ if $group_cell->has_block() and ( $group_cell->{'contents'}{'id'} != $ref_block->{'id'} );
}
$is_bad = 1 if $block_count >= $group->{'goals'};
}
return $is_bad;
}
sub display
{
my $cell = shift;
return $cell->{'contents'}->display() if $cell->{'contents'};
return $cell->{'display'} if $cell->is_wall or $cell->is_goal();
# return 'X' if $cell->is_bad();
return $cell->{'display'};
}
package block;
sub new
{
my $class = shift;
my $block = bless { @_ }, $class;
$block->{'mask'} = 1 << $block->{'id'};
$block->{'display'} = chr( ord('a') + $block->{'id'} );
return $block;
}
sub cell { my $block = shift; $block->{'cell'}; }
sub id { my $block = shift; $block->{'id'}; }
sub mask { my $block = shift; $block->{'mask'}; }
sub display { my $block = shift; $block->is_home() ? uc( $block->{'display'} ) : $block->{'display'}; }
sub is_home { my $block = shift; $block->{'cell'}->is_goal(); }
sub is_active { my $block = shift; my $direction = shift; $block->{'cell'}{$direction}->is_active(); }
sub can_move
{
my $block = shift;
my $direction = shift;
$block->{'cell'}{$direction}->is_free( $block );
}
sub move
{
my $block = shift;
my $direction = shift;
$block->move_to( $block->{'cell'}{$direction} );
$block->{'cell'}{'is_active'} = 0;
$block->{'cell'}{$_}->clear_active() foreach grep( ! /$opposite->{$direction}/, qw( up down left right ) );
$block->{'cell'}{$opposite->{$direction}}->set_active();
}
sub move_to
{
my $block = shift;
$block->{'cell'}{'contents'} = undef if $block->{'cell'};
$block->{'cell'} = shift;
$block->{'cell'}{'contents'} = $block;
}
sub possible_moves
{
my $block = shift;
my @available_moves = ();
foreach my $direction ( qw( up down left right ) )
{
next unless $block->can_move( $direction );
next unless $block->is_active( $opposite->{$direction} );
push( @available_moves, $direction );
}
return @available_moves;
}
package mover;
use base qw( block );
sub new { my $class = shift; return bless { @_ }, $class; }
sub display { my $mover = shift; $mover->{'cell'}->is_goal() ? '+' : '@'; }
sub lift_up { my $mover = shift; $mover->{'cell'}{'contents'} = undef; $mover->{'cell'} = undef; }
package test_suite;
use Test::More;
sub run
{
plan( tests => 116 );
my $puzzles = importer::import_puzzles( <DATA> );
test0( $puzzles->[0] );
test1( $puzzles->[1] );
exit();
}
sub test0
{
my $puzzle = shift;
is( scalar( @{$puzzle->{'cells'}} ), 3, 'only 3 cells' );
is( scalar( @{$puzzle->{'blocks'}} ), 1, 'only 1 block' );
is( $puzzle->{'mover'}{'cell'}{'id'}, 1, 'mover on cell 1' );
is( $puzzle->{'blocks'}[0]{'cell'}{'id'}, 0, 'block on cell 0' );
is( $puzzle->state(), '1:0', 'mover on cell 1, block only on cell 0' );
is( $puzzle->state_id(), '$++', 'state id: block active active' );
is( $puzzle->{'score'}, 1, 'score' );
is( $puzzle->{'top_score'}, 1, 'top score' );
is( $puzzle->is_solved(), '1', 'puzzle is already solved' );
is( $puzzle->{'cells'}[0]{'is_corner'}, 1, 'corner' );
is( $puzzle->{'cells'}[0]->has_block(), 1, 'has block' );
is( $puzzle->{'cells'}[0]->is_bad(), '', 'not bad (is goal)' );
is( $puzzle->{'cells'}[0]->is_free(), '', 'not free' );
is( $puzzle->{'cells'}[0]->is_goal(), 1, 'goal' );
is( $puzzle->{'cells'}[0]->is_active(), '', 'not active' );
is( $puzzle->{'cells'}[1]{'is_corner'}, '', 'not corner' );
is( $puzzle->{'cells'}[1]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[1]->is_bad(), '', 'not bad' );
is( $puzzle->{'cells'}[1]->is_free(), 1, 'free (mover doesn\'t count)' );
is( $puzzle->{'cells'}[1]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[1]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[2]{'is_corner'}, 1, 'corner' );
is( $puzzle->{'cells'}[2]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[2]->is_bad(), 1, 'bad' );
is( $puzzle->{'cells'}[2]->is_free(), '', 'not free (is bad)' );
is( $puzzle->{'cells'}[2]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[2]->is_active(), 1, 'active' );
}
sub test1
{
my $puzzle = shift;
my @possible_moves = ();
my $state = undef;
is( scalar( @{$puzzle->{'cells'}} ), 9, 'number of cells' );
is( scalar( @{$puzzle->{'blocks'}} ), 2, 'number of blocks' );
is( $puzzle->{'mover'}{'cell'}{'id'}, 1, 'mover location' );
is( $puzzle->{'blocks'}[0]{'cell'}{'id'}, 3, 'block location' );
is( $puzzle->state(), '1:3:4', 'mover and then block locations' );
is( $puzzle->state_id(), '+++$$++++', 'state id: block active active' );
is( $puzzle->{'score'}, 0, 'score' );
is( $puzzle->{'top_score'}, 3, 'top score' );
is( $puzzle->is_solved(), '', 'puzzle is already solved' );
is( $puzzle->{'cells'}[0]{'is_corner'}, 1, 'corner' );
is( $puzzle->{'cells'}[0]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[0]->is_bad(), 1, 'bad (corner)' );
is( $puzzle->{'cells'}[0]->is_free(), '', 'not free (corner)' );
is( $puzzle->{'cells'}[0]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[0]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[1]{'is_corner'}, '', 'not corner' );
is( $puzzle->{'cells'}[1]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[1]->is_bad(), '', 'not bad' );
is( $puzzle->{'cells'}[1]->is_free(), 1, 'free (mover doesn\'t count)' );
is( $puzzle->{'cells'}[1]->is_goal(), 1, 'goal' );
is( $puzzle->{'cells'}[1]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[2]{'is_corner'}, 1, 'corner' );
is( $puzzle->{'cells'}[2]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[2]->is_bad(), 1, 'bad (corner)' );
is( $puzzle->{'cells'}[2]->is_free(), '', 'not free (is bad)' );
is( $puzzle->{'cells'}[2]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[2]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[3]{'is_corner'}, '', 'not a corner' );
is( $puzzle->{'cells'}[3]->has_block(), 1, 'block' );
is( $puzzle->{'cells'}[3]->is_bad(), '', 'not bad' );
is( $puzzle->{'cells'}[3]->is_free(), '', 'not free (has block)' );
is( $puzzle->{'cells'}[3]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[3]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[4]{'is_corner'}, '', 'not corner' );
is( $puzzle->{'cells'}[4]->has_block(), 1, 'has block' );
is( $puzzle->{'cells'}[4]->is_bad(), '', 'not bad' );
is( $puzzle->{'cells'}[4]->is_free(), '', 'not free (has block)' );
is( $puzzle->{'cells'}[4]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[4]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[5]{'is_corner'}, '', 'not corner' );
is( $puzzle->{'cells'}[5]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[5]->is_bad(), '', 'not bad' );
is( $puzzle->{'cells'}[5]->is_free(), 1, 'free' );
is( $puzzle->{'cells'}[5]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[5]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[6]{'is_corner'}, 1, 'corner' );
is( $puzzle->{'cells'}[6]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[6]->is_bad(), '', 'not bad (is goal)' );
is( $puzzle->{'cells'}[6]->is_free(), 1, 'free' );
is( $puzzle->{'cells'}[6]->is_goal(), 1, 'goal' );
is( $puzzle->{'cells'}[6]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[7]{'is_corner'}, '', 'not corner' );
is( $puzzle->{'cells'}[7]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[7]->is_bad(), '', 'not bad' );
is( $puzzle->{'cells'}[7]->is_free(), 1, 'free' );
is( $puzzle->{'cells'}[7]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[7]->is_active(), 1, 'active' );
is( $puzzle->{'cells'}[8]{'is_corner'}, 1, 'corner' );
is( $puzzle->{'cells'}[8]->has_block(), '', 'no block' );
is( $puzzle->{'cells'}[8]->is_bad(), 1, 'bad' );
is( $puzzle->{'cells'}[8]->is_free(), '', 'not free (is bad)' );
is( $puzzle->{'cells'}[8]->is_goal(), '', 'not goal' );
is( $puzzle->{'cells'}[8]->is_active(), 1, 'active' );
@possible_moves = $puzzle->possible_moves();
is( scalar( @possible_moves ), 3, 'number of possible moves' );
is( $possible_moves[0]{'block_id'}, 0, 'block to move' );
is( $possible_moves[0]{'direction'}, 'down', 'direction to move' );
is( $possible_moves[1]{'block_id'}, 1, 'block to move' );
is( $possible_moves[1]{'direction'}, 'up', 'direction to move' );
is( $possible_moves[2]{'block_id'}, 1, 'block to move' );
is( $possible_moves[2]{'direction'}, 'down', 'direction to move' );
$puzzle->move_block( $puzzle->{'blocks'}[0], 'down' );
is( $puzzle->{'blocks'}[0]->is_home(), 1, 'moved block 0 onto goal' );
is( $puzzle->{'score'}, 1, 'score' );
is( $puzzle->{'top_score'}, 3, 'top score' );
is( $puzzle->is_solved(), '', 'not solved yet' );
@possible_moves = $puzzle->possible_moves();
is( scalar( @possible_moves ), 4, 'number of possible moves' );
is( $possible_moves[0]{'block_id'}, 1, 'block to move' );
is( $possible_moves[0]{'direction'}, 'up', 'direction to move' );
is( $possible_moves[1]{'block_id'}, 1, 'block to move' );
is( $possible_moves[1]{'direction'}, 'down', 'direction to move' );
is( $possible_moves[2]{'block_id'}, 1, 'block to move' );
is( $possible_moves[2]{'direction'}, 'left', 'direction to move' );
is( $possible_moves[3]{'block_id'}, 1, 'block to move' );
is( $possible_moves[3]{'direction'}, 'right', 'direction to move' );
$state = $puzzle->state();
$puzzle->move_block( $puzzle->{'blocks'}[1], 'down' );
is( $puzzle->{'blocks'}[1]->is_home(), '', 'moved block 1 wrong' );
is( $puzzle->{'score'}, 1, 'score' );
is( $puzzle->{'top_score'}, 3, 'top score' );
is( $puzzle->is_solved(), '', 'not solved yet' );
@possible_moves = $puzzle->possible_moves();
is( scalar( @possible_moves ), 0, 'no moves available' );
$puzzle->restore( $state );
$puzzle->dump();
@possible_moves = $puzzle->possible_moves();
is( scalar( @possible_moves ), 4, 'number of possible moves' );
is( $possible_moves[0]{'block_id'}, 1, 'block to move' );
is( $possible_moves[0]{'direction'}, 'up', 'direction to move' );
is( $possible_moves[1]{'block_id'}, 1, 'block to move' );
is( $possible_moves[1]{'direction'}, 'down', 'direction to move' );
is( $possible_moves[2]{'block_id'}, 1, 'block to move' );
is( $possible_moves[2]{'direction'}, 'left', 'direction to move' );
is( $possible_moves[3]{'block_id'}, 1, 'block to move' );
is( $possible_moves[3]{'direction'}, 'right', 'direction to move' );
$puzzle->move_block( $puzzle->{'blocks'}[1], 'up' );
$puzzle->dump();
is( $puzzle->{'blocks'}[1]->is_home(), 1, 'moved block 1 to goal' );
is( $puzzle->{'score'}, 3, 'score' );
is( $puzzle->{'top_score'}, 3, 'top score' );
is( $puzzle->is_solved(), 1, 'solved' );
}
__DATA__
#####
#*@ #
#####
#####
# + #
#$$ #
#. #
#####
####
#. #
# ##
# #
#$ #
#@ #
####
#####
# + #
##$##
# #
# #
# #
#####
####
### #
# $ #
# ## #
# .#
# @ #
# #
######