2018-05-11 12:31:53 +02:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2018-04-11 13:25:24 +02:00
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2018-05-12 11:21:59 +02:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2018-05-11 12:31:53 +02:00
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
2018-05-15 18:21:07 +02:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2018-05-11 12:31:53 +02:00
|
|
|
module Game (
|
2018-05-15 18:21:07 +02:00
|
|
|
Hanafuda.KoiKoi.Game(..)
|
|
|
|
, Key
|
|
|
|
, View
|
2018-05-11 12:31:53 +02:00
|
|
|
, T(..)
|
|
|
|
, export
|
|
|
|
, new
|
2018-05-15 18:21:07 +02:00
|
|
|
, play
|
2018-05-11 12:31:53 +02:00
|
|
|
) where
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
import Data.Text (pack)
|
2018-05-15 18:21:07 +02:00
|
|
|
import Data.Map (Map, (!), fromList, mapWithKey)
|
2018-05-13 18:08:12 +02:00
|
|
|
import Data.Aeson (FromJSON(..), ToJSON(..), ToJSON1(..), ToJSONKey(..), genericParseJSON, genericToEncoding, genericLiftToEncoding, toEncoding1, toJSON1)
|
2018-05-12 11:21:59 +02:00
|
|
|
import Data.Aeson.Types (toJSONKeyText)
|
2018-05-15 18:21:07 +02:00
|
|
|
import qualified JSON (defaultOptions, distinct, singleLCField)
|
|
|
|
import qualified Data (Key, RW(..))
|
2018-05-11 12:31:53 +02:00
|
|
|
import qualified Player (Key)
|
2018-05-12 11:21:59 +02:00
|
|
|
import qualified Hanafuda (Flower(..), Card(..), Pack, cardsOfPack, empty)
|
2018-05-15 18:21:07 +02:00
|
|
|
import qualified Hanafuda.Player (Player(..), Seat(..))
|
|
|
|
import qualified Hanafuda.KoiKoi.Game (remap)
|
|
|
|
import qualified Hanafuda.KoiKoi (Game(..), Mode(..), Move(..), On(..), Over(..), Score, Step(..), Yaku(..), new, play)
|
2018-04-11 13:25:24 +02:00
|
|
|
import GHC.Generics
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
deriving instance Generic Hanafuda.Card
|
2018-05-12 11:21:59 +02:00
|
|
|
deriving instance Generic Hanafuda.Flower
|
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Mode
|
2018-05-11 12:31:53 +02:00
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Move
|
2018-05-12 11:21:59 +02:00
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Yaku
|
|
|
|
deriving instance Generic Hanafuda.KoiKoi.Step
|
|
|
|
deriving instance Generic1 Hanafuda.Player.Player
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2018-05-15 18:21:07 +02:00
|
|
|
type On = Hanafuda.KoiKoi.On Player.Key
|
|
|
|
type Over = Hanafuda.KoiKoi.Over Player.Key
|
|
|
|
type View = Hanafuda.KoiKoi.Game Player.Key
|
|
|
|
|
|
|
|
deriving instance Generic On
|
|
|
|
deriving instance Generic Over
|
|
|
|
deriving instance Generic View
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
instance FromJSON Hanafuda.Card
|
|
|
|
instance ToJSON Hanafuda.Card
|
2018-04-11 13:25:24 +02:00
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
instance ToJSON Hanafuda.Flower
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.Pack where
|
|
|
|
toJSON = toJSON . Hanafuda.cardsOfPack
|
|
|
|
toEncoding = toEncoding . Hanafuda.cardsOfPack
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.KoiKoi.Mode
|
|
|
|
|
2018-05-13 18:08:12 +02:00
|
|
|
instance FromJSON Hanafuda.KoiKoi.Move where
|
|
|
|
parseJSON = genericParseJSON JSON.singleLCField
|
2018-05-11 12:31:53 +02:00
|
|
|
instance ToJSON Hanafuda.KoiKoi.Move where
|
2018-04-11 13:25:24 +02:00
|
|
|
toEncoding = genericToEncoding JSON.singleLCField
|
2018-05-11 12:31:53 +02:00
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
instance ToJSON1 Hanafuda.Player.Player where
|
|
|
|
liftToEncoding = genericLiftToEncoding JSON.defaultOptions
|
|
|
|
|
|
|
|
instance ToJSON (Hanafuda.Player.Player Hanafuda.KoiKoi.Score) where
|
|
|
|
toJSON = toJSON1
|
|
|
|
toEncoding = toEncoding1
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.KoiKoi.Yaku where
|
|
|
|
toEncoding = genericToEncoding JSON.defaultOptions
|
|
|
|
instance ToJSONKey Hanafuda.KoiKoi.Yaku where
|
|
|
|
toJSONKey = toJSONKeyText (pack . show)
|
|
|
|
|
|
|
|
instance ToJSON Hanafuda.KoiKoi.Step where
|
|
|
|
toEncoding = genericToEncoding JSON.defaultOptions
|
|
|
|
|
2018-05-15 18:21:07 +02:00
|
|
|
instance ToJSON On
|
|
|
|
instance ToJSON Over
|
|
|
|
|
|
|
|
instance ToJSON View where
|
|
|
|
toEncoding = genericToEncoding JSON.distinct
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
data T = T {
|
2018-05-12 11:21:59 +02:00
|
|
|
keys :: Map Hanafuda.Player.Seat Player.Key
|
2018-05-15 18:21:07 +02:00
|
|
|
, state :: Hanafuda.KoiKoi.Game Hanafuda.Player.Seat
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
2018-05-12 11:21:59 +02:00
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
type Key = Data.Key T
|
|
|
|
|
2018-05-15 18:21:07 +02:00
|
|
|
instance Data.RW (Hanafuda.KoiKoi.Game Hanafuda.Player.Seat) T where
|
|
|
|
get = state
|
|
|
|
set state game = game {state}
|
2018-05-11 12:31:53 +02:00
|
|
|
|
|
|
|
new :: Player.Key -> Player.Key -> IO T
|
|
|
|
new p1 p2 = do
|
2018-05-15 18:21:07 +02:00
|
|
|
on <- Hanafuda.KoiKoi.new Hanafuda.KoiKoi.WholeYear
|
2018-05-11 12:31:53 +02:00
|
|
|
return $ T {
|
2018-05-12 11:21:59 +02:00
|
|
|
keys = fromList [(Hanafuda.Player.Player1, p1), (Hanafuda.Player.Player2, p2)]
|
2018-05-15 18:21:07 +02:00
|
|
|
, state = Hanafuda.KoiKoi.On on
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 11:21:59 +02:00
|
|
|
export :: Player.Key -> T -> View
|
2018-05-15 18:21:07 +02:00
|
|
|
export key (T {keys, state}) =
|
|
|
|
case Hanafuda.KoiKoi.Game.remap (keys !) state of
|
|
|
|
view@(Hanafuda.KoiKoi.Error _) -> view
|
|
|
|
view@(Hanafuda.KoiKoi.Over _) -> view
|
|
|
|
(Hanafuda.KoiKoi.On on) -> Hanafuda.KoiKoi.On $ on {
|
|
|
|
Hanafuda.KoiKoi.stock = []
|
|
|
|
, Hanafuda.KoiKoi.players = mapWithKey maskOpponentsHand $ Hanafuda.KoiKoi.players on
|
|
|
|
}
|
2018-05-11 12:31:53 +02:00
|
|
|
where
|
2018-05-15 18:21:07 +02:00
|
|
|
maskOpponentsHand k player
|
|
|
|
| k == key = player
|
|
|
|
| otherwise = player {Hanafuda.Player.hand = Hanafuda.empty}
|
|
|
|
|
|
|
|
play :: Player.Key -> Hanafuda.KoiKoi.Move -> T -> IO T
|
|
|
|
play key move game@(T {keys, state = Hanafuda.KoiKoi.On on})
|
|
|
|
| keys ! Hanafuda.KoiKoi.playing on == key = do
|
|
|
|
newState <- Hanafuda.KoiKoi.play move on
|
|
|
|
return $ game {state = newState}
|
|
|
|
| otherwise = return $ game {state = Hanafuda.KoiKoi.Error "Not your turn"}
|
|
|
|
play _ _ game = return $ game {state = Hanafuda.KoiKoi.Error "This game is over"}
|