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.

  1. Use a data statement to define a data type Depth holding NoHole, Shallow or Deep.

  2. Use a data statement to define PHG, the pothole game data type, containing:

    1. Current car position (carpos)
    2. Current car health (carhealth)
    3. Road conditions (roadcond), which is a list of pairs Int position and Depth severity of pothole.
  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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 be NoHole.

     roadh :: PHG -> Int -> Depth
     roadh _ _ = NoHole
    
     check_roadh = [ roadh ex1 8 == NoHole,
                     roadh ex2 2 == Deep,
                     roadh ex2 3 == Shallow ]
    
  8. Write a function roadmap :: PHG -> Int -> String that takes in a number n and produces a string showing the conditions of the road in locations 1 up to n. Use the letters from rep to represent the potholes.

     check_road = (roadmap ex1 12 == " DS     S   ")
    
  9. The function heal :: PHG -> Int -> PHG increases the health of the car by the given number.

     check_heal = (55 == carhealth (heal ex1 10))
    
  1. The function driveOne :: PHG -> PHG increases the car position by 1 and assesses whatever damage the pothole at that new position causes. Write driveOne.

    check_drive = (ex2 == driveOne ex1)