Skip to content

Instantly share code, notes, and snippets.

@jessecogollo
Last active July 21, 2021 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessecogollo/2e8d38de900d5631786f8d7963e9ac70 to your computer and use it in GitHub Desktop.
Save jessecogollo/2e8d38de900d5631786f8d7963e9ac70 to your computer and use it in GitHub Desktop.
Ejercicio ahorcado en Haskell
-- import Data.Char
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 =
do
if n==0
then putStrLn "Perdiste..."
else if word == display
then putStrLn "Ganaste !!!"
else mkguess word display n
mkguess :: String -> String -> Int -> IO ()
mkguess word display n =
do
putStrLn (display ++ " " ++ take n (repeat '*'))
putStr " Ingresa tu letra: "
q <- getLine
let (correct, display') = check word display (q!!0)
let n' = if correct then n else n-1
turn word display' n'
ahorcado :: Int -> IO ()
ahorcado n =
do
let randomInt range = randomRIO range :: IO Int
let list = ["medellinjs", "javascript", "haskell", "curiosidad", "medellin", "Colombia"]
let l = length list
num <- randomInt(0, l-1)
let word = (list !! num)
turn word ['-' | x <- word] n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment