두 번째 플레이어가 승리합니다! 코드 작성 및 테스트 통과

이제 테스트에 따라 두 번째 플레이어가 승자임을 보장하는 조건을 추가합니다.

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]
    링크드인:
    트위터:

    좋은 웹페이지 즐겨찾기