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
If we run the tests now:
mix test
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
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
And now, if we rerun the tests:
mix test
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
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
And now, if we rerun the tests:
mix test
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
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
And now, if we rerun the tests:
mix test
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
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)