NAME
Games::Mastermind - A simple framework for Mastermind games
VERSION
version 0.06
SYNOPSIS
use Games::Mastermind;
# the classic game
$mm = Games::Mastermind->new;
# make a guess
$marks = $game->play(qw( Y C W R ));
# results
print "You win!\n" if $marks->[0] == $mm->holes();
# the game history is available at all times
$history = $mm->history();
$last_turn = $mm->history()->[-1];
# reset the game
$mm->reset();
DESCRIPTION
Games::Mastermind is a very simple framework for running Mastermind games.
METHODS
The Games::Mastermind class provides the following methods:
- new( %args )
-
Constructor. Valid parameters are
pegs
, a reference to the list of available pegs andholes
, the number of holes in the game.The default game is the original Mastermind:
pegs => [qw( B C G R Y W )] holes => 4
- play( @guess )
-
Give the answer to
@guess
as a reference to an array of two numbers: the number of black marks (right colour in the right position) and the number of white marks (right colour in the wrong position).The winning combination is
[ $mm->holes(), 0 ]
. - reset()
-
Start a new game: clear the history and compute a new code.
- turn()
-
Return the move number.
0
if the game hasn't started yet.
Accessors
Accessors are available for most of the game parameters:
- pegs()
-
The list of pegs (as a reference to a list of strings).
- holes()
-
The number of holes.
- history()
-
Return a reference to the game history, as an array of
[ guess, answer ]
arrays. - code()
-
The hidden code, as a reference to the list of hidden pegs.
All these getters are also setters. Note that setting any of these parameters will automatically reset()
the game.
GAME API
This section describes how to interface the game with a player.
Once the game is created, for each turn, it is given a guess and returns the outcome of this turn.
This example script show a very dumb player program:
use Games::Mastermind;
my $game = Games::Mastermind->new(); # standard game
my $holes = $game->holes();
my @pegs = @{ $game->pegs() };
# simply play at random
my $result = [ 0, 0 ];
while ( $result->[0] != $holes ) {
$result =
$game->play( my @guess = map { $pegs[ rand @pegs ] } 1 .. $holes );
print "@guess | @$result\n";
}
The flow of control is in the hand of the player program or object, which asks the game if the guess was good. The count of turns must be handled by the controlling program.
BUGS
Please report any bugs or feature requests to bug-games-mastermind@rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Mastermind. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
ACKNOWLEDGEMENTS
Sébastien Aperghis-Tramoni opened his old Super Mastermind game to check out what the black markers meant.
BUGS
Please report any bugs or feature requests on the bugtracker website http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Mastermind or by email to bug-games-mastermind@rt.cpan.org.
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
AUTHOR
Philippe Bruhat (BooK) <book@cpan.org>
COPYRIGHT
Copyright 2005-2013 Philippe Bruhat (BooK), All Rights Reserved.
LICENSE
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.