Algorithm-MasterMind
view release on metacpan or search on metacpan
lib/Algorithm/MasterMind/Sequential_Alt.pm view on Meta::CPAN
package Algorithm::MasterMind::Sequential_Alt;
use warnings;
use strict;
use Carp;
use lib qw(../../lib);
our $VERSION = sprintf "%d.%03d", q$Revision: 1.3 $ =~ /(\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'}};
my @tebahpla = reverse @alphabet;
$self->{'_engine_fw'} = variations_with_repetition(\@alphabet, $options->{'length'});
$self->{'_engine_bw'} = variations_with_repetition(\@tebahpla, $options->{'length'});
$self->{'_current_min'} = $alphabet[0]x$options->{'length'};
$self->{'_current_max'} = $tebahpla[0]x$options->{'length'};
$self->{'_direction'} = 1; # Forward, 0 for backwards
}
sub issue_next {
my $self = shift;
my $rules = $self->number_of_rules();
my ($match, $string);
do {
if ( $self->{'_direction'} ) {
$string = join("",@{$self->{'_engine_fw'}->next});
$self->{'_current_min'} = $string;
} else {
$string = join("",@{$self->{'_engine_bw'}->next});
$self->{'_current_max'} = $string;
}
$match = $self->matches($string);
} while ( ( $self->{'_current_min'} lt $self->{'_current_max'} )
&& $match->{'matches'} < $rules );
$self->{'_direction'} = !$self->{'_direction'};
return $self->{'_last'} = $string;
}
"some blacks, 0 white"; # Magic true value required at end of module
__END__
=head1 NAME
Algorithm::MasterMind::Sequential_Alt - Tests each combination in
turn, alternating with the beginning and end of the sequence.
=head1 SYNOPSIS
use Algorithm::MasterMind::Sequential_Alt;
my $secret_code = 'ADCB';
my @alphabet = qw( A B C D E F );
my $solver = new Algorithm::MasterMind::Sequential_Alt { alphabet => \@alphabet,
length => length( $secret_code ) };
( run in 0.821 second using v1.01-cache-2.11-cpan-5a3173703d6 )