Skip to content

Commit b1e96de

Browse files
committed
Complete Step 1 and Step 2 of the Hangman game
- Importing modules - Generating a word list
1 parent 0735cbf commit b1e96de

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

ch13/hangman/src/Main.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
module Main where
22

3+
import Control.Monad (forever)
4+
import Data.Char (toLower)
5+
import Data.Maybe (isJust)
6+
import Data.List (intersperse)
7+
import System.Exit (exitSuccess)
8+
import System.Random (randomRIO)
9+
310
main :: IO ()
411
main = do
512
putStrLn "hello world"
13+
14+
type WordList = [String]
15+
16+
allWords :: IO WordList
17+
allWords = do
18+
dict <- readFile "data/dict.txt"
19+
return (lines dict)
20+
21+
minWordLength :: Int
22+
minWordLength = 5
23+
24+
maxWordLength :: Int
25+
maxWordLength = 9
26+
27+
gameWords :: IO WordList
28+
gameWords = do
29+
aw <- allWords
30+
return $ filter gameLength aw
31+
where gameLength w =
32+
let l = length (w :: String)
33+
in l > minWordLength && l < maxWordLength
34+
35+
randomWord :: WordList -> IO String
36+
randomWord wl = do
37+
randomIndex <- randomRIO (0, length wl - 1)
38+
return $ wl !! randomIndex
39+
40+
randomWord' :: IO String
41+
randomWord' = gameWords >>= randomWord

0 commit comments

Comments
 (0)