Algorithm-MasterMind

 view release on metacpan or  search on metacpan

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

package Algorithm::MasterMind::Sequential;

use warnings;
use strict;
use Carp;

use lib qw(../../lib);

our $VERSION =   sprintf "%d.%03d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/g; 

use base 'Algorithm::MasterMind';

use Algorithm::Combinatorics qw(variations_with_repetition);

sub initialize {
  my $self = shift;
  my $options = shift || croak "Need options here";
  for my $o ( keys %$options ) {
    $self->{"_$o"} = $options->{$o}
  }
  my @alphabet = @{$self->{'_alphabet'}};
  $self->{'_engine'} = variations_with_repetition(\@alphabet, $options->{'length'});
  $self->{'_range'} = $alphabet[0]."-".$alphabet[$#alphabet];
  $self->{'_current_string'} = $alphabet[0] x $self->{'_length'};
  $self->{'_last_string'} = $alphabet[$#alphabet]x $self->{'_length'};

}

sub issue_next {
  my $self = shift;
  my $rules =  $self->number_of_rules();
  my ($match, $string, $combination);

  while ( $combination = $self->{'_engine'}->next ) {
    $string = join("", @$combination);
    $match = $self->matches($string);
    last if $match->{'matches'} == $rules;
  }
  return  $self->{'_last'} = $string;
}

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

__END__

=head1 NAME

Algorithm::MasterMind::Sequential - Tests each combination in turn.


=head1 SYNOPSIS

    use Algorithm::MasterMind::Sequential;

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

=head1 DESCRIPTION

Test combinations in turn, starting by A x length. Should find the
solution, but complexity increases with size. Not very efficient.

=head1 INTERFACE 



( run in 1.449 second using v1.01-cache-2.11-cpan-98e64b0badf )