Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Rock Paper Scissors

custom_rock_paper_scissors.livemd

Rock Paper Scissors

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

Navigation

Return Home Report An Issue

Custom Rock Paper Scissors

You’re going to create a rock paper scissors game where users can create their own choices.

  • Create a CustomGame struct.
  • Create required keys for :rock, :paper, :scissors.
  • Create a CustomGame.new/3 function that allows users to enter alternate atoms for :rock, :paper, and :scissors.
  • Create a CustomGame.play/3 which expects the return value of CustomGame.create_game/3 as a parameter. The second and third parameter should expect custom atoms the user previously defined.

For example,

game = CustomGame.new(:grass, :fire, :water)

CustomGame.play(game, :fire, :water)
"water beats fire"

CustomGame.play(game, :water, :water)
"draw"
ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    defmodule CustomGame do
    end

    assert 3 == Keyword.get(CustomGame.__info__(:functions), :new),
           "Ensure you define the `new/3` function"

    assert Keyword.get(CustomGame.__info__(:functions), :__struct__),
           "Ensure you use `defstruct`."

    assert match?(%{rock: _, paper: _, scissors: _}, struct(CustomGame)),
           "Ensure you use `defstruct` with :rock, :paper, and :scissors."

    assert %{rock: :custom_rock, paper: :custom_paper, scissors: :custom_scissors} =
             CustomGame.new(:custom_rock, :custom_paper, :custom_scissors)

    assert 3 == Keyword.get(CustomGame.__info__(:functions), :play),
           "Ensure you define the `play/3` function"

    game = CustomGame.new(:custom_rock, :custom_paper, :custom_scissors)

    beats? = fn p1, p2 ->
      {p1, p2} in [
        {:custom_rock, :custom_scissors},
        {:custom_paper, :custom_rock},
        {:custom_scissors, :custom_paper}
      ]
    end

    for player1 <- [:custom_rock, :custom_paper, :custom_scissors],
        player2 <- [:custom_rock, :custom_paper, :custom_scissors] do
      result = CustomGame.play(game, player1, player2)

      expected_result =
        cond do
          beats?.(player1, player2) -> "#{player1} beats #{player2}"
          beats?.(player2, player1) -> "#{player2} beats #{player1}"
          true -> "draw"
        end

      assert result == expected_result
    end
  end
end

ExUnit.run()

# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
defmodule CustomGame do
end

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish custom rock paper scissors exercise"