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


Games::Mastermind is a very simple framework for running Mastermind games.


The Games::Mastermind class provides the following methods:

new( %args )

Constructor. Valid parameters are pegs, a reference to the list of available pegs and holes, 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 ].


Start a new game: clear the history and compute a new code.


Return the move number. 0 if the game hasn't started yet.


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.


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.


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, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


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 or by email to

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) <>


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.