Skip to content

Commit

Permalink
Transition game between players and achieve game win
Browse files Browse the repository at this point in the history
  • Loading branch information
jrowah committed Dec 26, 2023
1 parent 8fcab4a commit c471cdf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/islands_engine/rules.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit c471cdf

Please sign in to comment.