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

Rock Paper Scissors Guards

exercises/deprecated_rps_guards.livemd

Rock Paper Scissors Guards

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 Math With GuardsProtocols

Rock Paper Scissors Guards

You’re going to recreate your rock paper scissors game using guards.

RockPaperScissors.play(:rock, :paper)
"paper beats rock!"

RockPaperScissors.play(:rock, :rock)
"draw!"

Example Solution

defmodule RockPaperScissorsGuards do
  defguard is_winner(guess1, guess2)
           when {guess1, guess2} in [{:rock, :scissors}, {:paper, :rock}, {:scissors, :paper}]

  def play(guess1, guess2) when is_winner(guess1, guess2) do
    "#{guess1} beats #{guess2}!"
  end

  def play(guess1, guess2) when is_winner(guess2, guess1) do
    "#{guess2} beats #{guess1}!"
  end

  def play(guess1, guess2) when guess1 == guess2 do
    "draw!"
  end
end

Implement the RockPaperScissorsGuards module as documented below.

defmodule RockPaperScissorsGuards do
  @moduledoc """
  Documentation for `RockPaperScissorsGuards`
  """

  @doc """
  Play rock paper scissors. Returns a string to describe who won the game.

  ## Examples

    iex> RockPaperScissorsGuards.play(:rock, :scissors)
    "rock beats scissors!"

    iex> RockPaperScissorsGuards.play(:paper, :rock)
    "paper beats rock!"

    iex> RockPaperScissorsGuards.play(:scissors, :paper)
    "scissors beats paper!"

    iex> RockPaperScissorsGuards.play(:rock, :paper)
    "paper beats rock!"

    iex> RockPaperScissorsGuards.play(:paper, :scissors)
    "scissors beats paper!"

    iex> RockPaperScissorsGuards.play(:scissors, :rock)
    "rock beats scissors!"

    iex> RockPaperScissorsGuards.play(:rock, :rock)
    "draw!"

    iex> RockPaperScissorsGuards.play(:paper, :paper)
    "draw!"

    iex> RockPaperScissorsGuards.play(:scissors, :scissors)
    "draw!"
  """
  def play(guess1, guess2) do
  end
end

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 Guards 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 Math With GuardsProtocols