Skip to content

Instantly share code, notes, and snippets.

@wchargin
Created March 5, 2016 16:14
Show Gist options
  • Select an option

  • Save wchargin/3c80faff37ed287cee40 to your computer and use it in GitHub Desktop.

Select an option

Save wchargin/3c80faff37ed287cee40 to your computer and use it in GitHub Desktop.
an example algebraic data type for a history event
data HistoryEvent = Play
{ tilesPlaced :: [Positioned LetterTile]
, wordsFormed :: [[Letter]]
, rawPoints :: Int
, pointsAfterSpecialTile :: Int
}
| Challenge { invalidWords :: [[Letter]] }
| Exchange { old :: [LetterTile], new :: [LetterTile] }
| Pass
-- In our GUI implementation,
-- this would be "update a sidebar" or something.
process :: Player -> HistoryEvent -> IO ()
process player (Play tp wf rp past) = do
putStrLn $ "Player " ++ show player ++ " played a word!"
putStrLn $ "The tiles were " ++ show tp ++ ","
putStrLn $ "and the words formed were: " ++ show wf ++ "."
putStrLn $ "Before special tiles were applied, "
putStrLn $ "this play was worth " ++ show rp ++ " point(s);"
putStrLn $ "after special tiles,"
putStrLn $ "it was worth " ++ show past ++ " point(s)."
process player (Challenge invs) = do
putStrLn $ "Player " ++ show player ++ " challenged!"
case invs of
[] -> putStrLn $ "It was unsuccessful, though."
_ -> putStrLn $ "It worked! These words were invalid: " ++ show invs
process player (Exchange old _) = do
putStrLn $ "Player " ++ show player ++ " exchanged tiles!"
putStrLn $ "The tiles themselves are secret,"
putStrLn $
"but the player exchanged exactly " ++ show (length old) ++ " of them."
process player Pass = putStrLn $ "Player " ++ show player ++ " passed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment