Algorithm-MasterMind

 view release on metacpan or  search on metacpan

lib/Algorithm/MasterMind/Partition_Most.pm  view on Meta::CPAN

package Algorithm::MasterMind::Partition_Most;

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::Consistent_Set;

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->{'_evaluated'} = scalar( @combinations  );
  $self->{'_partitions'} = new Algorithm::MasterMind::Consistent_Set \@combinations;
  my $partitions = $self->{'_partitions'};
  $partitions->compute_most_score;
  my @top_scorers = $partitions->top_scorers( 'most' );
  return $self->{'_last'} = $top_scorers[ rand(@top_scorers)];
}

sub issue_next {
  my $self = shift;
  my $last_rule = $self->{'_rules'}->[scalar(@{$self->{'_rules'}})-1];
  $self->{'_partitions'}->cull_inconsistent_with( $last_rule->{'combination'}, $last_rule );
  if ( @{$self->{'_partitions'}->{'_combinations'}} > 1 ) {
    $self->{'_partitions'}->compute_most_score;
    my @top_scorers = $self->{'_partitions'}->top_scorers('most');
    return $self->{'_last'} = $top_scorers[ rand(@top_scorers)];
  } else {
    return $self->{'_last'} = $self->{'_partitions'}->{'_combinations'}->[0]->string;
  }
}

"some blacks, 0 white"; # Magic true value required at end of module

__END__

=head1 NAME

Algorithm::MasterMind::Partition_Most - Plays combination with the highest number of partitions


=head1 SYNOPSIS

    use Algorithm::MasterMind::Partition_Worst;
    my $secret_code = 'EAFC';
    my @alphabet = qw( A B C D E F );
    my $solver = new Algorithm::MasterMind::Partition_Worst { alphabet => \@alphabet,
						   length => length( $secret_code ) };

  
=head1 DESCRIPTION

Solves the algorithm by issuing each time a combination that maximizes



( run in 2.244 seconds using v1.01-cache-2.11-cpan-63c85eba8c4 )