DEV Community

Cover image for Game draw! Building the code and make the tests pass
Diego Novais
Diego Novais

Posted on • Edited on

Game draw! Building the code and make the tests pass

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

# lib/game.ex

defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """
end
Enter fullscreen mode Exit fullscreen mode

If we run the tests now:

mix test
Enter fullscreen mode Exit fullscreen mode

We'll see the tests failures:

  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
Enter fullscreen mode Exit fullscreen mode

Let's make those tests pass

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

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

  def play(first_player_choice, second_player_choice) do
  end
end

Enter fullscreen mode Exit fullscreen mode

And then let's rerun the tests:

mix test
Enter fullscreen mode Exit fullscreen mode
 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
Enter fullscreen mode Exit fullscreen mode

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

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

Enter fullscreen mode Exit fullscreen mode

And now, if we rerun the tests:

mix test
Enter fullscreen mode Exit fullscreen mode

Everything passed, and everything was ok.

...

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

Randomized with seed 352470
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)