— CPSC 312 – 2019 – Games in Haskell
module Games2
(State(..), Result(..), Game, Rands, Player, mkRands) where
— To load it, try:
— ghci
— :load Games2
import System.Random
— gs=game_state act=action
data State gs act = State gs [act] — internal_state available_actions
deriving (Ord, Eq, Show)
data Result gs act =
EndOfGame String Double (State gs act) — end of game: message, value, starting state
| ContinueGame Double (State gs act) — continue with reward and new state
deriving (Eq, Show)
type Game gs act = act -> State gs act -> Result gs act
— gs=game_state act=action ps=player_state
type Player gs act ps = Game gs act -> Result gs act -> ps -> (act, ps)
——– The following can be used to implement a deck in a game.
— Put this in your game, and don’t export a way to create or access RandsC.
— for random numbers in states, we do not want print or comparisons to depend on them
— Don’t export access to the constructor. So no one else can look at them or construct them
data Rands = RandsC [Double] — random numbers. Do not print or compare them!!!
instance Show Rands where
show _ = “_”
instance Eq Rands where
x == y = True
instance Ord Rands where
— x < y = False
x <= y = True
-- Make randoms
mkRands :: RandomGen g => g -> Rands
mkRands rg = RandsC (randoms rg)