Games::Mastermind - A simple framework for Mastermind games
version 0.06
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();
Games::Mastermind is a very simple framework for running Mastermind games.
The Games::Mastermind class provides the following methods:
Constructor. Valid parameters are pegs, a reference to the list of available pegs and holes, the number of holes in the game.
pegs
holes
The default game is the original Mastermind:
pegs => [qw( B C G R Y W )] holes => 4
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).
@guess
The winning combination is [ $mm->holes(), 0 ].
[ $mm->holes(), 0 ]
Start a new game: clear the history and compute a new code.
Return the move number. 0 if the game hasn't started yet.
0
Accessors are available for most of the game parameters:
The list of pegs (as a reference to a list of strings).
The number of holes.
Return a reference to the game history, as an array of [ guess, answer ] arrays.
[ guess, answer ]
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.
reset()
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.
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.
bug-games-mastermind@rt.cpan.org
Sébastien Aperghis-Tramoni opened his old Super Mastermind game to check out what the black markers meant.
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.
Philippe Bruhat (BooK) <book@cpan.org>
Copyright 2005-2013 Philippe Bruhat (BooK), All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Games::Mastermind, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Games::Mastermind
CPAN shell
perl -MCPAN -e shell install Games::Mastermind
For more information on module installation, please visit the detailed CPAN module installation guide.