Created
March 12, 2019 08:17
-
-
Save snoyberg/ce5600bbc102cb0a70611e14ecdbf2ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
-- stack --resolver lts-12.21 script | |
{-# LANGUAGE NoImplicitPrelude #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
import Control.Lens | |
import Control.Monad (replicateM) | |
import System.Random | |
import RIO | |
data PlayerState = PlayerState { | |
_psName :: String, | |
_psScore :: Int | |
} deriving (Show) | |
makeClassy ''PlayerState | |
data Game = Game { | |
_turns :: Int, | |
_players :: [PlayerState] | |
} deriving (Show) | |
makeClassy ''Game | |
randomGameInit :: IO Game | |
randomGameInit = do | |
players <- replicateM 5 $ PlayerState <$> (replicateM 4 $ randomRIO ('a', 'z')) <*> randomRIO (1,10) | |
return $ Game 0 players | |
update :: (HasStateRef s env, HasGame s) => RIO env () | |
update = do | |
players . ix 0 . psName %= (\_ -> "mordor") | |
turns %= (+1) | |
exitCondition <- fmap (>10) (turns <%= id) | |
unless exitCondition update | |
main :: IO () | |
main = do | |
init <- randomGameInit >>= newSomeRef | |
runRIO init update >> runSimpleApp (logInfo "Game Over") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment