-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract state calculations into GameState
- Loading branch information
Showing
7 changed files
with
231 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Mjw.GameState do | ||
@doc """ | ||
Calculate the state of a game | ||
""" | ||
def state(%Mjw.Game{} = game) do | ||
{game, :tbd} | ||
|> waiting_for_players | ||
|> picking_winds | ||
|> rolling_for_first_dealer | ||
|> rolling_for_deal | ||
|> win_declared | ||
|> discarding | ||
|> drawing | ||
|> invalid | ||
end | ||
|
||
defp waiting_for_players({game, :tbd}) do | ||
if Mjw.Game.empty_seats_count(game) > 0 do | ||
{game, :waiting_for_players} | ||
else | ||
{game, :tbd} | ||
end | ||
end | ||
|
||
defp waiting_for_players({game, state}), do: {game, state} | ||
|
||
defp picking_winds({game, :tbd}) do | ||
if !Enum.empty?(Mjw.Game.remaining_winds_to_pick(game)) do | ||
{game, :picking_winds} | ||
else | ||
{game, :tbd} | ||
end | ||
end | ||
|
||
defp picking_winds({game, state}), do: {game, state} | ||
|
||
defp rolling_for_first_dealer({game, :tbd}) do | ||
if Enum.empty?(game.dice) do | ||
{game, :rolling_for_first_dealer} | ||
else | ||
{game, :tbd} | ||
end | ||
end | ||
|
||
defp rolling_for_first_dealer({game, state}), do: {game, state} | ||
|
||
defp rolling_for_deal({game, :tbd}) do | ||
if game.turn_state == :rolling do | ||
{game, :rolling_for_deal} | ||
else | ||
{game, :tbd} | ||
end | ||
end | ||
|
||
defp rolling_for_deal({game, state}), do: {game, state} | ||
|
||
defp win_declared({game, :tbd}) do | ||
if Mjw.Game.win_declared_seatno(game) do | ||
{game, :win_declared} | ||
else | ||
{game, :tbd} | ||
end | ||
end | ||
|
||
defp win_declared({game, state}), do: {game, state} | ||
|
||
defp discarding({game, :tbd}) do | ||
if game.turn_state == :discarding do | ||
{game, :discarding} | ||
else | ||
{game, :tbd} | ||
end | ||
end | ||
|
||
defp discarding({game, state}), do: {game, state} | ||
|
||
defp drawing({game, :tbd}) do | ||
if game.turn_state == :drawing do | ||
{game, :drawing} | ||
else | ||
{game, :tbd} | ||
end | ||
end | ||
|
||
defp drawing({game, state}), do: {game, state} | ||
|
||
defp invalid({_game, :tbd}), do: :invalid | ||
defp invalid({_game, state}), do: state | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
defmodule Mjw.GameStateTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "waiting_for_players" do | ||
game = Mjw.Game.new() | ||
assert Mjw.GameState.state(game) == :waiting_for_players | ||
end | ||
|
||
test "waiting for players when partially filled" do | ||
game = | ||
%Mjw.Game{} | ||
|> Mjw.Game.seat_player("id0", "name0") | ||
|> Mjw.Game.seat_player("id1", "name1") | ||
|
||
assert Mjw.GameState.state(game) == :waiting_for_players | ||
end | ||
|
||
test "picking_winds" do | ||
game = | ||
%Mjw.Game{} | ||
|> Mjw.Game.seat_player("id0", "name0") | ||
|> Mjw.Game.seat_player("id1", "name1") | ||
|> Mjw.Game.seat_player("id2", "name2") | ||
|> Mjw.Game.seat_player("id3", "name3") | ||
|
||
assert Mjw.GameState.state(game) == :picking_winds | ||
end | ||
|
||
test "rolling_for_first_dealer" do | ||
game = | ||
%Mjw.Game{} | ||
|> Mjw.Game.seat_player("id0", "name0") | ||
|> Mjw.Game.seat_player("id1", "name1") | ||
|> Mjw.Game.seat_player("id2", "name2") | ||
|> Mjw.Game.seat_player("id3", "name3") | ||
|> Mjw.Game.pick_random_available_wind(0) | ||
|> Mjw.Game.pick_random_available_wind(1) | ||
|> Mjw.Game.pick_random_available_wind(2) | ||
|> Mjw.Game.pick_random_available_wind(3) | ||
|
||
assert Mjw.GameState.state(game) == :rolling_for_first_dealer | ||
end | ||
|
||
test "rolling_for_deal" do | ||
game = | ||
%Mjw.Game{} | ||
|> Mjw.Game.seat_player("id0", "name0") | ||
|> Mjw.Game.seat_player("id1", "name1") | ||
|> Mjw.Game.seat_player("id2", "name2") | ||
|> Mjw.Game.seat_player("id3", "name3") | ||
|> Mjw.Game.pick_random_available_wind(0) | ||
|> Mjw.Game.pick_random_available_wind(1) | ||
|> Mjw.Game.pick_random_available_wind(2) | ||
|> Mjw.Game.pick_random_available_wind(3) | ||
|> Mjw.Game.roll_dice_and_reseat_players() | ||
|
||
assert Mjw.GameState.state(game) == :rolling_for_deal | ||
end | ||
|
||
test "discarding" do | ||
game = | ||
%Mjw.Game{} | ||
|> Mjw.Game.seat_player("id0", "name0") | ||
|> Mjw.Game.seat_player("id1", "name1") | ||
|> Mjw.Game.seat_player("id2", "name2") | ||
|> Mjw.Game.seat_player("id3", "name3") | ||
|> Mjw.Game.pick_random_available_wind(0) | ||
|> Mjw.Game.pick_random_available_wind(1) | ||
|> Mjw.Game.pick_random_available_wind(2) | ||
|> Mjw.Game.pick_random_available_wind(3) | ||
|> Mjw.Game.roll_dice_and_reseat_players() | ||
|> Mjw.Game.roll_dice_and_deal() | ||
|
||
assert Mjw.GameState.state(game) == :discarding | ||
end | ||
|
||
test "drawing" do | ||
{:ok, game} = | ||
Mjw.Game.new() | ||
|> Mjw.Game.seat_player("id0", "name0") | ||
|> Mjw.Game.seat_player("id1", "name1") | ||
|> Mjw.Game.seat_player("id2", "name2") | ||
|> Mjw.Game.seat_player("id3", "name3") | ||
|> Mjw.Game.pick_random_available_wind(0) | ||
|> Mjw.Game.pick_random_available_wind(1) | ||
|> Mjw.Game.pick_random_available_wind(2) | ||
|> Mjw.Game.pick_random_available_wind(3) | ||
|> Mjw.Game.roll_dice_and_reseat_players() | ||
|> Mjw.Game.roll_dice_and_deal() | ||
|> Mjw.Game.discard(0, "n1-1") | ||
|
||
assert Mjw.GameState.state(game) == :drawing | ||
end | ||
|
||
test "win_declared" do | ||
game = | ||
%Mjw.Game{} | ||
|> Mjw.Game.seat_player("id0", "name0") | ||
|> Mjw.Game.seat_player("id1", "name1") | ||
|> Mjw.Game.seat_player("id2", "name2") | ||
|> Mjw.Game.seat_player("id3", "name3") | ||
|> Mjw.Game.pick_random_available_wind(0) | ||
|> Mjw.Game.pick_random_available_wind(1) | ||
|> Mjw.Game.pick_random_available_wind(2) | ||
|> Mjw.Game.pick_random_available_wind(3) | ||
|> Mjw.Game.roll_dice_and_reseat_players() | ||
|> Mjw.Game.roll_dice_and_deal() | ||
|> Mjw.Game.declare_win_from_hand(0, "n1-0") | ||
|
||
assert Mjw.GameState.state(game) == :win_declared | ||
end | ||
end |
Oops, something went wrong.