Skip to content

Instantly share code, notes, and snippets.

@pragdave
Last active September 24, 2019 02:20

Revisions

  1. pragdave revised this gist Apr 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion assignment3.md
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ participate in that round's scoring.
    The function returns a tuple:

    ~~~ elixir
    { score, new_state }
    { gRADE, score, new_state }
    ~~~

    * `grade`
  2. pragdave created this gist Apr 20, 2018.
    99 changes: 99 additions & 0 deletions assignment3.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    # 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:

    ~~~ elixir
    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.

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

    ~~~ elixir
    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:

    ~~~ elixir
    { 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`)