DEV Community

Cover image for Second player wins! Building the code and make the tests pass
Diego Novais
Diego Novais

Posted on

Second player wins! Building the code and make the tests pass

Now, according to our tests, we'll add the condition that guarantees that the second player is the winner.

Our Game module looks like this:

# lib/game.ex

defmodule Game do
  @moduledoc """
  Documentation for `Game`.
  """
  @stone 1
  @paper 2
  @scissor 3

  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!"}

      first_player_choice == @scissor && second_player_choice == @paper ->
        {:ok, "First player win!!!"}

      first_player_choice == @paper && second_player_choice == @stone ->
        {:ok, "First player win!!!"}

      first_player_choice == @stone && second_player_choice == @scissor ->
        {:ok, "First player win!!!"}
    end
  end
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 second player wins when second player chooses stone and first player chooses scissors (GameTest)
     test/game_test.exs:85
     ** (CondClauseError) no cond clause evaluated to a truthy value
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) lib/game.ex:24: Game.result/2
       test/game_test.exs:89: (test)



  2) test Game.play/2 when second player wins when second player chooses scissors and first player chooses paper (GameTest)
     test/game_test.exs:67
     ** (CondClauseError) no cond clause evaluated to a truthy value
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) lib/game.ex:24: Game.result/2
       test/game_test.exs:71: (test)

...

  3) test Game.play/2 when second player wins when second player chooses paper and first player chooses stone (GameTest)
     test/game_test.exs:76
     ** (CondClauseError) no cond clause evaluated to a truthy value
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) lib/game.ex:24: Game.result/2
       test/game_test.exs:80: (test)

.

Finished in 0.05 seconds (0.00s async, 0.05s sync)
9 tests, 3 failures

Randomized with seed 710365
Enter fullscreen mode Exit fullscreen mode

When the first player chooses paper and the second player chooses scissors

We'll add the condition when the first player chooses paper and the second player chooses scissors:

# lib/game.ex

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 == @paper && second_player_choice == @scissor ->
        {:ok, "Second player win!!!"}
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

And now, if we rerun the tests:

mix test
Enter fullscreen mode Exit fullscreen mode

The test with the actual scenario passed ok. And now we have just two tests with failure.

Compiling 1 file (.ex)
.

  1) test Game.play/2 when second player wins when second player chooses stone and first player chooses scissors (GameTest)
     test/game_test.exs:85
     ** (CondClauseError) no cond clause evaluated to a truthy value
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) lib/game.ex:27: Game.result/2
       test/game_test.exs:89: (test)

.....

  2) test Game.play/2 when second player wins when second player chooses paper and first player chooses stone (GameTest)
     test/game_test.exs:76
     ** (CondClauseError) no cond clause evaluated to a truthy value
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) lib/game.ex:27: Game.result/2
       test/game_test.exs:80: (test)

.

Finished in 0.04 seconds (0.00s async, 0.04s sync)
9 tests, 2 failures

Randomized with seed 862104
Enter fullscreen mode Exit fullscreen mode

When the first player chooses stone and the second player chooses paper

  • We'll add the condition when the first player chooses stone and the second player chooses paper:
# lib/game.ex

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 == @paper && second_player_choice == @scissor ->
        {:ok, "Second player win!!!"}

      first_player_choice == @stone && second_player_choice == @paper ->
        {:ok, "Second player win!!!"}
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

And now, if we rerun the tests:

mix test
Enter fullscreen mode Exit fullscreen mode

The test with the actual scenario passed ok. And now we have just one tests with failure.

Compiling 1 file (.ex)


  1) test Game.play/2 when second player wins when second player chooses stone and first player chooses scissors (GameTest)
     test/game_test.exs:85
     ** (CondClauseError) no cond clause evaluated to a truthy value
     code: assert {:ok, match} = Game.play(first_player_choice, second_player_choise)
     stacktrace:
       (rock_paper_scissor_elixir 0.1.0) lib/game.ex:30: Game.result/2
       test/game_test.exs:89: (test)

........

Finished in 0.04 seconds (0.00s async, 0.04s sync)
9 tests, 1 failure

Randomized with seed 980583
Enter fullscreen mode Exit fullscreen mode

When first player chooses scissors and the the second player chooses stone

  • We'll add the condition when the first player chooses scissors and the second player chooses stone:
# lib/game.ex

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 == @paper && second_player_choice == @scissor ->
        {:ok, "Second player win!!!"}

      first_player_choice == @stone && second_player_choice == @paper ->
        {:ok, "Second player win!!!"}

      first_player_choice == @scissor && second_player_choice == @stone ->
        {:ok, "Second player win!!!"}
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

And now, if we rerun the tests:

mix test
Enter fullscreen mode Exit fullscreen mode

All the tests pass with success.

Compiling 1 file (.ex)
......

Finished in 0.03 seconds (0.00s async, 0.03s sync)
6 tests, 0 failures

Randomized with seed 795123
Enter fullscreen mode Exit fullscreen mode

In the next post (soon), we'll refactor and improve our module Game.

Contacts

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

Top comments (0)