diff --git a/lib/islands_engine/rules.ex b/lib/islands_engine/rules.ex index 001db91..c51a19c 100644 --- a/lib/islands_engine/rules.ex +++ b/lib/islands_engine/rules.ex @@ -66,6 +66,26 @@ defmodule IslandEngine.Rules do end end + def check(%Rules{state: :player1_turn} = rules, {:guess_coordinate, :player1}), + do: {:ok, %Rules{rules | state: :player2_turn}} + + def check(%Rules{state: :player1_turn} = rules, {:win_check, win_or_not}) do + case win_or_not do + :no_win -> {:ok, rules} + :win -> {:ok, %Rules{rules | state: :game_over}} + end + end + + def check(%Rules{state: :player2_turn} = rules, {:guess_coordinate, :player2}), + do: {:ok, %Rules{rules | state: :player1_turn}} + + def check(%Rules{state: :player2_turn} = rules, {:win_check, win_or_not}) do + case win_or_not do + :no_win -> {:ok, rules} + :win -> {:ok, %Rules{rules | state: :game_over}} + end + end + def check(_state, _action), do: :error defp both_players_islands_set?(rules),