summaryrefslogtreecommitdiff
path: root/lib/GridFiller/Status.pm
blob: 873d25273bfac0c36f4cbe44093a14ea6447b897 (plain)
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
package GridFiller::Status; 
use Moose;
use namespace::autoclean;
use List::Util qw(shuffle);
use List::MoreUtils qw(uniq);
use GridFiller::Types qw(WordListT GridStatusT);
use GridFiller::Constants ':all';
use Carp;
 
with 'MooseX::Log::Log4perl';
 
has words_to_use => (
    isa => WordListT,
    traits => ['Array'],
    handles => {
        has_next_word => 'count',
        get_next_word => 'shift',
    },
    is => 'rw',
);
 
has grid_status => (
    isa => GridStatusT,
    is => 'rw',
);
 
around BUILDARGS => sub {
    my ($orig$class$args@rest) = @_;
 
    if (exists $args->{words} && exists $args->{grid}) {
        $args->{words_to_use} = _munge_words_to_use(delete $args->{words});
        $args->{grid_status} = _munge_grid_status(delete $args->{grid});
    }
 
    return $class->$orig($args,@rest);
};
 
sub _munge_words_to_use {
    my $words=shift;
    # clone initial word list 
    return [ shuffle uniq @$words ];
}
 
sub _munge_grid_status {
    my $grid=shift;
    return [
            map {
                [
                    map {
                        $_ eq '*' ? $BLACK : $WHITE
                    @$_
                ]
            @$grid
    ];
}
 
sub place_word_at {
    my ($self$word$x$y$dir) = @_;
 
    $self->log->debug("Marking <$word> occupied at ${x}:${y} ($dir)");
 
    if ($dir == $HORIZONTAL) {
        for my $i (0..length($word)-1) {
            $self->_mark_occupied($x+$i,$y);
        }
    }
    elsif ($dir == $VERTICAL) {
        for my $i (0..length($word)-1) {
            $self->_mark_occupied($x,$y+$i);
        }
    }
    else {
        croak "What dir $dir?";
    }
}
 
sub _mark_occupied {
    my ($self,$x,$y) = @_;
 
    $self->grid_status->[$y][$x]=$NOTHING;
    return;
}
 
sub unfilled {
    my ($self) = @_;
 
    for my $row (@{$self->grid_status}) {
        for my $cell (@$row) {
            return 1 if $cell != $NOTHING;
        }
    }
    return 0;
}
 
sub find_place_for {
    my ($self,$word) = @_;
 
    my $dir = int(rand(2)) ? $HORIZONTAL : $VERTICAL;
 
    my $length = length $word;
 
    my @ret;
 
    if ($dir == $HORIZONTAL) {
        @ret = $self->_find_place_horiz($length);
        @ret = $self->_find_place_vert($lengthunless @ret;
    }
    else {
        @ret = $self->_find_place_vert($length);
        @ret = $self->_find_place_horiz($lengthunless @ret;
    }
 
    return @ret;
}
 
sub _find_place_horiz {
    my ($self,$length) = @_;
 
    my $rows = scalar @{$self->grid_status};
    my $col;
 
    for my $row (0..$rows-1) {
        $col = $self->_find_in_row($row,$length);
        return ($col,$row,$HORIZONTALif defined $col;
    }
    return;
}
 
sub _find_place_vert {
    my ($self,$length) = @_;
 
    my $cols = scalar @{$self->grid_status->[0]};
    my $row;
 
    for my $col (0..$cols-1) {
        $row = $self->_find_in_col($col,$length);
        return ($col,$row,$VERTICALif defined $row;
    }
    return;
}
 
{
my %symbols=(
    $NOTHING => ' ',
    $BLACK => 'X',
    $WHITE => 'O',
);
 
sub _do_find {
    my ($self,$str,$length) = @_;
 
    my ($skip) = ($str =~ m{^ (.*?) (?: X{$length} | O{$length} ) }x);
 
    $self->log->debug(defined $skip ? " skip <$skip>" : " nope");
 
    return length($skipif defined $skip;
    return;
}
 
sub _find_in_row {
    my ($self,$row,$length) = @_;
 
    my $str = join '',map { $symbols{$_} } @{$self->grid_status->[$row]};
 
    $self->log->debug("row $row = $str");
 
    return $self->_do_find($str,$length);
}
 
sub _find_in_col {
    my ($self,$col,$length) = @_;
 
    my $str = join '',map { $symbols{$_->[$col]} } @{$self->grid_status};
 
    $self->log->debug("col $col = $str");
 
    return $self->_do_find($str,$length);
}
 
sub to_string {
    my ($self) = @_;
 
    my $rows = scalar @{$self->grid_status};
 
    my $str;
 
    for my $row (0..$rows-1) {
        for my $cell (@{$self->grid_status->[$row]}) {
            $str .= $symbols{$cell};
        }
        $str .= "\n";
    }
 
    return $str;
}
}
 
1;