05 Jun 2009 04:19:03 UTC
- Distribution: Games-Mastermind-Cracker
- Module version: 0.03
- Source (raw)
- Browse (raw)
- How to Contribute
- Clone repository
- Testers (562 / 0 / 0)
- KwaliteeBus factor: 1
- % Coverage
- License: perl_5
- Activity24 month
- Download (27.3KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest versionSARTAK Shawn M Mooreand 1 contributors
- Shawn M Moore, C
- SEE ALSO
- COPYRIGHT AND LICENSE
Games::Mastermind::Cracker - quickly crack Mastermind
use Games::Mastermind::Cracker::Sequential; my $cracker = Games::Mastermind::Cracker::Sequential->new(); printf "The solution is %s!\n", $cracker->cracker;
Mastermind is a code-breaking game played by two players, the "code maker" and the "code breaker".
This module plays the role of code breaker. The only requirement is that you provide the answers to how many black pegs and how many white pegs a code gives.
You must instantiate a subclass of this module to actually break codes. There are a number of different cracker modules, described in "ALGORITHMS".
Games::Mastermind is the same game, except it plays the role of code maker.
Here are the algorithms, in roughly increasing order of quality.
This randomly guesses until it gets the right answer. It does not attempt to avoid guessing the same code twice.
This guesses each code in order until it gets the right answer. It uses no information from the results to prepare its next guesses.
This is the first usable algorithm. It will keep track of all the possible codes. When a result is known, it will go through the possible codes and eliminate any result inconsistent with the result. For example,
BBBBis not a possible result when
WKYWis guessed and receives a result of 1 black. This is because
WKYWwould not score 1 black if the correct code is
Creates a new Games::Mastermind::Cracker::* object. Note that you MUST instantiate a subclass of this module.
newtakes a number of arguments:
The number of holes. Default: 4.
The representations of the pegs. Default: 'K', 'B', 'G', 'R', 'Y', 'W'.
A coderef to call any time the module wants user input. It passes the coderef
$selfand the string of the guess (e.g.
KRBK) and expects to receive two numbers,
white pegs, as return value. I will call this method multiple times if necessary to get sane output, so you don't need to do much processing.
The default queries the user through standard output and standard input.
The method to call to crack a particular game of Mastermind. This takes no arguments. It returns the solution as a string, or
undefif no solution could be found.
This will return the number of holes used in the game.
This will return an array reference of the pegs used in the game.
This will return an array reference of the guesses made so far in the game. Each item in
historyis an array refrence itself, containing the guess, its black pegs, and its white pegs.
This module uses Moose so please use it to extend this module.
Your cracker should operate such that any update to its internal state is caused by
make_guess. This is because your
result_ofmethod may be called (multiple times) before
make_guessis first called.
If you absolutely have to entangle your guessing and result processing code, one way to make this work is to have
result_ofdo all the calculation and store the next guess to make in an attribute.
This method will receive no arguments, and expects a string representing the guessed code as a result. If your
undef, that will be interpreted as "unable to crack this code." If your
make_guessreturns a scalar reference, that will be interpreted as the correct solution.
This method will receive three arguments: the guess made, the number of black pegs, and the number of white pegs. It doesn't have to return anything.
This returns the last code guessed, or
undefif no code has been guessed yet.
This returns a peg randomly selected from valid pegs.
This returns a hash reference of all possible codes. This is not cached in any way, so each call is a large speed penalty.
This expects two codes. It will return the black and white marker count as if the first is a guess against the second. (actually, this method is associative, so you could say the second against the first
Shawn M Moore,
Copyright 2007-2009 Shawn M Moore.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Module Install Instructions
To install Games::Mastermind::Cracker, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Games::Mastermind::Cracker
For more information on module installation, please visit the detailed CPAN module installation guide.