-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.hs
195 lines (182 loc) · 5.66 KB
/
hangman.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import System.Random (randomRIO)
import Data.Char (isAlpha, toUpper)
import Data.List (intersperse)
import System.IO (hSetBuffering, stdin, BufferMode (NoBuffering) )
lang = EN
difficulty = Hard
main :: IO ()
main = do hSetBuffering stdin NoBuffering
f <- readFile $ show lang
startplaying $ lines f
startplaying :: [String] -> IO ()
startplaying words = do index <- randomRIO (0,length words - 1)
playgame (words !! index) []
putStrLn $ strings lang Another
ans <- getChar
case ans of
'n' -> return ()
_ -> startplaying words
playgame :: String -> [Char] -> IO ()
playgame word guessed
| won = printState word guessed Won ""
| lost = printState word guessed Lost word
| otherwise = do printState word guessed Pick ""
l <- fmap toUpper getChar
let guessed' | not (isAlpha l) = guessed
| l `elem` guessed = guessed
| otherwise = l : guessed
playgame word guessed'
where won :: Bool
won = all (`elem` guessed) (map toUpper word)
lost :: Bool
lost = guessedwrong word guessed >= (length $ hangman difficulty) - 1
guessedwrong :: String -> [Char] -> Int
guessedwrong word guessed = length $ filter (`notElem` map toUpper word) guessed
printState :: String -> [Char] -> Message -> String -> IO ()
printState word guessed message string =
putStrLn $ "\n\ESC[2J" ++
unlines [ strings lang Language
, (strings lang Diff) ++ (difficultystrings lang difficulty)
, (hangman difficulty) !! (guessedwrong word guessed)
, map (\x -> if (elem (toUpper x) guessed) then x else '-') word
, (strings lang Used) ++ (intersperse ' ' $ reverse guessed)
, ""
, strings lang message ++ string
]
strings :: Language -> Message -> String
strings NL m = case m of
Another -> "Wil je nog een keer spelen? [Y/n]"
Won -> "Gefeliciteerd! Je hebt het woord geraden!"
Lost -> "Je bent dood. Het woord was "
Pick -> "Kies een letter"
Used -> "Gebruikte letters: "
Language -> "Taal: Nederlands"
Diff -> "Moeilijkheidsgraad: "
strings EN m = case m of
Another -> "Play another game? [Y/n]"
Won -> "Congratulations! You got it!"
Lost -> "You're dead. The word was "
Pick -> "Pick a letter"
Used -> "Used letters: "
Language -> "Language: English"
Diff -> "Difficulty: "
difficultystrings NL d = case d of
Easy -> "Makkelijk"
Hard -> "Moeilijk"
difficultystrings EN d = case d of
Easy -> "Easy"
Hard -> "Hard"
data Message = Another | Won | Lost | Pick | Used | Language | Diff
data Language = NL | EN deriving (Show)
data Difficulty = Easy | Hard
hangman Easy =
[ unlines [ ""
, ""
, ""
, ""
, ""
, ""
, ""
, ""
]
, unlines [ ""
, ""
, ""
, ""
, ""
, ""
, ""
, " ___"
]
, unlines [ ""
, "|"
, "|"
, "|"
, "|"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|"
, "|"
, "|"
, "|"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/"
, "|"
, "|"
, "|"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/ |"
, "|"
, "|"
, "|"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/ |"
, "| (_)"
, "|"
, "|"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/ |"
, "| (_)"
, "| |"
, "| |"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/ |"
, "| (_)"
, "| /|"
, "| |"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/ |"
, "| (_)"
, "| /|\\"
, "| |"
, "|"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/ |"
, "| (_)"
, "| /|\\"
, "| |"
, "| /"
, "|"
, "|___"
]
, unlines [ "_________"
, "|/ |"
, "| (_)"
, "| /|\\"
, "| |"
, "| / \\"
, "|"
, "|___"
]
]
hangman Hard = drop 5 $ hangman Easy