-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.pm
148 lines (137 loc) · 4.02 KB
/
Field.pm
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
package Field;
use strict;
use warnings;
use Term::ANSIScreen qw/:color :cursor :screen :keyboard/;
sub new {
my $class = shift;
my $width = shift;
my $height = shift;
my $self;
$self = {
width => $width,
height => $height,
flings => {},
buff => [],
pos_buff => [],
last_id => 1,
removed => [],
history => {},
hm_id => '0',
solve_moves => [],
};
bless $self, $class;
return $self;
} ## --- end sub new
sub draw_field {
my $self = shift;
$self->{buff} = [];
my @a = (0..9, 'a'..'z', 'A'..'Z');
foreach(keys %{$self->{flings}}){
my $x = $self->{flings}{$_}->pos_x*3+1;
my $y = $self->{flings}{$_}->pos_y*3+1;
$self->{buff}[$y][$x] = $_;
$self->{buff}[$y][$x-1]='<' if $self->{flings}{$_}->is_move_possible('left');
$self->{buff}[$y][$x+1]='>' if $self->{flings}{$_}->is_move_possible('right');
$self->{buff}[$y-1][$x]='^' if $self->{flings}{$_}->is_move_possible('up');
$self->{buff}[$y+1][$x]='v' if $self->{flings}{$_}->is_move_possible('down');
}
for(my $i=0; $i<$self->{height}*3; $i++){
for(my $j=0; $j<$self->{width}*3; $j++){
if($self->{buff}[$i][$j]){
print colored [defined($self->{flings}{$self->{buff}[$i][$j]}) ? $self->{flings}{$self->{buff}[$i][$j]}{color} : 'white'], ($self->{buff}[$i][$j] =~ /\d+/) ? $a[$self->{buff}[$i][$j]] : $self->{buff}[$i][$j];
}elsif($i eq 0 || $i eq (($self->{height}*3)-1)){
unless((($j-1)/3)-int(($j-1)/3)){
print int($j/3);
}else{
print "-";
}
}elsif($j eq 0 || $j eq (($self->{width}*3)-1)){
unless((($i-1)/3)-int(($i-1)/3)){
print int($i/3);
}else{
print "|";
}
}else{
print " ";
}
print " ";
}
print "\n";
}
print color 'reset';
return 1;
} ## --- end sub draw_field
sub put_fling {
my $self = shift;
my $fling = shift;
$fling->{id} = $self->{last_id};
$self->{flings}->{$self->{last_id}} = $fling;
$self->{last_id}+=1;
$self->{pos_buff} = [] unless $self->{pos_buff};
$self->{pos_buff}[$fling->pos_y] = [] unless $self->{pos_buff}[$fling->pos_y];
$self->{pos_buff}[$fling->pos_y][$fling->pos_x] = $fling;
return ;
} ## --- end sub put_fling
sub refresh_positions {
my $self = shift;
$self->{pos_buff} = [];
foreach my $fling_id (keys %{$self->{flings}}){
$self->{pos_buff}[$self->{flings}{$fling_id}->pos_y] = [] unless $self->{pos_buff}[$self->{flings}{$fling_id}->pos_y];
$self->{pos_buff}[$self->{flings}{$fling_id}->pos_y][$self->{flings}{$fling_id}->pos_x] = $self->{flings}{$fling_id};
}
return ;
} ## --- end sub refresh_positions
sub width {
my $self = shift;
return $self->{width};
}
sub height {
my $self = shift;
return $self->{height};
} ## --- end sub height
sub _find_moves {
my $self = shift;
my %moves;
my @dirs = qw/up down left right/;
my $moves_cnt = 0;
foreach my $fling_id (keys %{$self->{flings}}){
@{$moves{$fling_id}} = grep {$self->{flings}{$fling_id}->is_move_possible($_)} @dirs;
$moves_cnt += scalar @{$moves{$fling_id}};
}
return ($moves_cnt, %moves);
} ## --- end sub _find_moves
sub do_move {
use Data::Dumper;
my $self = shift;
my $level = shift || 0;
$self->{hm_id}++;
my $hm_id = $self->{hm_id};
my ($moves_cnt, %moves) = $self->_find_moves();
if($moves_cnt){
# print "doing step $hm_id, moves:$moves_cnt\n";
foreach my $fling_id (keys %moves){
next unless scalar $moves{$fling_id};
foreach my $move (@{$moves{$fling_id}}) {
my $moveid = "$hm_id\_$fling_id";
# $self->draw_field();
@{$self->{history}{$moveid}} = $self->{flings}{$fling_id}->move($move);
push @{$self->{solve_moves}}, "Moving fling $fling_id to $move \n";
my $res = $self->do_move(++$level);
return 0 unless $res;
# print "ROLL BACK\n";
pop @{$self->{solve_moves}};
$self->{flings}{$_}->restore_position() foreach @{$self->{history}{$moveid}};
my $deleted = pop @{$self->{removed}};
$deleted->restore();
$self->refresh_positions();
delete $self->{history}{$moveid};
# $self->draw_field();
}
}
return -1;
}else{
# print Dumper($self->{history});
return scalar(keys(%{$self->{flings}}))-1;
}
} ## --- end sub do_move
1;