NOTE All functions should be implemented in Haskell Question

NOTE: All functions should be implemented in Haskell!

Question 2: Rock-Paper-Scissors (20 points) In the game rock-paper-scissors, each player picks one of the three options. If one player chooses rock and the other paper, paper will win. If one chooses paper and the other scissors, scissors will win. If one chooses scissors, and the other rock, then rock will win. If both players pick the same option, it is a tie. 2.1 Create a data type that can be used for this game. [4 points] 22 Using the type from part (2.1), write the type description for a function, rps, that will take two players\' moves and return either 1 if player 1 wins, 2 if player 2 wins, or 0 if it\'s a tie. [4 points]

Solution

import System.Random

data Move = Rock | Paper | Scissor deriving (Show, Eq, Enum)

instance Ord Move where
(<=) x y = x == y || (x,y) `elem` [(Rock,Paper),(Paper,Scissor),(Scissor,Rock)]

userSelect :: IO Move
userSelect = fmap (toEnum . read) getLine

machineSelect :: IO Move
machineSelect = fmap toEnum (randomRIO (0,2))

Result :: Ordering -> String
Result y = case y of
LT -> \"User Won\"
EQ -> \"Game Draw !\"
GT -> \"System Won\"

main :: IO ()
main = do
putStrLn \"Choose a move (0-Rock, 1-Paper, 2-Scissor) :\"
userMove <- userSelect
machineMove <- machineSelect
putStrLn $ show userMove ++ \" (you) vs \" ++ show machineMove ++ \" (computer)\"
putStrLn $ Result $ userMove `compare` machineMove
main

-------------
if u geting some error about System.random, then u have to install below package:
sudo apt-get install
cabal-install cabal update
cabal install random

NOTE: All functions should be implemented in Haskell! Question 2: Rock-Paper-Scissors (20 points) In the game rock-paper-scissors, each player picks one of the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site