summaryrefslogtreecommitdiff
path: root/lib/GridFiller/Result/Text.pm
blob: 03e7f8f4fbd3431dac998a5f8651ae3ca7fcffcd (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
package GridFiller::Result::Text; 
use Moose;
use namespace::autoclean;
use GridFiller::Types qw(TextResultT);
use GridFiller::Constants ':directions';
use Carp;
 
extends 'GridFiller::Result';
 
with 'MooseX::Log::Log4perl';
 
has grid => (
    isa => TextResultT,
    is => 'ro',
    lazy_build => 1,
    clearer => '_reset_g',
);
 
 
{
my %colourmap = (
    '*' => {
        $HORIZONTAL => {
            => 1,
            => 2,
        },
        $VERTICAL => {
            => 3,
            => 4,
        },
    },
    ' ' => {
        $HORIZONTAL => {
            => 5,
            => 6,
        },
        $VERTICAL => {
            => 7,
            => 8,
        },
    },
);
 
sub _build_grid {
    return [
            map {
                [
                    map {
                        $colourmap{$_}->{$HORIZONTAL}->{0}, ' ' ],
                    @$_
                ]
            } @{shift->source_grid}
    ];
}
 
sub _find_colour_for {
    my ($self,$x,$y,$dir) = @_;
 
    my $parity = ($dir == $HORIZONTAL) ? ($y % 2) : ($x % 2);
 
    my $chosen = $colourmap{$self->source_grid->[$y][$x]}->{$dir}->{$parity};
 
    # avoid colour runs 
    if ( ( $x>0 && $dir == $HORIZONTAL && $self->grid->[$y][$x-1][0] == $chosen )
             or
         $y>0 && $dir == $VERTICAL && $self->grid->[$y-1][$x][0] == $chosen )
     ) {
        $chosen = $colourmap{$self->source_grid->[$y][$x]}->{$dir}->{1-$parity};
    }
 
    if (!defined $chosen) {
        $self->log->warn(sprintf(q{Can't decide on colour for %d:%d (%s) direction %d},
                                 $x,$y,$self->source_grid->[$y][$x],$dir));
    }
 
    return $chosen;
}
}
 
sub place_word_at {
    my ($self$word$space$x$y$dir) = @_;
 
    my $colour = $self->_find_colour_for($x,$y,$dir);
 
    $self->log->debug("Placing $word at ${x}:${y} ($dir) in colour $colour");
 
    if ($dir == $HORIZONTAL) {
        for my $i (0..length($word)-1) {
            $self->_put_letter_at(substr($word,$i,1),
                                  $x+$i,$y,
                                  $colour);
        }
    }
    elsif ($dir == $VERTICAL) {
        for my $i (0..length($word)-1) {
            $self->_put_letter_at(substr($word,$i,1),
                                  $x,$y+$i,
                                  $colour);
        }
    }
    else {
        croak "What dir $dir?";
    }
}
 
sub _put_letter_at {
    my ($self,$letter,$x,$y,$colour) = @_;
 
    croak "undef colour" unless defined $colour;
    croak "undef letter" unless defined $letter;
 
    $self->grid->[$y][$x]=[$colour,$letter];
    return;
}
 
sub to_string {
    my ($self) = @_;
 
    my $rows = scalar @{$self->grid};
 
    my $str;
 
    for my $row (0..$rows-1) {
        for my $cell (@{$self->grid->[$row]}) {
            $str .= $cell->[1] eq ' ' ? '.' : $cell->[1];
        }
        $str .= "\n";
    }
 
    return $str;
}
 
after reset => sub {
    my ($self) = @_;
    $self->_reset_g;
};
 
1;