Skip to content

Instantly share code, notes, and snippets.

@pragdave
Last active September 24, 2019 02:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pragdave/4262a2a087d761a9462e7e30f1e5ba20 to your computer and use it in GitHub Desktop.
Save pragdave/4262a2a087d761a9462e7e30f1e5ba20 to your computer and use it in GitHub Desktop.

Elixir Assignment #3

Over the next few weeks we'll be implementing the game of mastermind.

A quick recap. The game is played with a bag containing lots of colored pegs. Typically there are 6 distinct colors, and there will be multiple pegs of each.

One player sets a challenge by picking four pegs and placing them in sequence somewhere the other player can't see. The other player takes turns trying to guess the chosen pegs. On each turn, the player chooses four pegs and places them on a board. The first player then scores the guess, awarding a black marker for pegs that are the correct color and in the correct position, and a white marker for any remaining pegs that are the correct color but in the wrong position.

For example, let's use numbers to represent the colors. The challenger picks the four numbers:

3 2 3 5

The first few rounds might then be:

Guess        Score
1 1 2 2      1 white
3 3 4 4      1 black, 1 white
3 1 3 6      2 black

Scoring exact matches (a "black") takes precedence over scoring whites. Once an exact match is found, the corresponding pegs no longer participate in that round's scoring.

Your Challenge

  1. Create an Elixir module called Mastermind.Game.

  2. Inside this implement two functions. The first is:

def new(num_colors, width, max_turns)
  • num_colors the number of different colors of peg. If this parameter is 6, then the players can choose colors 1, 2, 3, 4, 5, and 6.

  • width the width of the board, that is, the number of pegs chosen by the challenger

  • max_turns If the guessing player takes more than this number of turns, they lose.

The function will return a map. This map will contain the representation of the current state of this game. It will also contain any addition information that the rest of our code needs to score each move.

  1. The second function takes the current game state and a guess. It returns the number of black and white pegs that the guess scored.

    def score(game, guess)
    • game is the current game state. Initially this is the state returned by new.

    • guess is a list of integers representing the guess. You can assume this list had exactly width entries.

    The function returns a tuple:

    { gRADE, score, new_state }
    • grade is an atom. :won indicates the last guess was totally correct, (so score: %{ black: 4, white: 0}). :lost means the last guess was incorrect and the player is out of turns. :continue means that the guess has been scored and the game is expecting a next guess.

    • score is a map containing two keys, :black and white. The values corresponding to each key are the number of exact and inexact matches scored. Both keys must be present, even if their value is zero.

    • new_state is the updated state (for example, the number of turns left will have been decremented).

Please follow this API, as we'll be using it later to build interfaces to the game. (I'll also be using it to run some tests here.)

Please submit via either a GitHub collaboration (I'm pragdave) or by emailing me the file (dave@pragdave.me)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment