Here is a checklist of things that your program should include: laare to enter t
ID: 3752214 • Letter: H
Question
Here is a checklist of things that your program should include: laare to enter their names and use those names in the program Validate the nser input (For example, make sure that a user doesn't enter -1 for the number of points that it takes to win the game). Give appropriate feedback so it's clear to the users how the game is played and how the game is progressing. Provide a short introduction to your game and a conclusion. Your program must break ties by allowing the players to re-select gestures in each round until there is not a tie. . Your main function (main - do) should not do all the work; it should invoke other functions to run the "guts" of the game. You should use at least two other functions. Some examples include functions to determine a winner, get/return gesture throws, print the welcome and/or print . the rules. IJ AirExplanation / Answer
import qualified Data.Set as Set
import Test.HUnit
data Gesture = Rock | Paper | Scissors | Lizard | Spock deriving (Eq, Ord, Show)
data VersusResult = Won | Tie | Lost deriving (Eq, Show)
versus :: Gesture -> Gesture -> (VersusResult, VersusResult)
versus x y
| x == y = (Tie, Tie)
| x `superiorTo` y = (Won, Lost)
| otherwise = (Lost, Won)
superiorTo :: Gesture -> Gesture -> Bool
superiorTo x y = Set.member y $ getGesturesInferiorTo x
getGesturesInferiorTo :: Gesture -> Set.Set Gesture
getGesturesInferiorTo Rock = Set.fromList [Scissors, Lizard ]
getGesturesInferiorTo Paper = Set.fromList [Rock, Spock ]
getGesturesInferiorTo Scissors = Set.fromList [Paper, Lizard ]
getGesturesInferiorTo Lizard = Set.fromList [Paper, Spock ]
getGesturesInferiorTo Spock = Set.fromList [Rock, Scissors]
-- tests
versusTests = map toTest [
(Rock, Rock, (Tie, Tie )),
(Rock, Paper, (Lost, Won )),
(Rock, Scissors, (Won, Lost)),
(Rock, Lizard, (Won, Lost)),
(Rock, Spock, (Lost, Won )),
(Paper, Rock, (Won, Lost)),
(Paper, Paper, (Tie, Tie )),
(Paper, Scissors, (Lost, Won )),
(Paper, Lizard, (Lost, Won )),
(Paper, Spock, (Won, Lost)),
(Scissors, Rock, (Lost, Won )),
(Scissors, Paper, (Won, Lost)),
(Scissors, Scissors, (Tie, Tie )),
(Scissors, Lizard, (Won, Lost)),
(Scissors, Spock, (Lost, Won )),
(Lizard, Rock, (Lost, Won )),
(Lizard, Paper, (Won, Lost)),
(Lizard, Scissors, (Lost, Won )),
(Lizard, Lizard, (Tie, Tie )),
(Lizard, Spock, (Won, Lost)),
(Spock, Rock, (Won, Lost)),
(Spock, Paper, (Lost, Won )),
(Spock, Scissors, (Won, Lost)),
(Spock, Lizard, (Lost, Won )),
(Spock, Spock, (Tie, Tie ))]
where
toTest :: (Gesture, Gesture, (VersusResult, VersusResult)) -> Test
toTest (x, y, expected) = label . TestCase $ assertEqual "" actual expected
where
actual = x `versus` y
label = TestLabel (show x ++ " vs. " ++ show y)
main = runTestTT (TestList versusTests)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.