Skip to content

Instantly share code, notes, and snippets.

@tuttlem
Created September 4, 2012 11:19
Show Gist options
  • Save tuttlem/3620344 to your computer and use it in GitHub Desktop.
Save tuttlem/3620344 to your computer and use it in GitHub Desktop.
Scissors, Paper, Rock!
module Main where
import System.IO
import System.Random
data Move = Scissors | Paper | Rock | Unknown deriving (Eq,Show)
data Outcome = Winner | Draw | Loser | ND deriving (Show)
str2Move :: String -> Move
str2Move s = do
case s of
"s" -> Scissors
"p" -> Paper
"r" -> Rock
_ -> Unknown
getWinner :: Move -> Move
getWinner m = do
case m of
Scissors -> Rock
Rock -> Paper
Paper -> Scissors
Unknown -> Unknown
getOutcome :: Move -> Move -> Outcome
getOutcome player cpu
| player == Unknown || cpu == Unknown = ND
| player == cpu = Draw
| cpu == winner = Loser
| otherwise = Winner
where winner = getWinner player
getCpuMove :: StdGen -> Move
getCpuMove gen = do
let (randNumber, newGen) = randomR(1, 3) gen :: (Int, StdGen)
case randNumber of
1 -> Rock
2 -> Scissors
3 -> Paper
main = do
gen <- getStdGen
putStr "Enter your choice (s, p or r): "
hFlush stdout
line <- getLine
let player = str2Move line
let cpu = getCpuMove gen
let outcome = getOutcome player cpu
putStrLn $ "Player Chose: " ++ (show player)
putStrLn $ "Cpu Chose : " ++ (show cpu)
putStrLn $ "Outcome : " ++ (show outcome)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment