HASKELL Please submit a single Haskell file • Please put comments in your code to show which question you are answering with each piece of code. • You may create auxiliary functions if you like. You may use library functions from Haskell’s standard library. • Please limit your line lengths to 100 characters max. Please use the following two data types which you can copy-and-paste into your code. data Action = Rock | Paper | Scissors deriving (Eq, Show) data Outcome = Player1Win | Player2Win | Draw deriving Show Action represents a player’s chosen action and Outcome represents the outcome of playing a game. TASK Define a function check :: Action -> Action -> Outcome which calculates the outcome of a game where the first argument is the action of player 1 and the second argument is the action of player 2. For example, check Rock Paper = Player2Win and check Paper Paper = Draw. Define a function parse :: String -> Maybe Action which will be used to parse user input for the choice of an action. Return Nothing if the input string is not valid, otherwise parse strings into actions based solely on the first letter, e.g., parse "P" = Just Paper. Recall the I/O functions in Haskell for inputting a string from the user and outputting a string to the console: getLine :: IO String putStrLn :: String -> IO () Define a recursive function play :: Integer -> Integer -> IO () which takes two inputs: the number of wins so far for player 1 and the number of wins so far for player 2. The game should proceed as follows: • Print a message asking player 1 to make an action; • Read input from the user and parse it; • If the input is invalid according to the parser, print out the outcome of the game, i.e., who had the most wins and is therefore the overall winner, or whether overall it is a draw, and then return. • If the input is valid then: – Print a message asking player 2 to make an action; – Read input from the user and parse it; – If the input is invalid then print out the outcome of the whole game (see above). – If the input is valid then check to find out who is the winner and print an appropriate message, then recurse with the updated tally of player 1 and player 2 wins. Define main to start the game with a score of 0 for both players. Here is an example interaction which you should try to reproduce: *Main> main Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end. R Player 2: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end. P Player 2 wins! Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end. S Player 2: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end. R Player 2 wins! Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end. e Player 2 wins, with 2 games to 0 *Main> Multiple players can play together in a tournament, playing in pairs with the winner staying on to play in the next round. In this tournament, a player picks an action at the start and keeps playing that action, i.e., if they win with action A then in the next game they will again play action A. Tournaments can be represented by a binary tree, for which we use the following definition: type PlayerId = Integer data TournamentTree = Player Action PlayerId | Game TournamentTree TournamentTree deriving (Eq, Show) The nodes of the binary tree represent a game that will be played at some point. The leaves of the tree are players, which have a player id (an integer) and an action that they will take throughout the tournament. Here is an example tournament setup: example = Game (Game (Player Rock 1) (Player Scissors 2)) (Game (Player Paper 3) (Player Rock 4)) (a) Define a function playRound :: TournamentTree -> TournamentTree that plays one round of a tournament, i.e., it traverses the tournament tree and for any game that is ready to be played, of the form (Game (Player a1 id1) (Player a2 id2)), then the game is played (using check) and the resulting tournament tree has this node replaced with the winning player. For example: playRound example = Game (Player Rock 1) (Player Paper 3) If there is a draw in a game then its node is left as-is in the output tree.
HASKELL
Please submit a single Haskell file
• Please put comments in your code to show which question you are answering with each piece of
code.
• You may create auxiliary functions if you like. You may use library functions from Haskell’s
standard library.
• Please limit your line lengths to 100 characters max.
Please use the following two data types which you can copy-and-paste into your code.
data Action = Rock | Paper | Scissors deriving (Eq, Show)
data Outcome = Player1Win | Player2Win | Draw deriving Show
Action represents a player’s chosen action and Outcome represents the outcome of playing a game.
TASK
Define a function check :: Action -> Action -> Outcome which calculates the outcome of a
game where the first argument is the action of player 1 and the second argument is the action of
player 2.
For example, check Rock Paper = Player2Win and check Paper Paper = Draw.
Define a function parse :: String -> Maybe Action which will be used to parse user input for
the choice of an action. Return Nothing if the input string is not valid, otherwise parse strings
into actions based solely on the first letter, e.g., parse "P" = Just Paper.
Recall the I/O functions in Haskell for inputting a string from the user and outputting a string to the console:
getLine :: IO String
putStrLn :: String -> IO ()
Define a recursive function play :: Integer -> Integer -> IO () which takes two inputs:
the number of wins so far for player 1 and the number of wins so far for player 2. The game should
proceed as follows:
• Print a message asking player 1 to make an action;
• Read input from the user and parse it;
• If the input is invalid according to the parser, print out the outcome of the game, i.e., who had
the most wins and is therefore the overall winner, or whether overall it is a draw, and then
return.
• If the input is valid then:
– Print a message asking player 2 to make an action;
– Read input from the user and parse it;
– If the input is invalid then print out the outcome of the whole game (see above).
– If the input is valid then check to find out who is the winner and print an appropriate
message, then recurse with the updated tally of player 1 and player 2 wins.
Define main to start the game with a score of 0 for both players.
Here is an example interaction which you should try to reproduce:
*Main> main
Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
R
Player 2: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
P
Player 2 wins!
Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
S
Player 2: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
R
Player 2 wins!
Player 1: How will you play? Rock (R), Paper (P), or Scissors (S), or anything else to end.
e
Player 2 wins, with 2 games to 0
*Main>
Multiple players can play together in a tournament, playing in pairs with the winner staying on to
play in the next round. In this tournament, a player picks an action at the start and keeps playing
that action, i.e., if they win with action A then in the next game they will again play action A.
Tournaments can be represented by a binary tree, for which we use the following definition:
type PlayerId = Integer
data TournamentTree = Player Action PlayerId | Game TournamentTree TournamentTree
deriving (Eq, Show)
The nodes of the binary tree represent a game that will be played at some point. The leaves of the
tree are players, which have a player id (an integer) and an action that they will take throughout
the tournament. Here is an example tournament setup:
example = Game (Game (Player Rock 1) (Player Scissors 2))
(Game (Player Paper 3) (Player Rock 4))
(a) Define a function playRound :: TournamentTree -> TournamentTree that plays one round
of a tournament, i.e., it traverses the tournament tree and for any game that is ready to be
played, of the form (Game (Player a1 id1) (Player a2 id2)), then the game is played
(using check) and the resulting tournament tree has this node replaced with the winning
player.
For example: playRound example = Game (Player Rock 1) (Player Paper 3)
If there is a draw in a game then its node is left as-is in the output tree.
Step by step
Solved in 3 steps with 1 images