Last active
December 11, 2018 10:51
-
-
Save joom/7d220e603d58ff889902 to your computer and use it in GitHub Desktop.
Rock Paper Scissors in Haskell
This file contains 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 System.Random | |
data Move = Rock | Paper | Scissors deriving (Show, Eq, Enum) | |
instance Ord Move where | |
(<=) x y = x == y || (x,y) `elem` [(Rock,Paper),(Paper,Scissors),(Scissors,Rock)] | |
humanSelect :: IO Move | |
humanSelect = fmap (toEnum . read) getLine | |
computerSelect :: IO Move | |
computerSelect = fmap toEnum (randomRIO (0,2)) | |
resultString :: Ordering -> String | |
resultString y = case y of | |
LT -> "Player Wins" | |
EQ -> "Draw!" | |
GT -> "Computer Wins" | |
main :: IO () | |
main = do | |
putStrLn "Choose a move (0 for Rock, 1 for Paper, 2 for Scissors) :" | |
humanMove <- humanSelect | |
computerMove <- computerSelect | |
putStrLn $ show humanMove ++ " (you) vs " ++ show computerMove ++ " (computer)" | |
putStrLn $ resultString $ humanMove `compare` computerMove | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment