두 번째 플레이어가 승리합니다! 코드 작성 및 테스트 통과
17160 단어 tutorialwebdevelixiralgorithms
Game 모듈은 다음과 같습니다.
# 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
첫 번째 플레이어가 종이를 선택하고 두 번째 플레이어가 가위를 선택할 때
첫 번째 플레이어가 종이를 선택하고 두 번째 플레이어가 가위를 선택할 때 조건을 추가합니다.
# 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
이제 테스트를 다시 실행하면 다음과 같습니다.
mix test
실제 시나리오로 테스트를 통과했습니다. 이제 실패한 테스트가 두 번뿐입니다.
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
첫 번째 플레이어가 돌을 선택하고 두 번째 플레이어가 종이를 선택할 때
# 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
이제 테스트를 다시 실행하면 다음과 같습니다.
mix test
실제 시나리오로 테스트를 통과했습니다. 이제 실패한 테스트가 하나뿐입니다.
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
첫 번째 플레이어가 가위를 선택하고 두 번째 플레이어가 돌을 선택할 때
# 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
이제 테스트를 다시 실행하면 다음과 같습니다.
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/second-player-wins-building-the-code-and-make-the-tests-pass-5dh3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)