— CPSC 312 – 2021 – Games in Haskell
module CountGameNew where
data State = State InternalState [Action] — internal_state available_actions
deriving (Ord, Eq, Show)
data Result = EndOfGame Double State — end of game: value, starting state
| ContinueGame State — continue with new state
deriving (Eq, Show)
type Game = Action -> State -> Result
type Player = State -> Action
—– CountGame
type Action = Int — a move for a player is just an integer
type InternalState = Int — the state of the game is just an integer
countGame :: Int -> [Action] -> Game
countGame breakValue possibleActions move (State st _)
| newsum >= breakValue = EndOfGame (-1) (State 0 possibleActions)
| otherwise = ContinueGame (State newsum possibleActions)
where newsum = move+st
— Some games:
— countGame 20 [1,2,3,5,7]
— countGame 100 [1..9]
— countGame 101 [2,4,10,14]
createCountGame :: Int -> [Action] -> (Game ,State)
— Given breakValue possibleActions returns game and initial state
createCountGame breakValue possibleActions = (countGame breakValue possibleActions, State 0 possibleActions)
— A simple Player
simple_count_player :: Int -> [Action] -> Player
simple_count_player breakValue possibleActions (State st _ )
| elem (breakValue-1-st) possibleActions = (breakValue-1-st)
| otherwise = foldr max 0 possibleActions