Rock-paper-scissors: Difference between revisions
Content added Content deleted
(Better D code) |
|||
Line 636: | Line 636: | ||
1:2 Play: |
1:2 Play: |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
<lang haskell>import System.Random |
|||
data Choice = Rock | Paper | Scissors |
|||
deriving (Show, Eq) |
|||
res x y | x == y = "draw" |
|||
res Paper Rock = "player wins" |
|||
res Scissors Paper = "player wins" |
|||
res Rock Scissors = "player wins" |
|||
res _ _ = "player loses" |
|||
genrps (r,p,s) = fmap rps rand |
|||
where rps x | x <= s = Rock |
|||
| x <= s+r = Paper |
|||
| otherwise = Scissors |
|||
rand = randomRIO (1,r+p+s) :: IO Int |
|||
getrps = fmap rps getLine |
|||
where rps "scissors" = Scissors |
|||
rps "rock" = Rock |
|||
rps "paper" = Paper |
|||
rps _ = error "invalid input" |
|||
game (r,p,s) = do putStrLn "rock, paper or scissors?" |
|||
h <- getrps |
|||
c <- genrps (r,p,s) |
|||
putStrLn ("Player: " ++ show h ++ " Computer: " ++ show c) |
|||
putStrLn (res h c ++ "\n") |
|||
let rr = if h == Rock then r + 1 else r |
|||
pp = if h == Paper then p + 1 else p |
|||
ss = if h == Scissors then s + 1 else s |
|||
game (rr,pp,ss) |
|||
main = game (1,1,1)</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |