Skip to content

Commit

Permalink
the game will actually run all the way through now. still totally bus…
Browse files Browse the repository at this point in the history
…ted though
  • Loading branch information
Timothy J Dinkins committed Feb 16, 2009
1 parent 2451f3b commit c70a05b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
49 changes: 31 additions & 18 deletions src/game_21.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@
start_link(Players) ->
gen_fsm:start_link(?MODULE, [], Players).

stop(Pid) ->
gen_fsm:send_all_state_event(Pid, stop).
stop() ->
gen_fsm:send_all_state_event(?MODULE, terminate).

init(Ps) ->
Players = dict:from_list([{P, {0, []}} || P <- Ps]),
%gen_fsm:send_event_after(1000, announce),
error_logger:error_msg("Initialized the game\n"),
{ok, betting, {Players, deck:start()}, 0}.

betting(timeout, {Players, DeckPid}) ->
error_logger:error_msg("betting: notifying players\n"),
lists:foreach(fun({P, _V}) -> player:place_bet(P, self()) end, dict:to_list(Players)),
TimeoutPid = gen_fsm:send_event_after(30000, end_betting),
TimeoutPid = gen_fsm:send_event_after(3000, end_betting),
{next_state, receiving_bets, {[], Players, DeckPid, TimeoutPid}}.

receiving_bets({bet, Pid, Amount}, {Received, Players, DeckPid, TimeoutPid}) ->
error_logger:error_msg("receiving_bets, received a bet from ~w\n", [Pid]),
R = lists:sort([Pid|Received]),
case lists:sort(dict:fetch_keys(Players)) of
R ->
Expand All @@ -46,25 +48,29 @@ receiving_bets({bet, Pid, Amount}, {Received, Players, DeckPid, TimeoutPid}) ->
{next_state, receiving_bets, {R, Players, DeckPid}}
end;
receiving_bets(end_betting, {_Received, Players, DeckPid, _TimeoutPid}) ->
error_logger:error_msg("receiving_bets, ending betting\n"),
{next_state, dealing, {Players, DeckPid}, 0}.

dealing(timeout, {Players, DeckPid}) ->
error_logger:error_msg("dealing, dealing cards and notifying players of their hands.\n"),
Players1 = dict:map(fun(_Pid, {Amt, _}) -> {Amt, deck:draw_cards(DeckPid, 2)} end, Players),
dict:each(fun(Pid, {_, Cards}) -> player:cards(Pid, Cards) end, Players1),
lists:foreach(fun({Pid, {_, Cards}}) -> player:cards(Pid, Cards) end, dict:to_list(Players1)),
Dealer = deck:draw_cards(DeckPid, 2),
{next_state, playing_hands, {dict:fetch_keys(Players1), Players1, Dealer, DeckPid}, 0}.

playing_hands(timeout, {[], Players, Dealer, DeckPid}) ->
error_logger:error_msg("playing_hands, done with all players, moving on to dealer hand\n"),
{next_state, dealer_hand, {Players, Dealer, DeckPid}, 0};
playing_hands(timeout, {[K|Ks], Players, Dealer, DeckPid}) ->
error_logger:error_msg("playing_hands, playing hand for ~w\n", [K]),
player:hit_or_stay(K),
TimeoutPid = gen_fsm:send_event_after(30000, timeout_player),
TimeoutPid = gen_fsm:send_event_after(3000, timeout_player),
{next_state, playing_single_hand, {K, Ks, Players, Dealer, TimeoutPid, DeckPid}}.

playing_single_hand(hit, {K, Ks, Players, Dealer, TimeoutPid, DeckPid}) ->
Card = deck:draw_cards(DeckPid, 1),
{ok, {Amt, Cards}} = dict:find(K, Players),
case lib_hands:score_21([Card|Cards]) of
case score_hand([Card|Cards]) of
Score when Score > 21 ->
gen_fsm:cancel_timer(TimeoutPid),
{next_state, playing_hands, {Ks, Players, Dealer, DeckPid}, 0};
Expand All @@ -79,23 +85,23 @@ playing_single_hand(timeout_player, {_, Ks, Players, Dealer, DeckPid}) ->
{next_state, playing_hands, {Ks, Players, Dealer, DeckPid}, 0}.

dealer_hand(timeout, {Players, Dealer, DeckPid}) ->
Dealer1 = play_dealer_hand(Dealer, DeckPid),
{next_state, calc_hands, {Players, Dealer1}, 0}.
Score = play_dealer_hand(Dealer, DeckPid),
{next_state, calc_hands, {Players, Score}, 0}.

calc_hands(timeout, {Players, Dealer}) ->
DScore = lib_hands:score_21(Dealer),
calc_hands(timeout, {Players, DealerScore}) ->
Notify = fun({Pid, {_, Cards}}) ->
case lib_hands:score_21(Cards) of
Score when Score > DScore ->
case score_hand(Cards) of
Score when Score > DealerScore ->
player:win(Pid);
Score when Score =:= DScore ->
Score when Score == DealerScore ->
player:tie(Pid);
_ ->
player:lose(Pid)
end
end,
lists:foreach(Notify, dict:to_list(Players)).
%% Terminate the game now!
lists:foreach(Notify, dict:to_list(Players)),
{next_state, waiting_to_die, {Players, DealerScore}}.
% Terminate the game now!

waiting_to_die(Any, State) ->
io:format("Received: ~p. I shouldn't receive anything except a terminate message.~n", [Any]),
Expand All @@ -121,11 +127,18 @@ code_change(_Vsn, StateName, State, _Extra) ->
{ok, StateName, State}.

play_dealer_hand(Cards, DeckPid) ->
case lib_hands:score_21(Cards) of
error_logger:error_msg("calculating dealer hand with: ~w\n", [Cards]),
case score_hand(Cards) of
Score when Score > 21 ->
0;
Score when Score > 17 ->
Score;
_ ->
play_dealer_hand([deck:draw_cards(DeckPid, 1)|Cards], DeckPid)
NewCards = [deck:draw_cards(DeckPid, 1)|Cards],
error_logger:error_msg("calculating dealer hand with: ~w\n", [NewCards]),
play_dealer_hand(NewCards, DeckPid)
end.

score_hand(Hand) ->
Vals = [Val || {_Suit, _Card, Val} <- Hand],
lib_hands:score_21(Vals).
6 changes: 3 additions & 3 deletions src/lib_hands.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

score_21([H|Cs]) ->
score_21([H], Cs).
score_21(S, []) ->
lists:sum(S);
score_21(S, [H|Cs]) ->
case lists:sum([H|S]) > 21 of
true ->
Expand All @@ -14,6 +16,4 @@ score_21(S, [H|Cs]) ->
end;
false ->
score_21([H|S], Cs)
end;
score_21(S, []) ->
lists:sum(S).
end.
14 changes: 11 additions & 3 deletions src/player.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
terminate/2,
code_change/3]).
-export([start_link/0, stop/1]).
-export([join_table/1, bet/2, cards/2]).
-export([join_table/1, bet/2, cards/2, place_bet/2]).


start_link() ->
Expand All @@ -25,6 +25,9 @@ join_table(Pid) ->
bet(Pid, Amount) ->
gen_server:cast(Pid, {bet, Amount}).

place_bet(Pid, GamePid) ->
gen_server:cast(Pid, {place_bet, GamePid}).

cards(Pid, Cards) ->
gen_server:cast(Pid, {cards, Cards}).

Expand All @@ -38,7 +41,8 @@ handle_cast(join, GamePid) ->
table:join(self()),
{noreply, GamePid};

handle_cast({game_pid, Pid}, _GamePid) ->
handle_cast({place_bet, Pid}, _GamePid) ->
io:format("Place your bet.~n"),
{noreply, Pid};

handle_cast({bet, Amount}, GamePid) ->
Expand All @@ -50,7 +54,11 @@ handle_cast({cards, Cards}, GamePid) ->
{noreply, GamePid};

handle_cast(stop, State) ->
{stop, normal, State}.
{stop, normal, State};

handle_cast(Any, State) ->
io:format("Unknown message: ~w~n", [Any]),
{noreply, State}.

code_change(_Vsn, State, _Extra) ->
{ok, State}.
Expand Down

0 comments on commit c70a05b

Please sign in to comment.