Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.IO
  2. import System.Random
  3.  
  4. -- scegli una riga a caso dal file
  5. randomline :: StdGen -> [String] -> String
  6. randomline g l = l' !! i
  7. where
  8.   l' = [x | x <- l, not(null(x))]
  9.    i = fst $ randomR (0, length l' - 1) g
  10.  
  11. -- controlla se la lettera e` stata indovinata
  12. ismatch :: (Char, Char) -> Char -> Bool
  13. ismatch x c = snd x == c && fst x == '_'
  14.  
  15. -- calcola nuovo stato
  16. newstatus :: [(Char, Char)] -> Char -> [(Char, Char)]
  17. newstatus s c = [if ismatch x c then (snd(x),snd(x)) else x | x <- s]
  18.  
  19. -- chiedi input e prova finche` non e` tutto indovinato
  20. trials :: [(Char, Char)] -> IO ()
  21. trials status = do
  22. nc <- getChar
  23. --let newstatus = [if ismatch c n then (snd(c),snd(c)) else c | c <- status]
  24. let ns = newstatus status nc
  25. putStrLn [fst x | x <- ns]
  26. if all (==True) [fst(x) == snd(x) | x <- ns] then
  27.   return ()
  28. else
  29.   trials ns
  30.  
  31. --------------------------------------------------------
  32. main = do
  33. fc <- readFile "pallotron"
  34. gen <- getStdGen
  35. let target = randomline gen (lines fc)
  36. let start = take (length target) (repeat '_')
  37. putStrLn "Indovina, stronzo!"
  38. trials $ zip start target
  39. putStrLn ("Esatto, era proprio " ++ target)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement