-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDice.hs
36 lines (28 loc) · 873 Bytes
/
Dice.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Dice where
import Control.Monad (liftM)
import Control.Monad.Random
import Data.List (intercalate)
data DiceBotCmd =
Start
| Quit
| Roll { cmdDice :: [Die] }
| None
| Bad { cmdBad :: String }
deriving (Show, Eq)
data Die =
Const { dieConst :: Int }
| Die { dieNum :: Int, dieType :: Int }
deriving (Show, Eq)
showDice :: [Die] -> String
showDice = intercalate " + " . map showDie
where showDie (Const n) = show n
showDie (Die n t) = show n ++ "d" ++ show t
showResult :: [Int] -> String
showResult = intercalate " + " . map show
randomFace :: RandomGen g => Int -> Rand g Int
randomFace t = getRandomR (1, t)
rollDie :: RandomGen g => Die -> Rand g [Int]
rollDie (Const n) = return [n]
rollDie (Die n t) = sequence . replicate n $ randomFace t
rollDice :: [Die] -> IO [Int]
rollDice = evalRandIO . liftM concat . mapM rollDie