Ch08 Quiz A

Quiz on basic Data and Code World.

In this game you will work with four kinds of shapes. The circles have radius 1.5 and the squares have side length 3.0.

Clicking on a shape means anywhere within the shape, not just its exact center.

  1. Make a data type MyShape that can represent only Circle, Square, Complete, or Gremlin.

  2. Make a data type Mode that is either Placing or Gaming.

  3. Make a type alias called ShapeLoc that is an ordered pair of a Point and a MyShape.

  4. Make a data type Game that holds a Mode and a list of ShapeLoc. Use the record syntax where you name the fields mode and shapes in the definition.

  5. Write a drawBasic :: MyShape -> Picture function to create the image for a each shape, centered at (0,0).

    • Circle - radius 1.5, outline
    • Square - side length 3.0, outline
    • Compete - radius 1.5 solid orange circle
    • Gremlin - side length 1.5 x 3.0 solid green rectangle
  6. Write a drawing function drawh :: Game -> Picture that places the basic shapes from each ShapeLoc at the point it is paired with.

  7. Write the clickedOn :: Point -> ShapeLoc -> Bool function to determine if a click is on the shape.

     clickedOn :: Point -> ShapeLoc -> Bool
     clickedOn pt shapepos = False
    
  8. Write advance which moves a MyShape to the next one. Circle to Square to Complete. After Complete do not advance more. Gremlin should not advance

  9. Write an event handler that handles the clicks of the mouse. In Placing mode, place a circle at the mouse click. In Gaming mode, “advance” the shape clicked on to the next shape.

  10. Write an event handler for key presses:

    • “A” key sets Placing mode
    • “D” key sets Gaming mode
  11. Write a function to find what shape was clicked on.

    findGremlin :: Point -> [ShapeLoc] -> Maybe ShapeLoc
    
  12. Write a function to remove a given ShapeLoc from the list, convert it into a Gremlin at the same location, and put it on top of any other shapes.

    convertGremlin :: ShapeLoc -> [ShapeLoc] -> [ShapeLoc]
    
  13. Modify your event handler so that mouse clicks inside of an existing shape (findGremlin) in Placing mode turn that shape and the one you place into Gremlins.

Helper Functions

dist :: Point -> Point -> Double
dist (x0,y0) (x1,y1) = sqrt $ (x0-x1)**2 + (y0-y1)**2

-- only good for squares as written
distrect :: Point -> Point -> Double
distrect (x0,y0) (x1,y1) = max dx dy
  where dx = abs(x0-x1)
        dy = abs(y0-y1)

Extras

You could have a visible circle follow the mouse in Placing mode. That would require some changes.

Last modified November 22, 2024: A somewhat challenging quiz. 50 min! (1fa715e)