First attempt at defining the game's states

This commit is contained in:
Sasha 2018-02-04 23:01:42 +01:00
parent ea96530511
commit e7619af640

View file

@ -30,14 +30,20 @@ data Player = Player {
, scored :: Score , scored :: Score
} }
data State = State { data Turn = Player1 | Player2
data Step = PlayACard | ChooseWhichCard | Scored
data Game = Game {
players :: (Player, Player) players :: (Player, Player)
, river :: Pack , river :: Pack
, deck :: [ Card ] , deck :: [ Card ]
, player :: Turn
, month :: Flower , month :: Flower
, turn :: Bool , step :: Step
} }
data Move = Take (Turn, Card, Card) | Choose (Turn, Card) | KoiKoi Bool
fixed :: (Yaku, Points) -> Pack -> YakuFinder fixed :: (Yaku, Points) -> Pack -> YakuFinder
fixed points indicatorSet pack = if pack == indicatorSet then Just points else Nothing fixed points indicatorSet pack = if pack == indicatorSet then Just points else Nothing
@ -93,8 +99,8 @@ yakus = foldl (\map -> unionWith (++) map . index) empty [
, ([SakeCup, CampCurtain], fixed (HanamiZake, 5)) , ([SakeCup, CampCurtain], fixed (HanamiZake, 5))
] ]
capture :: State -> Card -> Pack -> (Pack, Score) capture :: Game -> Card -> Pack -> (Pack, Score)
capture (State {month}) card pack = capture (Game {month}) card pack =
let newPack = add pack card in let newPack = add pack card in
let yakuFinders = (unionWith (++) yakus . index $ tsukiFuda month) ! card in let yakuFinders = (unionWith (++) yakus . index $ tsukiFuda month) ! card in
(newPack, foldl (\score -> foldr (uncurry insert) score . ($newPack)) empty yakuFinders) (newPack, foldl (\score -> foldr (uncurry insert) score . ($newPack)) empty yakuFinders)
@ -104,16 +110,17 @@ foldApply [] init = init
foldApply (f:fs) [x] = foldApply (f:fs) [x] =
let (a,b) = f x in a : foldApply fs [b] let (a,b) = f x in a : foldApply fs [b]
deal :: IO State deal :: IO Game
deal = do deal = do
shuffled <- shuffle cards shuffled <- shuffle cards
let [hand1,hand2,river,deck] = foldApply (take 3 . repeat $ splitAt 8) [shuffled] let [hand1,hand2,river,deck] = foldApply (take 3 . repeat $ splitAt 8) [shuffled]
let p1 = Player {hand = packOfCards hand1, captured = packOfCards [], scored = empty} let p1 = Player {hand = packOfCards hand1, captured = packOfCards [], scored = empty}
let p2 = Player {hand = packOfCards hand2, captured = packOfCards [], scored = empty} let p2 = Player {hand = packOfCards hand2, captured = packOfCards [], scored = empty}
return $ State { return $ Game {
players = (p1, p2) players = (p1, p2)
, river = packOfCards river , river = packOfCards river
, deck , deck
, player = Player1
, month = Pine , month = Pine
, turn = False , step = PlayACard
} }