| Games-Mastermind-Solver documentation | Contained in the Games-Mastermind-Solver distribution. |
Games::Mastermind::Solver - a Master Mind puzzle solver
# a trivial Mastermind solver
use Games::Mastermind;
use Games::Mastermind::Solver::BruteForce;
my $player = Games::Mastermind::Solver::BruteForce
->new( Games::Mastermind->new );
my $try;
print join( ' ', @{$player->game->code} ), "\n\n";
until( $player->won || ++$try > 10 ) {
my( $win, $guess, $result ) = $player->move;
print join( ' ', @$guess ),
' ',
'B' x $result->[0], 'W' x $result->[1],
"\n";
}
Games::Mastermind::Solver is a base class for Master Mind solvers.
$player = Games::Mastermind::Solver->new( $game );
Constructor. Takes a Games::Mastermind object as argument.
( $won, $guess, $result ) = $player->move;
( $won, $guess, $result ) = $player->move( $guess );
The player chooses a suitable move to continue the game, plays it
against the game object passed as constructor and updates its knowledge
of the solution. The $won return value is a boolean, $guess is
an array reference holding the value passed to Games::Mastermind::play
and $result is the value returned by play.
It is possible to pass an array reference as the move to make.
$number = $player->remaining;
The number of possible solutions given the knowledge the player has accumulated.
$player->reset;
Resets the internal state of the player.
$guess = $player->guess;
Guesses a solution (to be implemented in a subclass).
$player->check( $guess, $result );
Given a guess and the result for the guess, determines which positions are still possible solutions for the game (to be implemented in a subclass).
Mattia Barbon <mbarbon@cpan.org>
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Games-Mastermind-Solver documentation | Contained in the Games-Mastermind-Solver distribution. |
package Games::Mastermind::Solver; use strict; use warnings; use base qw(Class::Accessor::Fast); our $VERSION = '0.02'; __PACKAGE__->mk_ro_accessors( qw(game won) ); sub new { my( $class, $game ) = @_; my $self = $class->SUPER::new( { game => $game } ); $self->reset; return $self; } sub move { my( $self, $guess ) = @_; return ( 1, undef, undef ) if $self->won; $guess ||= $self->guess; my $result = $self->game->play( @$guess ); if( $result->[0] == $self->game->holes ) { $self->{won} = 1; } else { $self->check( $guess, $result ); } return ( $self->won, $guess, $result ); } sub reset { my( $self ) = @_; $self->game->reset; $self->{won} = 0; } 1; __END__