From a5c41cd01d2cbcab263c7306b9cc9068deb9fff3 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 25 Nov 2010 21:23:02 +0000 Subject: more refactoring --- lib/GridFiller.pm | 4 ++- lib/GridFiller/Result.pm | 1 + lib/GridFiller/Status.pm | 80 ++---------------------------------------------- 3 files changed, 6 insertions(+), 79 deletions(-) diff --git a/lib/GridFiller.pm b/lib/GridFiller.pm index 44f0a50..022b14b 100644 --- a/lib/GridFiller.pm +++ b/lib/GridFiller.pm @@ -4,6 +4,7 @@ use namespace::autoclean; use GridFiller::Types qw(GridT WordListT); use GridFiller::Status; use GridFiller::Result; +use GridFiller::Chooser; use Carp; with 'MooseX::Log::Log4perl'; @@ -25,13 +26,14 @@ sub fill { my $status = GridFiller::Status->new({grid => $self->grid, words => $self->words}); my $result = GridFiller::Result->new({source_grid => $self->grid}); + my $chooser = GridFiller::Chooser->new({status => $status}); while ($status->unfilled() && $status->has_next_word()) { my $word = $status->get_next_word(); $self->log->debug("Placing $word"); - my ($x,$y,$dir) = $status->find_place_for($word); + my ($x,$y,$dir) = $chooser->find_place_for($word); if (! defined $x) { $self->log->debug("No place for $word"); diff --git a/lib/GridFiller/Result.pm b/lib/GridFiller/Result.pm index 56ae3c2..d0e6684 100644 --- a/lib/GridFiller/Result.pm +++ b/lib/GridFiller/Result.pm @@ -26,6 +26,7 @@ has grid => ( has source_grid => ( isa => GridT, is => 'ro', + required => 1, ); { diff --git a/lib/GridFiller/Status.pm b/lib/GridFiller/Status.pm index 873d252..4f55c6f 100644 --- a/lib/GridFiller/Status.pm +++ b/lib/GridFiller/Status.pm @@ -17,11 +17,13 @@ has words_to_use => ( get_next_word => 'shift', }, is => 'rw', + required => 1, ); has grid_status => ( isa => GridStatusT, is => 'rw', + required => 1, ); around BUILDARGS => sub { @@ -92,53 +94,6 @@ sub unfilled { 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($length) unless @ret; - } - else { - @ret = $self->_find_place_vert($length); - @ret = $self->_find_place_horiz($length) unless @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,$HORIZONTAL) if 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,$VERTICAL) if defined $row; - } - return; -} - { my %symbols=( $NOTHING => ' ', @@ -146,37 +101,6 @@ my %symbols=( $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($skip) if 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) = @_; -- cgit v1.2.3