-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameIO.hs
More file actions
104 lines (86 loc) · 2.84 KB
/
GameIO.hs
File metadata and controls
104 lines (86 loc) · 2.84 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module GameIO (strToPegs, printCols, printWrLength, printRating, printWin, getGuess) where
import GameRules
import Data.List (lookup, repeat)
import Data.Maybe (fromJust)
import Control.Monad (forM, replicateM_)
import Text.Printf (printf)
import qualified System.Console.ANSI as Con
chars = [('1', Red), ('2', Green), ('3', Blue), ('4', Yellow), ('5', Purple), ('6', Orange),
('R', Red), ('G', Green), ('B', Blue), ('Y', Yellow), ('P', Purple), ('O', Orange),
('r', Red), ('g', Green), ('b', Blue), ('y', Yellow), ('p', Purple), ('o', Orange)]
-- convert user-input to data we can work with --
charToPeg :: Char -> Maybe PegColor
charToPeg c = lookup c chars
strToPegs :: String -> PegCode
strToPegs = foldr con []
where
con c xs = case charToPeg c of
Just x -> x:xs
Nothing -> xs
colors = [
(Red, "[ Red ]"),
(Green, "[ Green ]"),
(Blue, "[ Blue ]"),
(Yellow, "[ Yellow ]"),
(Purple, "[ Purple ]"),
(Orange, "[ Orange ]")]
showCol :: PegColor -> String
showCol c = case lookup c colors of
Just s -> s
Nothing -> error "well..."
-- pretty output --
conColors = [
(Red, (Con.Red, Con.Dull)),
(Green, (Con.Green, Con.Dull)),
(Blue, (Con.Blue, Con.Dull)),
(Yellow, (Con.Yellow, Con.Dull)),
(Purple, (Con.Magenta, Con.Dull)),
(Orange, (Con.Red, Con.Vivid))]
-- | sets Foreground to Intensity and Color
setConCol int col = Con.setSGR $ (:[]) $ Con.SetColor Con.Foreground int col
resetConCol = Con.setSGR $ (:[]) $ Con.Reset
printCols :: PegCode -> IO ()
printCols code = do
forM code (\c -> do
let (col, int) = fromJust $ lookup c conColors
setConCol int col
putStr $ showCol c )
resetConCol
printWrLength :: IO ()
printWrLength = do
setConCol Con.Dull Con.Red
putStrLn "The code you entered has the wrong length!"
resetConCol
printRating :: Int -> PegCode -> CompRes -> IO()
printRating n guess (n1,n2) = do
putStr ">> "
printCols guess
putStr " ["
setConCol Con.Vivid Con.Green
putStr $ take n1 $ repeat '+'
resetConCol
putStr $ take n2 $ repeat 'o'
putStr $ take (n - n1 - n2) $ repeat ' '
putStrLn "]"
printWin :: PegCode -> IO ()
printWin code = do
putStrLn ""
setConCol Con.Vivid Con.Green
putStr "++ "
printCols code
setConCol Con.Vivid Con.Green
putStrLn " ++++"
resetConCol
prettyCount :: Int -> String
prettyCount n
| n < length ns = ns !! n
| otherwise = (show n) ++ "th"
where
ns = ["last", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth"]
getGuess :: Int -> IO String
getGuess n = do
printf "\nThis is your %s try. Your guess please:\n" $ prettyCount n
str <- getLine
Con.cursorUpLine 2
Con.clearFromCursorToScreenEnd
return str