Rock-paper-scissors: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: hlint, hindent, type signatures, specified import) |
|||
Line 2,312: | Line 2,312: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell>import System.Random |
<lang haskell>import System.Random (randomRIO) |
||
data Choice = Rock | Paper | Scissors |
|||
⚫ | |||
data Choice |
|||
= Rock |
|||
| Paper |
|||
| Scissors |
|||
⚫ | |||
beats :: Choice -> Choice -> Bool |
|||
beats Paper Rock = True |
beats Paper Rock = True |
||
beats Scissors Paper = True |
beats Scissors Paper = True |
||
Line 2,323: | Line 2,326: | ||
beats _ _ = False |
beats _ _ = False |
||
genrps ( |
genrps :: (Int, Int, Int) -> IO Choice |
||
genrps (r, p, s) = rps <$> rand |
|||
⚫ | |||
where |
|||
⚫ | |||
rps x |
|||
⚫ | |||
| x <= s = Rock |
|||
⚫ | |||
⚫ | |||
rand = randomRIO (1, r + p + s) :: IO Int |
|||
getrps |
getrps :: IO Choice |
||
getrps = rps <$> getLine |
|||
where rps "scissors" = Scissors |
|||
where |
|||
⚫ | |||
rps "scissors" = Scissors |
|||
rps "rock" = Rock |
|||
rps "paper" = Paper |
|||
rps _ = error "invalid input" |
|||
game :: (Int, Int, Int) -> IO a |
|||
⚫ | |||
game (r, p, s) = do |
|||
⚫ | |||
⚫ | |||
⚫ | |||
h <- getrps |
|||
⚫ | |||
⚫ | |||
putStrLn (if beats h c then "player wins\n" |
|||
⚫ | |||
else if beats c h then "player loses\n" |
|||
putStrLn |
|||
else "draw\n") |
|||
(if beats h c |
|||
let rr = if h == Rock then r+1 else r |
|||
then "player wins\n" |
|||
else if beats c h |
|||
⚫ | |||
then "player loses\n" |
|||
⚫ | |||
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 :: IO a |
||
main = game (1, 1, 1)</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |