Algorithm-MasterMind
view release on metacpan or search on metacpan
lib/Algorithm/MasterMind/Partition.pm view on Meta::CPAN
package Algorithm::MasterMind::Partition;
use warnings;
use strict;
use Carp;
use lib qw(../../lib ../../../lib);
our $VERSION = sprintf "%d.%03d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/g;
use base 'Algorithm::MasterMind';
use Algorithm::MasterMind qw( partitions );
sub initialize {
my $self = shift;
my $options = shift;
for my $o ( keys %$options ) {
$self->{"_$o"} = $options->{$o};
}
$self->{'_partitions'} = {};
}
sub issue_first {
my $self = shift;
my @combinations = $self->all_combinations();
$self->{'_consistent'} = \@combinations;
return $self->{'_last'} = $self->issue_first_Knuth();
}
sub issue_next {
my $self = shift;
my $rules = $self->number_of_rules();
# Check consistency
for ( my $i = 0; $i <= $#{$self->{'_consistent'}}; $i++ ) {
my $match = $self->matches($self->{'_consistent'}->[$i]);
$self->{'_evaluated'}++;
if ( $match->{'matches'} < $rules ) {
delete $self->{'_consistent'}->[$i];
}
}
#Eliminate null
@{$self->{'_consistent'}} = grep( $_, @{$self->{'_consistent'}} );
if ( @{$self->{'_consistent'}} > 1 ) {
# Compute partitions
my $partitions = partitions( @{$self->{'_consistent'}} );
# Break ties
my $string = $self->compute_next_string( $partitions );
# Obtain next
if ( $string eq '' ) {
warn "Something is wrong\n";
}
return $self->{'_last'} = $string;
} else {
return $self->{'_last'} = $self->{'_consistent'}->[0];
}
}
sub compute_next_string {
croak "To be reimplemented by derived classes";
}
( run in 2.565 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )