Problem Solving Assignment 9
TShape.java
Overview
TShape.java is a class that encapsulates what it means to be a shape in tetris
the graphical representation of the shape is a 2D array of integers where 0 values
represent open space and non zero values represent a block of a certain color. This
is not a class that you need to modify, but is a class that you need to understand.
Especially how it interacts with the tetris board as a whole. The algorithm is to
superimpose the piece onto a sequence of positions on the board and to copy the
values over.
Instance Variables
- px, py denotes the position of the TShape on the 2D board.
It is actually the left most and highest most point of the shape on the
2D board. When the shape is drawn it will begin drawing from this point
and draw the entire shape with relation to this point. Therefore if we
have the square shape at position px = 0 py = 0 then it means that the
top left
most block of the square is at the top left most position in the array
- the shapeGrid is a 2D representation of the piece in an array form.
It cannot be a ragged array.
- the TOTAL_WEIGHT variable is a final that represents the total weight of all of the shapes that can be generated
It can be tweaked to add in your shapes as you like it.
- rgen is a Random number generator
there's no real reason you need to change this, but feel free to use it.
- The private static final 2D int arrays are a representation of all of the shapes are producable in the game.
Zero entries represent empty space and non zero entries represent that
a block should be drawn at that point. Different numbers represent
different colors. To have a sense of how many colors are allowable, see
the public void paint(Graphics g) method in GamePanel.java and the
public void paint(Graphics g) method in PreviewPanel, an inner class in
the TetrisWindow.java file.
Methods
- public TShape() generates a TShape with a random pattern, chosen among the private
static 2D int arrays. It calls the public TShape(int type) constructor
- public TShape(int posx, int posy) calls the default constructor and then sets the px
and py of the shape to the incoming posx and posy. Note that posx and poxy are
positions in the array NOT positions in the 2D graphics space. This should be called
whenever actually trying to place a piece on the board
- public TShape(int type) generates a TShape according to the incoming type variable.
The types are weighted differently. This can be seen in the switch statement. If the
incoming parameter type is 0 then the TShape will be the long rectangle. If it is
14, 15, 16, or 17 then it will be one of the Z style shapes that's infamous for
screwing you at the right moment.
- public setPos(int x, int y) is the only mutator for the position, you're free to make
others if you feel the need to. The current position (px, py) of the current shape is
set to the new incoming position. To move the block one space to the right you would
simply call the setPos with the current x value + 1.
- public synchronized void rotateCW(int maxw) rotates the piece clockwise. maxw is the length
of the board.
- public synchronized void rotateCCW(int maxw) rotates the piece counter clockwise. maxw is
the length of the board. You should call both rotate functions from GameLogic.java's rotate
methods unless you want to write your own rotate methods.
- The last few methods are accessors that you might want to use in your GameLogic class.