Skip to content

Instantly share code, notes, and snippets.

@dcastro
Created September 15, 2017 14:13
Show Gist options
  • Save dcastro/4e80bdf9d620ef69334ac2a54287fc2b to your computer and use it in GitHub Desktop.
Save dcastro/4e80bdf9d620ef69334ac2a54287fc2b to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
type Position = (Int, Int)
newtype PlayerData = PlayerData { _pos :: Position }
data GameStateData = GameStateData PlayerData [Position]
-- player typeclass and instance
class Player p where
playerPosition :: p -> Position
playerMoveTo :: Position -> p -> p
instance Player PlayerData where
playerPosition = _pos
playerMoveTo pos player = player { _pos = pos }
-- gamestate typeclass and implementation
class Player (PlayerType g) => GameState g where
type PlayerType g :: *
getPlayer :: g -> PlayerType g
getMonsterPositions :: g -> [Position]
instance GameState GameStateData where
type PlayerType GameStateData = PlayerData
getPlayer (GameStateData p _) = p
getMonsterPositions (GameStateData _ mPoses) = mPoses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment