Algorithm-MasterMind
view release on metacpan or search on metacpan
lib/Algorithm/MasterMind/Random.pm view on Meta::CPAN
package Algorithm::MasterMind::Random;
use warnings;
use strict;
use Carp;
use lib qw(../../lib);
our $VERSION = sprintf "%d.%03d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/g;
use base 'Algorithm::MasterMind';
sub initialize {
my $self = shift;
my $options = shift;
for my $o ( keys %$options ) {
$self->{"_$o"} = $options->{$o};
}
}
sub issue_next {
my $self = shift;
my $rules = $self->number_of_rules();
my ($match, $string);
my @alphabet = @{$self->{'_alphabet'}};
my $length = $self->{'_length'};
do {
$string ='';
for ( my $i = 0; $i < $length; $i++ ) {
$string .= $alphabet[rand(@alphabet)];
}
$match = $self->matches($string);
$self->{'_evaluated'}++;
} while ( $match->{'matches'} < $rules );
return $self->{'_last'} = $string;
}
"some blacks, 0 white"; # Magic true value required at end of module
__END__
=head1 NAME
Algorithm::MasterMind::Random - Plays random consistent combinations
=head1 SYNOPSIS
use Algorithm::MasterMind::Random;
my $secret_code = 'EAFC';
my @alphabet = qw( A B C D E F );
my $solver = new Algorithm::MasterMind::Random { alphabet => \@alphabet,
length => length( $secret_code ) };
=head1 DESCRIPTION
Not very efficient, but memory footprint is null and works pretty well
for small spaces. Beware of big spaces, it could get stuck
=head1 INTERFACE
=head2 initialize()
Called from C<new>
=head2 new ( $options )
( run in 1.031 second using v1.01-cache-2.11-cpan-ceb78f64989 )