첫 번째 플레이어가 승리합니다! 코드 작성 및 테스트 통과
17503 단어 tutorialwebdevelixiralgorithms
Game 모듈은 다음과 같습니다.
# lib/game.ex
defmodule Game do
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
end
With our tests done, if we run the tests now:
mix test
We'll see the tests failures:
1) test Game.play/2 when first player wins when first player chooses scissors and second player chooses paper (GameTest)
test/game_test.exs:38
** (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:12: Game.result/2
test/game_test.exs:42: (test)
2) test Game.play/2 when first player wins when first player chooses stone and second player chooses scissors (GameTest)
test/game_test.exs:56
** (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:12: Game.result/2
test/game_test.exs:60: (test)
3) test Game.play/2 when first player wins when first player chooses paper and second player chooses stone (GameTest)
test/game_test.exs:47
** (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:12: Game.result/2
test/game_test.exs:51: (test)
..
Finished in 0.03 seconds (0.00s async, 0.03s sync)
6 tests, 3 failures
테스트를 통과하도록 합시다
이제 돌, 종이, 가위에 대한 몇 가지 모듈 속성을 만들어야 합니다.
defmodule Game do
@moduledoc """
Documentation for `Game`.
"""
@stone 1
@paper 2
@scissor 3
# ...
end
그런 다음 테스트에 따라 첫 번째 플레이어가 승자임을 보장하는 조건을 추가합니다.
첫 번째 플레이어가 가위를 선택하고 두 번째 플레이어가 종이를 선택할 때
첫 번째 플레이어가 가위를 선택하고 두 번째 플레이어가 종이를 선택할 때 조건을 추가합니다.
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!"}
first_player_choice == @scissor && second_player_choice == @paper ->
{:ok, "First player win!!!"}
end
end
end
이제 테스트를 다시 실행하면 다음과 같습니다.
mix test
실제 시나리오로 테스트를 통과했습니다. 이제 실패한 테스트가 두 번뿐입니다.
Ps.: We have one warning message about the module attributes @stone that was set but never used.
Compiling 1 file (.ex)
warning: module attribute @stone was set but never used
lib/game.ex:5
...
1) test Game.play/2 when first player wins when first player chooses stone and second player chooses scissors (GameTest)
test/game_test.exs:56
** (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:18: Game.result/2
test/game_test.exs:60: (test)
.
2) test Game.play/2 when first player wins when first player chooses paper and second player chooses stone (GameTest)
test/game_test.exs:47
** (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:18: Game.result/2
test/game_test.exs:51: (test)
Finished in 0.03 seconds (0.00s async, 0.03s sync)
6 tests, 2 failures
Randomized with seed 797736
...
Finished in 0.02 seconds (0.00s async, 0.02s sync)
3 tests, 0 failures
Randomized with seed 352470
첫 번째 플레이어가 종이를 선택하고 두 번째 플레이어가 돌을 선택할 때
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!"}
first_player_choice == @scissor && second_player_choice == @paper ->
{:ok, "First player win!!!"}
first_player_choice == @paper && second_player_choice == @stone ->
{:ok, "First player win!!!"}
end
end
end
이제 테스트를 다시 실행하면 다음과 같습니다.
mix test
실제 시나리오로 테스트를 통과했습니다. 이제 실패한 테스트가 하나뿐입니다.
Compiling 1 file (.ex)
..
1) test Game.play/2 when first player wins when first player chooses stone and second player chooses scissors (GameTest)
test/game_test.exs:56
** (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:21: Game.result/2
test/game_test.exs:60: (test)
...
Finished in 0.03 seconds (0.00s async, 0.03s sync)
6 tests, 1 failure
Randomized with seed 450094
첫 번째 플레이어가 돌을 선택하고 두 번째 플레이어가 가위를 선택한 경우
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!"}
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
이제 테스트를 다시 실행하면 다음과 같습니다.
mix test
모든 테스트가 성공적으로 통과됩니다.
Compiling 1 file (.ex)
......
Finished in 0.03 seconds (0.00s async, 0.03s sync)
6 tests, 0 failures
Randomized with seed 795123
다음 포스트에서는 두 번째 플레이어가 이겼을 때의 시나리오를 다루는 테스트를 빌드할 것입니다.
콘택트 렌즈
이메일: [email protected]
링크드인:
트위터:
Reference
이 문제에 관하여(첫 번째 플레이어가 승리합니다! 코드 작성 및 테스트 통과), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/dnovais/first-player-wins-building-the-code-and-make-the-tests-pass-38if텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)