Homework 7
Purpose This problem set concerns the design and
creation of a larger program —
Important! Because exam 1 is on Friday, we have provided an extension on this homework, to Thursday, March 2.
Problem 1 Develop the Bin-Packing game.
A "tetra" is made of four square-shaped "bricks." There are seven such shapes you can make out of four bricks; each one is called a "tetra." A tetra falls from just above the top of a 10x20-block grid at a constant rate and as it falls the user can navigate the tetra left and right and rotate the tetra. The tetra cannot be moved off of the grid and once it lands on the base of the grid or another tetra, it is frozen in position and a new tetra, of randomly selected kind, falls from the ceiling. The game is over as soon as a tetra lands but extends beyond the top of the grid.
The game is scored by counting the number of bricks that have been fit onto the grid. (Which is basically four times the number of tetras that have been fit onto the grid.)
left arrow: Shift the current piece left.
right arrow: Shift the current piece right.
s: Rotate the current piece 90 degrees clockwise.
a: Rotate the current piece 90 degrees counterclockwise.
"O"
"I"
"L"
"J"
"T"
"Z"
"S"
This game is unlike Tetris in that bricks never go away.
The following data definitions may or may not be useful (and indicative of the kind of operations you might want to develop) for writing this program:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
;;; Data Definitions |
|
;;; A Brick is a (make-brick Number Number Color) |
(define-struct brick [x y color]) |
|
;;; A Pt (2D point) is a (make-posn Integer Integer) |
;;; |
;;; A Tetra is a (make-tetra Pt Bricks) |
;;; The center point is the point around which the tetra |
;;; rotates when it spins. |
(define-struct tetra [center bricks]) |
|
;;; A Bricks (Set of Bricks) is one of: |
;;; - empty |
;;; - (cons Brick Bricks) |
;;; Order does not matter. |
|
;;; A World is a (make-world Tetra Bricks) |
;;; The set of bricks represents the pile of bricks |
;;; at the bottom of the screen. |
(define-struct world [tetra pile]) |
Rotations are a little tricky; here’s how you do it. Assuming the above data definitions, this code will perform a ninety-degree, counterclockwise rotation of a brick around a given point:
;; brick-rotate-ccw : Brick Pt -> Brick |
;; Rotate the brick 90 counterclockwise around the posn. |
(define (brick-rotate-ccw sqr c) |
(make-brick (+ (posn-x c) |
(- (posn-y c) |
(brick-y sqr))) |
(+ (posn-y c) |
(- (brick-x sqr) |
(posn-x c))) |
(brick-color sqr))) |
A clockwise rotation is just the same as three counterclockwise rotations. If you look at the rotation code, you’ll see that as long as the coordinates of both the rotation center and the brick are integers, the result coordinates will also be integers.
Note that you must choose the rotation point for each tetra carefully: if the tetra’s bricks are all grid aligned before the rotation, you want them to be grid-aligned after the rotation, too. For example, if you rotate the "long" tetra (four bricks in a horizontal straight line) around its exact center-point, it will wind up between two columns of the grid. If you set the rotation point as the center of either brick 2 or brick 3 in the tetra, then the long tetra will rotate a bit eccentrically, but bricks will always land on grid bricks.
You may implement more features, if you like (e.g., fancier scoring, increasing difficulty, game pausing). However, extra features won’t save you from points taken off if your code has bugs or isn’t well written. You will not receive 100% credit simply for having code that works. For full credit, your code must work and be well written. So you should put your effort into writing clean, readable, bug-free code.
You will be upgrading the game to full Tetris in a future assignment. So effort expended in making sure that your code is clean and well-written will be rewarded when you have to extend it later —
it is quite difficult to modify and extend code that is a snarled-up, confused pile of chaos. As always, we advise you that the Design Recipe will help you get your code written.
Start early. It will take you time to work out the assignment.