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

이제 우리는 테스트에 따라 모듈 게임을 계속 코딩할 것입니다. 그러나 이번에는 첫 번째 플레이어가 이기는 시나리오를 다룹니다.

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

    좋은 웹페이지 즐겨찾기