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([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue StructsRPG Dialogue

Rock Paper Scissors Lizard Spock

YouTube.new("https://www.youtube.com/watch?v=_PUEoDYpUyQ")

You’re going to create a Rock Paper Scissors Lizard Spock game. Rock Paper Scissors Lizard Spock is a more complex adaptation of Rock Paper Scissors.

The rules start the same as normal rock paper scissors.

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

Then we 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.

We’ll represent these choices as atoms :rock, :paper, :scissors, :lizard, :spock.

  • Create a beats?/2 function which determines if once choice beats another.
RockPaperScissorsLizardSpock.beats?(:rock, :lizard) # true
  • Create a play/2 function which determines if player 1 or player 2 won the game.
RockPaperScissorsLizardSpock.play(:rock, :lizard) # "Player 1 Wins!"
RockPaperScissorsLizardSpock.play(:lizard, :rock) # "Player 1 Wins!"
RockPaperScissorsLizardSpock.play(:lizard, :lizard) # "Draw!"

Example Solution

  def beats?(guess1, guess2) do
    case {guess1, guess2} do
      {:rock, :scissors} -> true
      {:rock, :lizard} -> true
      {:paper, :rock} -> true
      {:paper, :spock} -> true
      {:scissors, :paper} -> true
      {:scissors, :lizard} -> true
      {:lizard, :spock} -> true
      {:lizard, :paper} -> true
      {:spock, :scissors} -> true
      {:spock, :rock} -> true
      _answer -> false
    end
  end

  def play(player1, player2) do
    cond do
      beats?(player1, player2) -> "Player 1 Wins!"
      beats?(player2, player1) -> "Player 2 Wins!"
      true -> "Draw"
    end
  end

Implement the RockPaperScissorsLizardSpock module below.

> @doc syntax runs tests on your code to provide feedback. However, be aware that for the sake of conciseness we do not always write fully comprehensive tests. You solution may pass tests, but be incomplete. You may press the dropdown button to hide the @doc documentation/tests.

defmodule RockPaperScissorsLizardSpock do
  def beats?(guess1, guess2) do
    {guess1, guess2} in [
      {:rock, :scissors},
      {:scissors, :paper},
      {:paper, :rock},
      {:rock, :lizard},
      {:lizard, :spock},
      {:spock, :scissors},
      {:scissors, :lizard},
      {:lizard, :paper},
      {:paper, :spock},
      {:spock, :rock},
    ]
  end

  def play(player1, player2) do
    cond do
      player1 === player2 -> "Draw!"
      beats?(player1, player2) -> "Player 1 Wins!"
      true -> "Player 2 Wins!"
    end
  end
end

RockPaperScissorsLizardSpock.play(:lizard, :lizard)

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Rock Paper Scissors Lizard Spock exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue StructsRPG Dialogue