Skip to content

Instantly share code, notes, and snippets.

@SuperManEver
Created October 9, 2016 07:46
Show Gist options
  • Select an option

  • Save SuperManEver/55f23a8a63409b665ab5c002edf997f2 to your computer and use it in GitHub Desktop.

Select an option

Save SuperManEver/55f23a8a63409b665ab5c002edf997f2 to your computer and use it in GitHub Desktop.
import Data.Char
import Data.List
sumCows :: (Eq a, Num b) => [a] -> [a] -> b
sumCows [] _ = 0
sumCows _ [] = 0
sumCows (x:xs) (y:ys) = current + sumCows (xs++[x]) ys
where
current = (if (x /= y) && (elem y xs) then 1 else 0)
containsDups lst = lenOrg /= lenDer
where
xs = map toUpper lst
lenOrg = length xs
lenDer = length . nub $ xs
playTheGame :: String -> Int -> IO ()
playTheGame isogramWord attemptsNum
| attemptsNum <= 0 = putStrLn "Jeez, you should allow for at least one attempt to guess.."
| containsDups isogramWord = putStrLn "You play Bulls'n'Cows only with an isogram words!"
| otherwise = guessTurn isogramWord attemptsNum
guessTurn :: String -> Int -> IO ()
guessTurn _ attemptsNum
| attemptsNum <= 0 = putStrLn "Too bad, no more attempts left. You lost!"
guessTurn isogramWord attemptsNum =
do
putStrLn ("You have "++ (show attemptsNum) ++" attemps left. Make your guess: " ++ ['#' | _ <- isogramWord])
inputGuess <- getLine
let
isogramWord' = capitalize isogramWord
inputGuess' = capitalize inputGuess
bulls = length . filter (== True) . zipWith (==) isogramWord' $ inputGuess'
cows = sumCows isogramWord' inputGuess'
in
do
putStrLn ("You've got " ++ (show bulls) ++ " bulls and " ++ (show cows) ++ " cows.")
if bulls == (length isogramWord)
then putStrLn "Attaboy! You've just won!"
else guessTurn isogramWord (attemptsNum-1)
main = do
putStrLn "Enter your guess"
word <- getLine
playTheGame word 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment