summaryrefslogtreecommitdiff
path: root/lib/GridFiller.pm
blob: 11392779f12cba55e0e13b8fb58a942d8b4018a3 (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
package GridFiller; 
use Moose;
use namespace::autoclean;
use GridFiller::Types qw(GridT WordListT );
use Carp;
 
with 'MooseX::Log::Log4perl';
 
has status => (
    isa => 'GridFiller::Status',
    is => 'ro',
    required => 1,
);
 
has chooser => (
    isa => 'GridFiller::Chooser',
    is => 'ro',
    required => 1,
);
 
has result => (
    isa => 'GridFiller::Result',
    is => 'ro',
    required => 1,
);
 
sub fill {
    my ($self) = @_;
 
    my $status=$self->status;
    my $result=$self->result;
    my $chooser=$self->chooser;
 
    while ($status->unfilled() && $status->has_next_word()) {
        my $word = $status->get_next_word();
 
        $self->log->debug("Placing $word");
 
        my ($space,$x,$y,$dir) = $chooser->find_place_for($word);
 
        if (! defined $x) {
            $self->log->debug("No place for $word");
            $result->mark_leftover($word);
            next;
        };
 
        $result->place_word_at($word,$space,$x,$y,$dir);
        $status->place_word_at($word,$space,$x,$y,$dir);
 
        if ($self->log->is_debug) {
            $self->log->debug($status->to_string);
            $self->log->debug($result->to_string);
        }
    }
 
    return;
}
 
1;
 
__END__
 
=head1 AUTHOR
 
Gianni Ceccarelli <dakkar@thenautilus.net>
 
=head1 COPYRIGHT AND LICENSE
 
This software is copyright (c) 2012 by Gianni Ceccarelli.
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
 
=cut