Last active
August 29, 2015 14:23
-
-
Save bruno-cadorette/19295aafc0f96e55ae83 to your computer and use it in GitHub Desktop.
Rock paper scissor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Text.Read (readMaybe) | |
| import System.Random (randomRIO) | |
| data Move = Rock | Paper | Scissor deriving(Eq, Read, Show, Enum) | |
| data Result = Victory | Defeat | Draw deriving(Show) | |
| play Rock Scissor = Victory | |
| play Paper Rock = Victory | |
| play Scissor Paper = Victory | |
| play a b | |
| |a==b = Draw | |
| |otherwise = Defeat | |
| ai = do | |
| i <- randomRIO (0, 2) | |
| return $ [Rock ..] !! i | |
| getInput = do | |
| input <- fmap readMaybe getLine | |
| case input of | |
| Just x -> return x | |
| Nothing -> do | |
| putStrLn "Please chose a valid value" | |
| getInput | |
| main = do | |
| putStrLn "Rock Paper Scissor?" | |
| choice <- getInput | |
| aichoice <- ai | |
| putStrLn $ "The computer selected " ++ show aichoice | |
| print $ play choice aichoice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment