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

Guessing Games

guessing_games.livemd

Guessing Games

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

Guess the Word

In the Elixir cell below create a word guessing game.

  • Bind a variable guess to the player’s guess as a string.
  • Bind a variable answer to the actual answer as a string.
  • Check that the guess matches the answer. Bind correct to "Correct!" if the answer and guess match or "Incorrect." if they don’t.

Run the code with a correct and incorrect guess to ensure your code passes the tests.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "Exercise" do
    try do
      Process.flag(:trap_exit, true)
      guess = "hangman"
      answer = "hangman"

      correct = if guess == answer, do: "Correct!", else: "Incorrect."

      assert Enum.all?([guess, answer, correct], &is_binary/1),
             "Ensure `guess`, `answer`, and `correct` are all strings"

      if guess == answer do
        assert correct == "Correct!"
      else
        assert correct == "Incorrect."
      end
    catch
      error ->
        flunk("""
          Your solution threw the following error:

          #{inspect(error)}
        """)

      :exit, {error, {GenServer, message_type, [_pid, message, _timeout]}} ->
        flunk("""
            GenServer crashed with the following error:

            #{inspect(error)}

            When it recieved: #{inspect(message)} #{message_type}

            Likely you need to define the corresponding handler for #{inspect(message)}.

            Ensure you defined a `handle_call/3`, `handle_info/2`, or `handle_cast/2` or appropriate handler function.

              def handle_call(:message, _from, state) do
                ...
              end

            Then ensure you call `GenServer.call/2`, `GenServer.cast/2`, or otherwise send the message correctly.

              GenServer.call(pid, :message)
        """)

      :exit, error ->
        flunk("""
          Unhandled exit with the following error:

          #{inspect(error)}
        """)
    after
      # all warnings and errors are printed to the previous Kino Frame
      # to avoid cluttering the test results display.
      Process.sleep(10)
      Kino.render(Kino.Markdown.new("### Test Results 

"
)) end end end ExUnit.run() # Make variables and modules defined in the test available. # Also allows for exploration using the output of the cell. # Unfortunately, this results in duplication of warnings. guess = "hangman" answer = "hangman" correct = if guess == answer, do: "Correct!", else: "Incorrect."

Guess the Number

In the Elixir cell below create a number guessing game.

  • Bind a variable guess to the player’s guess as an integer.
  • Bind a variable answer to the actual answer as an integer.
  • If guess equals answer bind correct to "Correct!"
  • If guess is less than answer bind correct to "Too low!"
  • If guess is greater than answer bind correct to "Too high!"

Run your code with an equal, smaller, and larger guess than the answer to ensure your code passes all tests.

ExUnit.start(auto_run: false)

defmodule Assertion do
  use ExUnit.Case

  test "Exercise" do
    try do
      Process.flag(:trap_exit, true)
      guess = 12
      answer = 13

      correct =
        cond do
          guess == answer -> "Correct!"
          guess < answer -> "Too low!"
          guess > answer -> "Too high!"
        end

      assert is_integer(guess), "Ensure `guess` is an integer"
      assert is_integer(answer), "Ensure `answer` is an integer"
      assert is_binary(correct), "Ensure `correct` is a string"

      cond do
        guess == answer -> assert correct == "Correct!"
        guess < answer -> assert correct == "Too low!"
        guess > answer -> assert correct == "Too high!"
      end
    catch
      error ->
        flunk("""
          Your solution threw the following error:

          #{inspect(error)}
        """)

      :exit, {error, {GenServer, message_type, [_pid, message, _timeout]}} ->
        flunk("""
            GenServer crashed with the following error:

            #{inspect(error)}

            When it recieved: #{inspect(message)} #{message_type}

            Likely you need to define the corresponding handler for #{inspect(message)}.

            Ensure you defined a `handle_call/3`, `handle_info/2`, or `handle_cast/2` or appropriate handler function.

              def handle_call(:message, _from, state) do
                ...
              end

            Then ensure you call `GenServer.call/2`, `GenServer.cast/2`, or otherwise send the message correctly.

              GenServer.call(pid, :message)
        """)

      :exit, error ->
        flunk("""
          Unhandled exit with the following error:

          #{inspect(error)}
        """)
    after
      # all warnings and errors are printed to the previous Kino Frame
      # to avoid cluttering the test results display.
      Process.sleep(10)
      Kino.render(Kino.Markdown.new("### Test Results 

"
)) end end end ExUnit.run() # Make variables and modules defined in the test available. # Also allows for exploration using the output of the cell. # Unfortunately, this results in duplication of warnings. guess = 12 answer = 13 correct = cond do guess == answer -> "Correct!" guess < answer -> "Too low!" guess > answer -> "Too high!" 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 guessing games exercise"