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

Rock Paper Scissors Lizard Spock

rock_paper_scissors_lizard_spock.livemd

Rock Paper Scissors Lizard Spock

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

Exercise

In the Elixir cell below, you’re going to create a RockPaperScissorsLizardSpock module with a RockPaperScissorsLizardSpock.play/2 function.

The play/2 function will accept input from two players. The input will be either :rock, :paper, :scissors, :lizard, or :spock.

The play/2 function should return "____ beats ____." depending on who wins. If it’s a draw, then return "Tie game!"

The rules to rock paper scissors lizard spock start normal:

  • Rock beats Scissors.
  • Paper beats Rock.
  • Scissors beats Paper.

Then add the following extra rules

  • Rock beats Lizard.
  • Lizard beats Spock.
  • Spock beats Scissors.
  • Scissors beats Lizard.
  • Lizard beats Paper.
  • Paper beats Spock.
  • Spock beats Rock.

For example,

RockPaperScissorsLizardSpock.play(:lizard, :spock)
":lizard beats :spock"
ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "" do
    defmodule RockPaperScissorsLizardSpock do
    end

    assert Keyword.has_key?(RockPaperScissorsLizardSpock.__info__(:functions), :play),
           "Ensure you define the `play/2` function"

    game_permutations =
      for player1 <- [:rock, :paper, :scissors, :lizard, :spock],
          player2 <- [:rock, :paper, :scissors, :lizard, :spock] do
        {player1, player2}
      end

    beats? = fn player1, player2 ->
      {player1, player2} in [
        {:rock, :scissors},
        {:paper, :rock},
        {:scissors, :paper},
        {:rock, :lizard},
        {:lizard, :spock},
        {:scissors, :lizard},
        {:lizard, :paper},
        {:paper, :spock},
        {:spock, :scissors},
        {:spock, :rock}
      ]
    end

    Enum.each(game_permutations, fn {p1, p2} ->
      expected_result =
        cond do
          beats?.(p1, p2) -> "#{p1} beats #{p2}."
          beats?.(p2, p1) -> "#{p2} beats #{p1}."
          true -> "Tie game!"
        end

      actual = RockPaperScissorsLizardSpock.play(p1, p2)

      assert actual == expected_result,
             """
             Failed on RockPaperScissorsLizardSpock.play(:#{p1}, :#{p2}).
             expected: #{inspect(expected_result)}
             received: #{inspect(actual)}
             """
    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 RockPaperScissorsLizardSpock 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 rock paper scissors lizard spock exercise"