8.3

Homework 7

home work!

Programming Language ISL+

Due Date Tue 10/26 at 9:00pm (Week 7)

Purpose To make BoxOut into a playable game

Finger Exercises

None

Graded Exercises

In this assignment, you’ll add enough behavior to BoxOut to make it a playable game. You will continue to work with your partner from Homework 5, to improve your previous milestone for this project.

Revised data definitions: Many of you came up with a variety of data definitions that were of widely varying usefulness. Here are the data definitions you should use for part 2 of this project. You will need to review and revise your existing code to use these new definitions.

(define-struct world [size lives regions wall mouse])
; A World is a
; (make-world Posn Number [List-of Region] Wall Posn)
; INTERPRETATION: This keeps track of the size of the gameboard (in pixels),
; the number of lives the player has remaining, the collection of
; currently playable regions, the wall (if any), and the current position
; of the mouse cursor.  Note: All regions must have bounds between the origin
; and the size of this world.
 
(define-struct region [bounds lob])
; A Region is a (make-region Bounds [List-of Ball])
; INTERPRETATION: This keeps track of the position and size of the region,
; and the balls bouncing within it.  
 
(define-struct bounds [top-left bottom-right])
; A Bounds is a (make-bounds Posn Posn)
; INTERPRETATION: This records the coordinates of the top-left and
; bottom-right corners of each rectangular region of the board.
 
(define-struct ball [pos vel])
; A Ball is a (make-ball Posn Velocity)
; INTERPRETATION: This records the current position (in pixels)
; and velocity (in pixels/tick) of an individual ball.
 
(define-struct vel [dx dy])
; A Velocity is a (make-vel Number Number)
; INTERPRETATION: The velocity of something (in pixels/tick) in the x and y
; directions
 
; A Posn is a (make-posn Number Number)
; INTERPRETATION: The position of something (in pixels), measured from (0,0)
; in the top-left corner, with +x pointing right and +y pointing down.
 
; A Direction is one of:
; - "horizontal"
; - "vertical"
 
(define-struct wall [direction top-left bottom-right])
; A Wall is one of:
; - Direction
; - (make-wall Direction Posn Posn)
; INTERPRETATION: Either a direction for the next wall that gets created,
; or the direction of growth, and the top-left and bottom-right corners of
; a growing wall.
 
(define WALL-THICKNESS 4)
(define BALL-RADIUS 5)

If your data definitions on Homework 5 were substantially different from these (i.e., you may have had slightly different names and that’s fine, but if your data was organized differently, or you were missing at least one of these definitions, that’s an interesting difference), leave a comment in your code explaining why you think they differed. Did you misunderstand a part the previous assignment? Did you think of a different solution than we did?

List abstractions: You must revise your code to use list abstractions everywhere they are appropriate. In addition, you must use list abstractions in all the new functionality you add in this assignment.

Walls: There is only at most one wall ever in progress in a game of BoxOut, which is ensured by our data definitions above. The behavior of walls depends on whether a wall is currently growing or not. If there is no wall:
  • When a player presses the "t" key, the wall direction toggles between horizontal and vertical.

  • When a player clicks the mouse inside a playable region, a tiny wall, WALL-WIDTH pixels wide and tall, is created centered at the current mouse location, and growing in the currently selected direction.

If there is currently a wall:
  • On every tick, the wall grows by some fixed speed (for the game to be fun, this speed should be faster than the balls’ speeds) at both ends of the wall.

  • If one end of the wall hits the boundary of the region the wall is growing in, it should be truncated and limited to that boundary of the region. The other end is free to keep growing.

  • Once both ends of the wall have hit the boundary of the region the wall is growing in, the wall will split the region (see below).

  • If a ball hits the wall while it is still growing, the wall disappears and the player loses a life.

Note: our rules for wall growth and balls-breaking-walls does not include the ability to destroy “one half of a wall”, as the original game did. Why do you think we chose that simplification? What would you have to change, above, in order to support that enhancement? You should not attempt it for this homework! But it’s a fun puzzle to think about...

Splitting regions: When both ends of a growing wall touch the boundary of a region, the wall splits the region into two parts. The area covered by the wall itself will belong to neither of the two new regions (i.e., the sum of the areas of the two new regions is strictly smaller than the area of the original region). The list of balls in the region is then partitioned into two lists, of balls that belong in either of the two new regions. (Note that no balls could “get stuck” in the area between the two regions, since that area belonged to the wall, and if a ball were in that region, it would already have destroyed the wall before it could split the regions!) If all of the balls wind up on one side of the wall, and the other region is empty, that empty region is eliminated. The new region(s) replace the original region in the world state. Finally, the wall itself should go away, and the world state should just preserve the current direction of wall growth, for the next time the player clicks to create a wall.

Scoring and new levels: Enhance your new-random-level to start the player with one more life than there are balls in the level.

Enhance your on-tick handler to check if the level has been won, and if so, to start a new random level with one additional ball than the current level has.

Enhance your to-draw handler to show the number of lives remaining, and the current covered-area percentage, somewhere above the game-board image itself. Also ensure that your drawing function correctly shows the current direction of wall growth, as shown in Homework 5.