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 )
This function, and all the rest, are directly inherited from base
=head2 issue_first ()
Issues the first combination, which might be generated in a particular
way
=head2 issue_next()
Issues the next combination
=head2 feedback()
Obtain the result to the last combination played
=head2 guesses()
Total number of guesses
=head2 evaluated()
Total number of combinations checked to issue result
=head2 number_of_rules ()
Returns the number of rules in the algorithm
=head2 rules()
Returns the rules (combinations, blacks, whites played so far) y a
reference to array
=head2 matches( $string )
Returns a hash with the number of matches, and whether it matches
every rule with the number of blacks and whites it obtains with each
of them
=head1 AUTHOR
JJ Merelo C<< <jj@merelo.net> >>
=head1 LICENCE AND COPYRIGHT
( run in 0.574 second using v1.01-cache-2.11-cpan-39bf76dae61 )