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 ../../../../lib);
our $VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/g;
use base 'Algorithm::MasterMind::Partition';
use Algorithm::MasterMind qw( partitions );
sub compute_next_string {
my $self = shift;
my $partitions = shift || croak "No partitions";
# Obtain best
my %min_c;
my $max = 0;
for my $c ( keys %$partitions ) {
$min_c{$c} = keys %{$partitions->{$c}};
if ( $min_c{$c} > $max ) {
$max = $min_c{$c};
}
}
# Find all partitions with that max
my @most_parts = grep( $min_c{$_} == $max, keys %min_c );
# Break ties
return $most_parts[ rand( @most_parts )];
}
"while some were blacks, the sun was shining"; # Magic true value required at end of module
__END__
=head1 NAME
Algorithm::MasterMind::Partition::Most - Uses "most partitions"
criterium to play
=head1 SYNOPSIS
use Algorithm::MasterMind::Partition::Most;
my $secret_code = 'EAFC';
my @alphabet = qw( A B C D E F );
my $solver = new Algorithm::MasterMind::Partition::Most { alphabet => \@alphabet,
length => length( $secret_code ) };
=head1 DESCRIPTION
Solves the algorithm by issuing each time a combination with a
particular score; the computation of that score is delegated to
subclasses.
=head1 INTERFACE
=head2 compute_next_string
Computes the string with the most non-null partitions
=head1 AUTHOR
JJ Merelo C<< <jj@merelo.net> >>, proposed initially by Bestavros.
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2010, JJ Merelo C<< <jj@merelo.net> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
( run in 1.248 second using v1.01-cache-2.11-cpan-56fb94df46f )