Algorithm-MasterMind
view release on metacpan or search on metacpan
app/mm-eda.cgi view on Meta::CPAN
#!/usr/bin/perl
=head1 NAME
mm-eda.cgi - CGI for playing an Mastermind using an Estimation of Distribution Algorithms
=head1 SYNOPSIS
http://localhost/cgi-bin/mm-eda.cgi
=head1 DESCRIPTION
This script uses L<Algorithm::Mastermind::EDA> for playing a basic
Mastermind game, with 6 colors and 4 pegs. When called without
parameters it shows a form for introducing the secret combination, if
called with parameter C<code> it will call the algorithm and produce
the solution. It usually takes less than a second, but your mileage
may vary depending on the server and Perl version.
=cut
use strict;
use warnings;
use lib qw(/home/jmerelo/proyectos/CPAN/Algorithm_Mastermind/lib
/home/jmerelo/proyectos/CPAN/Algorithm-Evolutionary/lib
..);
use CGI qw(:standard :form);
use Algorithm::MasterMind qw(check_combination);
use Algorithm::MasterMind::EDA;
print header();
if ( param('code' ) ) {
solve( param('code') );
} else {
show_form();
}
print end_html;
sub show_form {
print start_html( 'MM solver using EDAs' ),
h1('Mastermind input form'),
p('This program tries to find a code (composed of four letters in the set A..F) using an estimation of distributiona algorithm. Please enter any combination, and wait for the result!');
form();
}
sub form {
print
start_form,
p("Combination to find e.g. ACDE "),
textfield(-name => 'code',
-default => 'ACEF',
-size=> 4),
end_form;
}
sub solve {
my $code = shift;
#Clean up
$code = uc( $code );
$code = substr( $code, 0, 4);
$code =~ s/[^A-F]/A/g;
if ( length( $code ) < 4 ) {
do {
$code .= 'A';
} until (length( $code ) == 4);
}
my @alphabet = qw( A B C D E F );
my $solver = new Algorithm::MasterMind::EDA { alphabet => \@alphabet,
length => length( $code ),
pop_size => 300};
( run in 1.128 second using v1.01-cache-2.11-cpan-39bf76dae61 )