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

Elixir Bootcamp - Booleans

livebooks/002-booleans.livemd

Elixir Bootcamp - Booleans

Booleans

Elixir represents true and false values with the boolean type. There are only two values: true and false. These values can be bound to a variable:

true_variable = true
false_variable = false

We can evaluate strict boolean expressions using the and/2, or/2, and not/1 operators.

true_variable = true and true
false_variable = true and false

true_variable = false or true
false_variable = false or false

true_variable = not false
false_variable = not true

When writing a function that returns a boolean value, it is idiomatic to end the function name with a ?. The same convention can be used for variables that store boolean values.

def either_true?(a?, b?) do
  a? or b?
end

Exercise - Pacman

In this exercise, you need to translate some rules from the classic game Pac-Man into Elixir functions.

You have four rules to translate, all related to the game states.

1. Define if Pac-Man eats a ghost

Define the Rules.eat_ghost?/2 function that takes two arguments (if Pac-Man has a power pellet active and if Pac-Man is touching a ghost) and returns a boolean value if Pac-Man is able to eat the ghost. The function should return true only if Pac-Man has a power pellet active and is touching a ghost.

Rules.eat_ghost?(false, true)
# => false

2. Define if Pac-Man scores

Define the Rules.score?/2 function that takes two arguments (if Pac-Man is touching a power pellet and if Pac-Man is touching a dot) and returns a boolean value if Pac-Man scored. The function should return true if Pac-Man is touching a power pellet or a dot.

Rules.score?(true, true)
# => true

3. Define if Pac-Man loses

Define the Rules.lose?/2 function that takes two arguments (if Pac-Man has a power pellet active and if Pac-Man is touching a ghost) and returns a boolean value if Pac-Man loses. The function should return true if Pac-Man is touching a ghost and does not have a power pellet active.

Rules.lose?(false, true)
# => true

4. Define if Pac-Man wins

Define the Rules.win?/3 function that takes three arguments (if Pac-Man has eaten all of the dots, if Pac-Man has a power pellet active, and if Pac-Man is touching a ghost) and returns a boolean value if Pac-Man wins. The function should return true if Pac-Man has eaten all of the dots and has not lost based on the arguments defined in part 3.

Rules.win?(false, true, false)
# => false

Implementation

defmodule Rules do
  def eat_ghost?(power_pellet_active?, touching_ghost?) do
    # Please implement the eat_ghost?/2 function
  end

  def score?(touching_power_pellet?, touching_dot?) do
    # Please implement the score?/2 function
  end

  def lose?(power_pellet_active?, touching_ghost?) do
    # Please implement the lose?/2 function
  end

  def win?(has_eaten_all_dots?, power_pellet_active?, touching_ghost?) do
    # Please implement the win?/3 function
  end
end

Tests

ExUnit.start(autorun: false)

defmodule RulesTest do
  use ExUnit.Case

  describe "eat_ghost?/2" do
    @tag task_id: 1
    test "ghost gets eaten" do
      assert Rules.eat_ghost?(true, true)
    end

    @tag task_id: 1
    test "ghost does not get eaten because no power pellet active" do
      refute Rules.eat_ghost?(false, true)
    end

    @tag task_id: 1
    test "ghost does not get eaten because not touching ghost" do
      refute Rules.eat_ghost?(true, false)
    end

    @tag task_id: 1
    test "ghost does not get eaten because no power pellet is active, even if not touching ghost" do
      refute Rules.eat_ghost?(false, false)
    end
  end

  describe "score?/2" do
    @tag task_id: 2
    test "score when eating dot" do
      assert Rules.score?(false, true)
    end

    @tag task_id: 2
    test "score when eating power pellet" do
      assert Rules.score?(true, false)
    end

    @tag task_id: 2
    test "no score when nothing eaten" do
      refute Rules.score?(false, false)
    end
  end

  describe "lose?/2" do
    @tag task_id: 3
    test "lose if touching a ghost without a power pellet active" do
      assert Rules.lose?(false, true)
    end

    @tag task_id: 3
    test "don't lose if touching a ghost with a power pellet active" do
      refute Rules.lose?(true, true)
    end

    @tag task_id: 3
    test "don't lose if not touching a ghost" do
      refute Rules.lose?(true, false)
    end
  end

  describe "win?/3" do
    @tag task_id: 4
    test "win if all dots eaten" do
      assert Rules.win?(true, false, false)
    end

    @tag task_id: 4
    test "don't win if all dots eaten, but touching a ghost" do
      refute Rules.win?(true, false, true)
    end

    @tag task_id: 4
    test "win if all dots eaten and touching a ghost with a power pellet active" do
      assert Rules.win?(true, true, true)
    end
  end
end

ExUnit.run()

Previous Page Next Page