summaryrefslogtreecommitdiff
path: root/lib/GridFiller/Status.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/GridFiller/Status.pm')
-rw-r--r--lib/GridFiller/Status.pm17
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/GridFiller/Status.pm b/lib/GridFiller/Status.pm
index 092f9a9..6e457b6 100644
--- a/lib/GridFiller/Status.pm
+++ b/lib/GridFiller/Status.pm
@@ -4,7 +4,7 @@ use namespace::autoclean;
use List::Util qw(shuffle);
use List::MoreUtils qw(uniq);
use GridFiller::Types qw(WordListT GridStatusT GridT WordListT);
-use MooseX::Types::Moose qw(Str);
+use MooseX::Types::Moose qw(Str CodeRef);
use GridFiller::Constants ':all';
use Carp;
use feature 'switch';
@@ -29,6 +29,12 @@ has mode => (
default => 'given',
);
+has length => (
+ isa => CodeRef,
+ is => 'rw',
+ default => sub { sub {length shift} },
+);
+
has words_to_use => (
isa => WordListT,
traits => ['Array'],
@@ -51,12 +57,13 @@ sub _build_words_to_use {
my ($self) = @_;
my $words = $self->words;
my $mode = $self->mode;
+ my $l = $self->length;
given ($mode) {
when ('random') { return [ shuffle uniq @$words ] }
when ('given') { return [ uniq @$words ] }
- when ('longest') { return [ sort {length($b) <=> length($a)} uniq @$words ] }
- when ('shortest') { return [ sort {length($a) <=> length($b)} uniq @$words ] }
+ when ('longest') { return [ sort {$l->($b) <=> $l->($a)} uniq @$words ] }
+ when ('shortest') { return [ sort {$l->($a) <=> $l->($b)} uniq @$words ] }
default { croak "Unknown mode $mode" }
}
}
@@ -80,12 +87,12 @@ sub place_word_at {
$self->log->debug("Marking <$word> occupied at ${x}:${y} ($dir)");
if ($dir == $HORIZONTAL) {
- for my $i (0..length($word)-1) {
+ for my $i (0..$self->length->($word)-1) {
$self->_mark_occupied($x+$i,$y);
}
}
elsif ($dir == $VERTICAL) {
- for my $i (0..length($word)-1) {
+ for my $i (0..$self->length->($word)-1) {
$self->_mark_occupied($x,$y+$i);
}
}