Powered by AppSignal & Oban Pro

Rock Paper Scissors

exercises/rock_paper_scissors.livemd

Rock Paper Scissors

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.8.0", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Create the Perfect AI

You’re going to create the perfect AI for rock paper scissors that will always win.

flowchart LR
scissors --beats--> paper --beats--> rock --beats--> scissors

Generate a random player choice of :rock ,:paper, or :scissors or manually enter :rock, :paper, and :scissors to determine your program works correctly.

player_choice = Enum.random([:rock, :paper, :scissors])

Then, return the winning choice of either :rock, :paper, or :scissors that beats the player’s choice.

Example solution

  player_choice = :scissors
  
  case player_choice do
    :rock -> :paper
    :paper -> :scissors
    :scissors -> :rock
  end

Enter your solution below.

Create Two Player Rock Paper Scissors

Now that you know how to create a rock paper scissors AI, you’re going to create a two player game of rock paper scissors.

Bind a player1_choice and player2_choice variable to :rock, :paper, or :scissors.

  • If player1_choice beats player2_choice, return "Player 1 wins!".

  • If player2_choice beats player1_choice, return "Player 2 wins!".

  • If both players have the same choice, then return "Draw".

    Example solution

    player1 = :rock
    player2 = :scissors
    
    case {player1, player2} do
      {:rock, :scissors} -> "Player 1 Wins!"
      {:paper, :rock} -> "Player 1 Wins!"
      {:scissors, :paper} -> "Player 1 Wins!"
      {:rock, :paper} -> "Player 2 Wins!"
      {:paper, :scissors} -> "Player 2 Wins!"
      {:scissors, :rock} -> "Player 2 Wins!"
      {_same, _same} -> "Draw"
    end

    You can also reduce code repetition using functions and the in keyword to check if the value exists in a list.

    player1 = :rock
    player2 = :scissors
    
    beats? = fn choice1, choice2 ->
      {choice1, choice2} in [{:rock, :scissors}, {:paper, :rock}, {:scissors, :paper}]
    end
    
    cond do
      beats?(player1, player2) -> "Player1"
      beats?(player2, player1) -> "Player2"
      player1 == player2 -> "Draw"
    end

Enter your solution below.

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "rock_paper_scissors_reading"
    "exercises" -> "rock_paper_scissors_exercise"
  end

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, save_name, false)

form =
  Kino.Control.form(
    [
      completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
    ],
    report_changes: true
  )

Task.async(fn ->
  for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
    File.write!(
      progress_path,
      Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
    )
  end
end)

form

Commit Your Progress

Run the following in your command line from the curriculum folder to track and save your progress in a Git commit. Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.

$ git checkout -b rock-paper-scissors-exercise
$ git add .
$ git commit -m "finish rock paper scissors exercise"
$ git push origin rock-paper-scissors-exercise

Create a pull request from your rock-paper-scissors-exercise branch to your solutions branch. Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.

DockYard Academy Students Only:

Notify your teacher by including @BrooklinJazz in your PR description to get feedback. You (or your teacher) may merge your PR into your solutions branch after review.

If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.

Up Next

Previous Next
Naming Numbers Modules