-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.hs
60 lines (48 loc) · 1.64 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
import Data.List
type Sentence = String
type Hp = Int
type UsedChars = [Char]
data State = State Sentence Hp UsedChars
data PlayState = Won | Lost | Play
main = do
putStrLn "Choose starting hp:"
line <- getLine
game $ State sentence ((read line)::Hp) []
where sentence = "lala haha" :: Sentence
game state@(State sentence hp used) = do
case playState of
Won -> putStrLn "gg!"
Lost -> putStrLn ("lost, the solution was " ++ sentence)
Play -> do
putStrLn $ getStateRepr state
line <- getLine
game $ getNewState state line
where playState = getPlayState state
getPlayState (State sentence hp used)
| hp == 0 = Lost
| (getUniqueIgnoreWs sentence) \\ used == [] = Won
| otherwise = Play
getStateRepr (State sentence hp used) = stateRepr
where
stateRepr = revealed ++ "\nused: " ++ usedRepr ++ ", hp: " ++ (show hp)
revealed = getRevealed sentence used
usedRepr = "[" ++ (intercalate ", " $ map (\x -> [x]) used) ++ "]"
getNewState state@(State sentence hp used) line = case line of
[] -> state
[x] -> if x `elem` used then state else State sentence (hp-1) (x:used)
(x:xs) -> if (x:xs) == sentence
then State sentence hp $ getUniqueIgnoreWs sentence
else State sentence 0 (x:used)
getRevealed guessingStr usedChars = foldr reveal [] guessingStr
where
reveal c acc = (getValC c):acc
getValC ' ' = ' '
getValC c = if c `elem` usedChars then c else '_'
getUniqueIgnoreWs = getUnique . filter (\x -> x /= ' ')
getUnique xs =
getUnique' xs []
where
getUnique' [] res = res
getUnique' (x:xs) res
| x `elem` xs = getUnique' xs res
| otherwise = getUnique' xs (x:res)