Pothole
In the pothole “game”, you drive on a rough road. The road is
represented by the number line [1,2,3...]. Your car starts at
position 1 with 50 points. You get 10 points for moving to the next
square, and 20 points for quitting before you break down.
Every location on the road (integer) has either “no hole”, or a “shallow” or “deep” pothole. You do not need to explicitly mark locations with no pothole.
-
Use a
datastatement to define a data typeDepthholdingNoHole,ShalloworDeep. -
Use a
datastatement to definePHG, the pothole game data type, containing:- Current car position (
carpos) - Current car health (
carhealth) - Road conditions (
roadcond), which is a list of pairsIntposition andDepthseverity of pothole.
- Current car position (
-
Create an example
ex1representing a car with health 45 at position 8. The road should have a deep pothole at position 2, and shallow potholes at positions 3 and 9. Do not include any entries forNoHole; just include actual potholes. -
Create an example
ex2representing a car with health 42 at position 9. The road should have a deep pothole at position 2, and shallow potholes at positions 3 and 9. -
Write a function
damage :: Depth -> Intthat tells how much damage a pothole will do. Shallow potholes do 3 damage and deep potholes do 7 damage. Nothing else does damage. -
Write a function
rep :: Depth -> Charthat gives a string to represent the depth of the pothole. Use S for shallow potholes, D for deep potholes, and a space for anything else. -
Write a helper function to determine what the
Depthis at a particular location. If there is no pothole at that location, the depth should beNoHole.findDepth :: PHG -> Int -> Depth findDepth _ _ = NoHole check_roadh = [ findDepth ex1 8 == NoHole, findDepth ex2 2 == Deep, findDepth ex2 3 == Shallow ] -
Write a function
roadmap :: PHG -> Int -> Stringthat takes in a numbernand produces a string showing the conditions of the road in locations 1 up ton. Use the letters fromrepto represent the potholes.check_road = (roadmap ex1 12 == " DS S ") -
The function
heal :: PHG -> Int -> PHGincreases the health of the car by the given number.check_heal = (55 == carhealth (heal ex1 10))
-
The function
driveOne :: PHG -> PHGincreases the car position by 1 and assesses whatever damage the pothole at that new position causes. WritedriveOne.check_drive = (ex2 == driveOne ex1)