-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
47 lines (40 loc) · 1.5 KB
/
main.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
module Main where
import Control.Monad(when)
import Data.List
import Data.Sequence
import Data.String
import System.Environment
import System.IO
main :: IO ()
main = do
(filename:args) <- getArgs
fullText <- (readFile filename)
let sequencedWords = fromList $ words fullText
(blankedText, removedWords) = foldlWithIndex blankWords ([], []) sequencedWords
displayText = concat $ intersperse " " blankedText
putStrLn displayText
putStrLn $ "\n\nRemoved words: " ++ (concat $ intersperse " " (Data.List.sort removedWords))
askForGuess removedWords
blankWords :: ([String], [String]) -> Int -> String -> ([String], [String])
blankWords (remainingWords, removedWords) i word =
if (rem i 5) == 0
then (remainingWords ++ ["__" ++ show (div i 5) ++ "__"], removedWords ++ [word])
else (remainingWords ++ [word], removedWords)
data RemovedWord = RemovedWord {
word :: String,
index :: Int
}
askForGuess :: [String] -> IO ()
askForGuess removedWords = do
putStrLn "\nEnter a guess:"
theGuess <- getLine
when (not $ Data.List.null theGuess) $ do
let (indexString:tail) = (words theGuess)
index = read indexString
guess = head tail
isValidGuess = checkGuess removedWords (index, guess)
putStrLn $ "\n\nYou said: " ++ guess ++ "\nValid? " ++ show isValidGuess
askForGuess removedWords
checkGuess :: [String] -> (Int, String) -> Bool
checkGuess removedWords (index, guess) =
elem guess removedWords