Game draw! Building the code and make the tests pass
Tags: elixir, algorithms, webdev, tutorial
Published: 2022-08-30 04:07:00

---
series: Rock, Paper, and Scissors with Elixir
---

Now we'll code our module **Game** following the tests.

```Elixir
# lib/game.ex

defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """
end
```

> If we run the tests now:

```Elixir
mix test
```

> We'll see the tests failures:

```Bash
  1) test Game.play/2 when the players draw when all players choose scissor (GameTest)
     test/game_test.exs:27
     ** (UndefinedFunctionError) function Game.play/2 is undefined or private
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) Game.play(3, 3)
       test/game_test.exs:31: (test)


  2) test Game.play/2 when the players draw when all players choose paper (GameTest)
     test/game_test.exs:18
     ** (UndefinedFunctionError) function Game.play/2 is undefined or private
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) Game.play(2, 2)
       test/game_test.exs:22: (test)


  3) test Game.play/2 when the players draw when all players choose stone (GameTest)
     test/game_test.exs:9
     ** (UndefinedFunctionError) function Game.play/2 is undefined or private
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) Game.play(1, 1)
       test/game_test.exs:13: (test)



Finished in 0.02 seconds (0.00s async, 0.02s sync)
3 tests, 3 failures
```

### Let's make those tests pass

Firstly, we must create the function play passing two arguments, first_player_choice and second_player_choice:

```Elixir
defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """

  def play(first_player_choice, second_player_choice) do
  end
end

```

And then let's rerun the tests:

```Bash
mix test
```

```Bash
 1) test Game.play/2 when the players draw when all players choose stone (GameTest)
     test/game_test.exs:9
     match (=) failed
     code:  assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     left:  {:ok, match}
     right: nil
     stacktrace:
       test/game_test.exs:13: (test)



  2) test Game.play/2 when the players draw when all players choose scissor (GameTest)
     test/game_test.exs:27
     match (=) failed
     code:  assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     left:  {:ok, match}
     right: nil
     stacktrace:
       test/game_test.exs:31: (test)



  3) test Game.play/2 when the players draw when all players choose paper (GameTest)
     test/game_test.exs:18
     match (=) failed
     code:  assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     left:  {:ok, match}
     right: nil
     stacktrace:
       test/game_test.exs:22: (test)



Finished in 0.03 seconds (0.00s async, 0.03s sync)
3 tests, 3 failures
```

Now we need to guarantee that the game will draw if the player chooses the same item:

```Elixir
defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """

  def play(first_player_choice, second_player_choice) do
    result(first_player_choice, second_player_choice)
  end

  defp result(first_player_choice, second_player_choice) do
    cond do
      first_player_choice == second_player_choice -> {:ok, "Draw!"}
    end
  end
end

```

And now, if we rerun the tests:

```Bash
mix test
```

Everything passed, and everything was ok.

```Bash
...

Finished in 0.02 seconds (0.00s async, 0.02s sync)
3 tests, 0 failures

Randomized with seed 352470
```

In the next post, we'll build the tests covering the scenario when the first player wins.

#### Contacts
Email: contato@diegonovais.com.br
Linkedin: https://www.linkedin.com/in/diegonovais/
Twitter: https://twitter.com/diegonovaistech