September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,36 +1,59 @@
|
|||
import System.Random
|
||||
|
||||
data Choice = Rock | Paper | Scissors
|
||||
deriving (Show, Eq)
|
||||
import System.Random (randomRIO)
|
||||
|
||||
data Choice
|
||||
= Rock
|
||||
| Paper
|
||||
| Scissors
|
||||
deriving (Show, Eq)
|
||||
|
||||
beats :: Choice -> Choice -> Bool
|
||||
beats Paper Rock = True
|
||||
beats Scissors Paper = True
|
||||
beats Rock Scissors = True
|
||||
beats _ _ = False
|
||||
|
||||
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
|
||||
genrps :: (Int, Int, Int) -> IO Choice
|
||||
genrps (r, p, s) = 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"
|
||||
getrps :: IO Choice
|
||||
getrps = 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 (if beats h c then "player wins\n"
|
||||
else if beats c h then "player loses\n"
|
||||
else "draw\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)
|
||||
game :: (Int, Int, Int) -> IO a
|
||||
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
|
||||
(if beats h c
|
||||
then "player wins\n"
|
||||
else if beats c h
|
||||
then "player loses\n"
|
||||
else "draw\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)
|
||||
main :: IO a
|
||||
main = game (1, 1, 1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue