Skip to content

Instantly share code, notes, and snippets.

@bruno-cadorette
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save bruno-cadorette/19295aafc0f96e55ae83 to your computer and use it in GitHub Desktop.

Select an option

Save bruno-cadorette/19295aafc0f96e55ae83 to your computer and use it in GitHub Desktop.
Rock paper scissor
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