From 200b1a1ddcccfa29bc92a4f6b8fb2994542a1ce1 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Mon, 30 Jul 2012 22:35:11 -0400 Subject: [PATCH] Records and a module for handling game setup --- include/riichi.hrl | 31 +++++++++++++++++++++++++++++++ src/riichi.erl | 24 ++++++++++++++++-------- src/riichi_game.erl | 12 ++++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/riichi_game.erl diff --git a/include/riichi.hrl b/include/riichi.hrl index add7414..3c1e76e 100644 --- a/include/riichi.hrl +++ b/include/riichi.hrl @@ -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()] +}). diff --git a/src/riichi.erl b/src/riichi.erl index f9a54b0..389828d 100644 --- a/src/riichi.erl +++ b/src/riichi.erl @@ -1,15 +1,17 @@ -module(riichi). --include("riichi.hrl"). +-include("../include/riichi.hrl"). -export([ - is_valid_tile/1, - dora/1, - nearest/2, - score/3, - score_hand/1, - score_hand/2, - score_hand/3 + is_valid_tile/1, + dora/1, + nearest/2, + score/3, + score_hand/1, + score_hand/2, + 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]). diff --git a/src/riichi_game.erl b/src/riichi_game.erl new file mode 100644 index 0000000..0f285fb --- /dev/null +++ b/src/riichi_game.erl @@ -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)}.