module Euromilhoes where data Aposta = Ap [Int] (Int,Int) valida :: Aposta -> Bool valida (Ap num (a,b)) = (length num)==5 && (numValidos num) && a/=b && 1<=a && 1<=b && a<=9 && b<=9 numValidos :: [Int] -> Bool numValidos [] = True numValidos (h:t) = 1<=h && h<=50 && not (elem h t) && (numValidos t) comuns :: Aposta -> Aposta -> (Int,Int) comuns (Ap ns (a,b)) (Ap xs (c,d)) = (numComs ns xs, numComs [a,b] [c,d]) numComs :: [Int] -> [Int] -> Int numComs [] _ = 0 numComs (x:xs) ns = if (elem x ns) then 1+(numComs xs ns) else (numComs xs ns) instance Eq Aposta where ap1 == ap2 = (comuns ap1 ap2)==(5,2) premio :: Aposta -> Aposta -> Maybe Int premio ap ch = case (comuns ap ch) of (5,2) -> Just 1 (5,1) -> Just 2 (5,0) -> Just 3 (4,2) -> Just 4 (4,1) -> Just 5 (4,0) -> Just 6 (3,2) -> Just 7 (3,1) -> Just 8 (2,2) -> Just 9 (3,0) -> Just 10 (1,2) -> Just 11 (2,1) -> Just 12 _ -> Nothing leAposta :: IO Aposta leAposta = do putStrLn "Lista de 5 Numeros: " nums <- getLine putStrLn "Par de Estrelas: " est <- getLine let ap = (Ap (read nums) (read est)) if (valida ap) then return ap else do putStrLn "Aposta invalida !!" leAposta joga :: Aposta -> IO () joga ch = do ap <- leAposta case (premio ap ch) of Just n -> putStrLn ("Tem o "++(show n)++"o premio.") _ -> putStrLn "Nao tem premio ..."