-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConsole.hs
More file actions
62 lines (57 loc) · 2.05 KB
/
Copy pathConsole.hs
File metadata and controls
62 lines (57 loc) · 2.05 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Startups.Interpreter
import Startups.Utils
import Startups.Game
import Startups.GameTypes
import Backends.Common
import Control.Lens
import Control.Applicative
import System.Random
import qualified Text.PrettyPrint.ANSI.Leijen as PP
import Data.Char (isDigit)
readNumber :: IO Int
readNumber = do
n <- getLine
return $ if all isDigit n
then read n
else -1
consoleDict :: OperationDict IO
consoleDict = OperationDict pd ac ar tp gm
where
pd age turn pid necards stt = do
let cards = _NonEmpty # necards
pm = stt ^. playermap
x = allowableActions age pid cards pm
r <- case pid of
"you" -> do
print (PP.pretty (quicksituation age turn pm))
print (PP.pretty (playerActionsDialog pid pm cards x))
readNumber
_ -> randomRIO (0, length x - 1)
if r >= 0 && r < length x
then let (pa,e,_) = x !! r
in return (pa, e)
else pd age turn pid necards stt
ac turn pid necards stt msg = do
let cards = _NonEmpty # necards
pm = stt ^. playermap
case pid of
"you" -> do
print (PP.pretty (cardChoiceDialog pid pm cards))
n <- readNumber
if n >= 0 && n < length cards
then return (cards !! n)
else ac turn pid necards stt msg
_ -> (cards !!) <$> randomRIO (0, length cards - 1)
tp "you" msg = print (PP.pretty msg)
tp _ _ = return ()
gm msg = print (PP.pretty msg)
ar stt actions = print (PP.pretty (displayActions (stt ^. playermap) actions))
main :: IO ()
main = do
g <- newStdGen
(_, o) <- runInterpreter consoleDict (initialGameState g ["you","pim","pam"]) playGame
case o of
Left rr -> error (show (PP.pretty rr))
Right x -> print x