Lab | Assigned | Design Due (Mondays 2 PM) |
Lab Due (Fridays 2 PM) |
|||
---|---|---|---|---|---|---|
9 | Apr | 12 | Apr | 16 | Apr |
The design will be discussed Monday in class. In Lab, your graded design will be returned. No other handouts or code will be issued. So, think carefully about your design!
Zip includes:
Remember to type the following lines at the top of any files that use the terminal or canvas classes.
import cs101.terminal.*; import cs101.canvas.*;
Although this lab does not involve a lot of coding, it is tricky. In the design, you apply (high-school) algebra to simplify what must be done in the lab.
This lab will combine with the next lab to make a Tetris game. The division of work is as follows:
It is helpful to think of a tetris shape as if it were constructed of squares that are glued together:
Above, you see a tetris piece (the Gamma piece, named for the shape of the Greek letter) in outline form, as you would see the piece when you play. | Above, you see how the piece is constructed
using four square blocks.
You see that one of the square blocks is distinguished; it is the main component, or the first block put down to form this piece. The entire piece is constructed of such blocks, glued together. When block A is glued to block B, block A must follow the joined side around, wherever that side of block A might go. Each block is added to the piece in this way. When the main block moves or rotates, so do its sides. Any piece joined to a side that moves also moves, which moves its sides. By moving one block of the piece, the entire piece eventually moves. |
So, each block follows its glued side around. Even the first block of a Tetris piece is glued to a special side, known as the root side of the tetris piece. The root is invisible, but movement of the root side causes the main block to move, which causes the entire piece to move, as described above.
Above you see another kind of
tetris piece, the zigzag piece. Zigzag
is also constructed from four blocks,
but glued together differently from the first
piece you saw.
In this discussion, suppose the blocks in the above piece are placed in the order A, B, C, D. |
When a block is placed, it must be glued to some side.
A side is essentially a vector,
with a length and direction. Above, you see a vector to the left of
the partially constructed block A. The vector runs from South to North.
The vector that induces block A is shown to the left of block A, but really the vector conincides exactly with side 0 of block A. The sides of a block are established as follows:
All squares are drawn in this manner. Side 0 is the first side drawn, side 1 is the next side drawn, and so on. Thus, picture above shows sides 0, 1, and 2 completed for block A. Side 3 would be drawn next, closing the square. |
When block C is glued to block A, side 1 of block A is chosen as the
inducing vector. Thus, C's side 0 is drawn as the reverse of
A's side 1. The remaining sides of B are drawn by moving counterclockwise
to complete the square.
The picture above shows blocks A and C separated by space, but really they would abut. Side 0 of C would coincide with side 1 of A. |
The side that induces a block always coincides with the block's Side 0, except that the inducing side runs the opposite direction. Once Side 0 of a block is determined, the other sides are given, since they proceed counterclockwise, they are are all the same size, and they form the sides of a square.
Side 0 of a block will either be horizontal or vertical, and will either point East, West, North, or South. Let us assume the side is drawn from
North | (x,y) to (x,y-s) |
East | (x,y) to (x+s,y) |
South | (x,y) to (x,y+s) |
West | (x,y) to (x-s,y) |
Side | From | To |
---|---|---|
0 | (x,y) | (x+h, y+k) |
1 | (x+h,y+k) | (x+h+k, y+k-h) |
2 | (x+h+k,y+k-h) | (x+k, y-h) |
3 | (x+k,y-h) | (x, y) |
Translating (moving) a side is simple: we can change the coordinates of the side's starting and ending points by displacement.
Rotation is trickier. To rotate a tetris piece, we must rotate the side that induces its main piece.
The letter s refers to the length of the side. Thus, the original vector goes from (x,y) to (x,y-s) on a CS101 Graphics Canvas, because the "y" coordinate increases as you move down the screen.
We need a function that tells a side how to move when it is rotated. One part is easy. Note that when a vector rotates, its new ending position is its old starting position. But what about the new starting position?
Previous | Next | ||
start.x | start.y | start.x | start.y |
x | y | x+s | y |
x+s | y | x+s | y-s |
x+s | y-s | x | y-s |
x | y-s | x | y |
previous next start.x start.y start.x | | | | V V x a + y b + c = x+s (x+s) a + y b + c = x+s (x+s) a + (y-s) b + c = x x a + (y-s) b + c = xSimilarly, we can formulate equations for the next start.y from the previous start.x and start.y:
previous next start.x start.y start.y | | | | V V x d + y e + f = y (x+s) d + y e + f = y-s (x+s) d + (y-s) e + f = y-s x d + (y-s) e + f = y
Point
final public
that declares x
and
y
instance variables.
Startup
selfTest
calls for any classes you add.
Side
void displace(int shiftX, int shiftY)
moveTo
, so that
the listener can be informed of the move.
void registerSideListener(TetrisBlock)
TetrisBlock
as the object
that should be informed, via its sideMoved
, whenever this
side moves.
void notifyListener()
moveTo(Point start, Point end)
TetrisBlock
getSide(int)
TetrisBlock
.
sideMoved()
TetrisBlock
is a listener
of its inducing side.
TetrisPiece
inducer
instance variable is the inducing side.
exercise
This class is abstract, and its subclasses must provide a name for the piece.
TetrisSquare
Important reminder: Collaborate as much as you like on the conceptual and design aspects of labs. You can work together to solve the equations, too. But the write-up you turn in is to be your own work. For more information, consult the course's collaboration policy |
TetrisZigZag
and TetrisGamma
to make
the appropriate shapes.