Jump to content

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)
 
data Choice = Rock | Paper | Scissors
deriving (Show, Eq)
 
data Choice
= Rock
| Paper
| Scissors
deriving (Show, Eq)
 
beats :: Choice -> Choice -> Bool
beats Paper Rock = True
beats Scissors Paper = True
Line 2,323 ⟶ 2,326:
beats _ _ = False
 
genrps :: (rInt,p Int,s) = fmapInt) rps-> randIO Choice
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= IntRock
where rps x | x <= s + r = Rock Paper
| otherwise = Scissors
rand = randomRIO (1, r + p + s) :: IO Int
 
getrps =:: fmap rps getLineIO Choice
getrps = rps <$> getLine
where rps "scissors" = Scissors
where
rps "rock" = Rock
rps "paperscissors" = PaperScissors
rps _"rock" = error "invalid input"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
game (r,p,s) = do putStrLn "rock, paper or scissors?"
c <- genrps (r,p,s)
h <- getrps
putStrLn ("Player: " ++ show h ++ " Computer: " ++ show c)
c <- genrps (r, p, s)
putStrLn (if beats h c then "player wins\n"
putStrLn ("Player: " ++ show h ++ " Computer: " ++ show c)
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
pp = if h == Paper then p+1 else"player pwins\n"
else if beats c h
ss = if h == Scissors then s+1 else s
then "player game (rr,pp,ss)loses\n"
else h <- getrps"draw\n")
let rr =
rpsif "rock"h == Rock
then r + 1
else r
pp =
if h | x <= s+r = Paper
then p + 1
else p
ss =
ss = if h == Scissors then s+1 else s
then s + 1
else s
game (rr, pp, ss)
 
main =:: gameIO (1,1,1)</lang>a
main = game (1, 1, 1)</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.