Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

[WIP] Acceptance testing #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions lib/pokerwars/game.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ defmodule Pokerwars.Game do

@small_blind 10
@big_blind @small_blind * 2

defstruct players: [], status: :waiting_for_players, current_deck: nil, original_deck: nil

@max_players 2

defstruct players: [], status: :waiting_for_players, current_deck: nil, original_deck: nil, \
hole_cards: [], current_player: 0

def create(deck \\ Deck.in_order) do
%__MODULE__{ original_deck: deck }
end
Expand All @@ -28,6 +28,22 @@ defmodule Pokerwars.Game do
defp phase(:ready_to_start, game, action) do
ready_to_start(action, game)
end
defp phase(:pre_flop, game, action) do
if(game.current_player == length(game.players)) do
new_game = %{ game | status: :pre_flop, current_player: game.current_player + 1 }
else
flop = Enum.take(game.current_deck.cards, 3)
new_cards = Enum.slice(game.current_deck.cards, 3, 52)

new_game = %{ game | status: :flop, hole_cards: flop, \
current_deck: %Deck{game.current_deck | cards: new_cards } }
end

{:ok, new_game}
end
defp phase(:flop, game, action) do
{:ok, game}
end

defp next_status(%__MODULE__{status: :waiting_for_players, players: players} = game)
when length(players) == @max_players do
Expand Down Expand Up @@ -103,3 +119,15 @@ defmodule Pokerwars.Game do
{updated_deck, [updated_player | updated_others]}
end
end

defimpl String.Chars, for: Pokerwars.Game do
def to_string(game) do
Enum.join [
"%Pokerwars.Game{\n",
" status: #{game.status}\n",
" current_deck: (#{length(game.current_deck.cards)} cards)\n",
" players: (#{length(game.players)})\n",
"}"
]
end
end
42 changes: 42 additions & 0 deletions test/acceptance/running_the_simplest_game_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
defmodule Pokerwars.RunningASimpleGameTest do
use ExUnit.Case, async: true
import Pokerwars.TestHelpers

alias Pokerwars.{Game, Player, Deck, Card}

@player1 Player.create "Bill", 100
@player2 Player.create "Ben", 100

test "Running a simple game" do
step "We create a game and it is waiting for players"
game = Game.create
assert game.status == :waiting_for_players

step "The players join and the game is ready to start"
game = with \
{:ok, game} <- Game.apply_action(game, {:join, @player1}),
{:ok, game} <- Game.apply_action(game, {:join, @player2}),
do: game
assert game.status == :ready_to_start

step "The game is started"
{:ok, game} = Game.apply_action(game, {:start_game})
assert game.status == :pre_flop

step "The players pay small and big blinds automatically"
assert [90, 80] == Enum.map(game.players, &(&1.stack))

step "Both players can see 2 cards"
assert 2 == length(Enum.map(game.players, &(&1.hand)))

step "Both players check"
game = with \
{:ok, game} <- Game.apply_action(game, {:check, @player1}),
{:ok, game} <- Game.apply_action(game, {:check, @player2}),
do: game
assert game.status == :flop

step "There are 3 cards on the table"
assert length(game.hole_cards) == 3
end
end
5 changes: 5 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ defmodule Pokerwars.TestHelpers do
def david do
Player.create("David")
end

def step(message) do
IO.puts message
end
end

ExUnit.start()