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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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. 

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education