Skip to content

Instantly share code, notes, and snippets.

@n4to4
Created May 13, 2018 07:17
Show Gist options
  • Save n4to4/be0792103ad7eafbf7f5bec8062cca52 to your computer and use it in GitHub Desktop.
Save n4to4/be0792103ad7eafbf7f5bec8062cca52 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RankNTypes #-}
module Main where
import Control.Monad.State
import System.Random
type GenAction m = forall a. (Random a) => m a
type GenActionR m = forall a. (Random a) => (a, a) -> m a
data Player = Player
{ playerName :: String
, playerPos :: (Double, Double)
} deriving (Eq, Ord, Show)
genRandom :: (RandomGen g) => GenAction (State g)
genRandom = state random
genRandomR :: (RandomGen g) => GenActionR (State g)
genRandomR range = state (randomR range)
randomPlayer :: (MonadIO m) => GenActionR m -> m Player
randomPlayer genR = do
liftIO $ putStrLn "Generating random player..."
len <- genR (8, 12)
name <- replicateM len $ genR ('a', 'z')
x <- genR (-100, 100)
y <- genR (-100, 100)
liftIO $ putStrLn "Done."
return $ Player name (x, y)
main :: IO ()
main = randomPlayer randomRIO >>= print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment