Records and a module for handling game setup

This commit is contained in:
Correl Roush 2012-07-30 22:35:11 -04:00
parent 4d85ba0d10
commit 200b1a1ddc
3 changed files with 59 additions and 8 deletions

View file

@ -1,3 +1,10 @@
-define(SIMPLES, [#tile{suit=S, value=V} || S <- [pin, man, sou], V <- lists:seq(2,8)]).
-define(TERMINALS, [#tile{suit=S, value=V} || S <- [pin, man, sou], V <- [1,9]]).
-define(DRAGONS, [#tile{suit=dragon, value=V} || V <- [green, red, white]]).
-define(WINDS, [#tile{suit=wind, value=V} || V <- [east, south, west, north]]).
-define(HONOURS, ?DRAGONS ++ ?WINDS).
-define(TILES, ?SIMPLES ++ ?TERMINALS ++ ?HONOURS).
-type wind() :: east | south | west | north.
-type dragon() :: green | red | white.
@ -19,3 +26,27 @@
melds=[] :: [meld()]
}).
-type hand() :: #hand{}.
-record(player, {
name :: string(),
seat :: wind(),
hand :: hand(),
discards :: [tile()],
drawn :: none | {tsumo | ron, tile()}
}).
-type player() :: #player{}.
-type phase() :: draw | discard.
-record(game, {
rounds=2 :: integer(),
timeout=infinity :: integer() | infinity,
round=east :: wind(),
turn=east :: wind(),
phase=draw :: phase(),
wall :: [tile()],
rinshan :: [tile()],
dora :: [tile()],
uradora :: [tile()],
players :: [player()]
}).

View file

@ -1,6 +1,6 @@
-module(riichi).
-include("riichi.hrl").
-include("../include/riichi.hrl").
-export([
is_valid_tile/1,
@ -9,7 +9,9 @@
score/3,
score_hand/1,
score_hand/2,
score_hand/3
score_hand/3,
shuffle/1,
tiles/0
]).
is_valid_tile(#tile{suit=dragon, value=Value}) ->
@ -130,3 +132,9 @@ find_sets(Tiles) ->
Unique = sets:to_list(sets:from_list(Tiles)),
[{length(lists:filter(fun(X) -> X == T end, Tiles)), T}
|| T <- Unique].
shuffle(List) ->
[X || {_, X} <- lists:sort([{random:uniform(), I} || I <- List])].
tiles() ->
lists:flatten([lists:duplicate(4, T) || T <- ?TILES]).

12
src/riichi_game.erl Normal file
View file

@ -0,0 +1,12 @@
-module(riichi_game).
-include("../include/riichi.hrl").
-export([new/0]).
new() ->
Tiles = riichi:shuffle(riichi:tiles()),
#game{rinshan=lists:sublist(Tiles, 1, 4),
dora=lists:sublist(Tiles, 5, 5),
uradora=lists:sublist(Tiles, 10,5),
wall=lists:sublist(Tiles, 15, 124)}.