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

Rock Paper Scissors Pattern Matching

deprecated_rps_pattern_matching.livemd

Rock Paper Scissors Pattern Matching

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 Replacing NilsMetric Conversion

Rock Paper Scissors Using Pattern Matching

You’re going to recreate/refactor your rock paper scissors game using pattern matching.

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

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

Example Solution

Using multiple function clauses.

defmodule RockPaperScissorsPatternMatching do
  def play(:rock, :scissors), do: "rock beats scissors!"
  def play(:paper, :rock), do: "paper beats rock!"
  def play(:scissors, :paper), do: "scissors beats paper!"
  def play(:rock, :paper), do: "paper beats rock!"
  def play(:paper, :scissors), do: "scissors beats paper!"
  def play(:scissors, :rock), do: "rock beats scissors!"
  def play(_same, _same), do: "draw!"
end

Here’s how we might accomplish the same thing using case.

defmodule RockPaperScissorsPatternMatching do
  def play(guess1, guess2) do
    case {guess1, guess2} do
      {:rock, :scissors} -> "rock beats scissors!"
      {:paper, :rock} -> "paper beats rock!"
      {:scissors, :paper} -> "scissors beats paper!"
      {:rock, :paper} -> "paper beats rock!"
      {:paper, :scissors} -> "scissors beats paper!"
      {:scissors, :rock} -> "rock beats scissors!"
      {_same, _same} -> "draw!"
    end
  end
end

Implement the RockPaperScissorsPatternMatching module as documented below.

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

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

  ## Examples

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

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

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

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

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

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

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

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

    iex> RockPaperScissorsPatternMatching.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 Pattern Matching 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 Replacing NilsMetric Conversion