Skip to content

Instantly share code, notes, and snippets.

@elessime
Created August 1, 2021 19:11
Show Gist options
  • Save elessime/ef344b9ac58c052d0cfbade9eb18cd83 to your computer and use it in GitHub Desktop.
Save elessime/ef344b9ac58c052d0cfbade9eb18cd83 to your computer and use it in GitHub Desktop.
Starman extended
import System.Random
check :: String -> String -> Char -> (Bool, String)
check word display c = (c `elem` word, [if x == c then c else y | (x, y) <- zip word display])
turn :: String -> String -> Int -> IO ()
turn word display n =
if n == 0
then putStrLn "You lose"
else
if word == display
then putStrLn "You win!"
else mkguess word display n
mkguess :: String -> String -> Int -> IO ()
mkguess word display n =
do
putStrLn (display ++ " " ++ replicate n '*')
putStr " Enter your guess: "
q <- getLine
if length q == 1
then do
let (correct, display') = check word display (head q)
let n' = if correct then n else n -1
turn word display' n'
else do
putStrLn " \n Please enter one letter at a time\n"
turn word display n
randomword :: IO String
randomword =
do
file <- readFile "/usr/share/dict/words"
let linesOfFile = lines file
index <- randomRIO (0, length linesOfFile - 1)
return (linesOfFile !! index)
starman :: IO ()
starman =
do
word <- randomword
let len = length word
let n
| len <= 5 = 5
| len `elem` [6,7] = 6
| len `elem` [8,9] = 7
| len `elem` [10..12] = 8
| otherwise = 9
turn word ['-' | x <- word] n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment