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
data
statement to define a data typeDepth
holdingNoHole
,Shallow
orDeep
. -
Use a
data
statement to definePHG
, the pothole game data type, containing:- Current car position (
carpos
) - Current car health (
carhealth
) - Road conditions (
roadcond
), which is a list of pairsInt
position andDepth
severity of pothole.
- Current car position (
-
Create an example
ex1
representing 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. -
Create an example
ex2
representing 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 -> Int
that 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 -> Char
that 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
Depth
is at a particular location. If there is no pothole at that location, the depth should beNoHole
.roadh :: PHG -> Int -> Depth roadh _ _ = NoHole check_roadh = [ roadh ex1 8 == NoHole, roadh ex2 2 == Deep, roadh ex2 3 == Shallow ]
-
Write a function
roadmap :: PHG -> Int -> String
that takes in a numbern
and produces a string showing the conditions of the road in locations 1 up ton
. Use the letters fromrep
to represent the potholes.check_road = (roadmap ex1 12 == " DS S ")
-
The function
heal :: PHG -> Int -> PHG
increases the health of the car by the given number.check_heal = (55 == carhealth (heal ex1 10))
-
The function
driveOne :: PHG -> PHG
increases the car position by 1 and assesses whatever damage the pothole at that new position causes. WritedriveOne
.check_drive = (ex2 == driveOne ex1)