Rock-paper-scissors: Difference between revisions
m
→{{header|Haskell}}: hlint, hindent, type signatures, specified import
m (→{{header|Haskell}}: hlint, hindent, type signatures, specified import) |
|||
Line 2,312:
=={{header|Haskell}}==
<lang haskell>import System.Random (randomRIO)
deriving (Show, Eq) ▼
data Choice
= Rock
| Paper
| Scissors
beats :: Choice -> Choice -> Bool
beats Paper Rock = True
beats Scissors Paper = True
Line 2,323 ⟶ 2,326:
beats _ _ = False
genrps :: (
genrps (r, p, s) = rps <$> rand
where rps x | x <= s = Rock ▼
where
| x <= s+r = Paper▼
rps x
| otherwise = Scissors ▼
| x
rand = randomRIO (1, r + p + s) :: IO Int
getrps
getrps = rps <$> getLine
where
rps "rock" = Rock▼
rps "paper" = Paper
rps _ = error "invalid input"
game :: (Int, Int, Int) -> IO a
game (r,p,s) = do putStrLn "rock, paper or scissors?"▼
game (r, p, s) = do
h <- getrps▼
c <- genrps (r,p,s)▼
h <- getrps
putStrLn ("Player: " ++ show h ++ " Computer: " ++ show c)▼
putStrLn
(if beats h c
else if beats c h
ss = if h == Scissors then s+1 else s▼
then "player
let rr =
then r + 1
else r
pp =
then p + 1
else p
ss =
then s + 1
else s
game (rr, pp, ss)
main
main = game (1, 1, 1)</lang>
=={{header|Icon}} and {{header|Unicon}}==
|