cs1705.tetris
Class Piece

java.lang.Object
  extended bycs1705.tetris.Piece

public final class Piece
extends Object

An immutable representation of a tetris piece in a particular rotation. Each piece is defined by the blocks that make up its body.

Version:
2003.10.13
Author:
Stephen Edwards (based on Nick Parlante's original)

Field Summary
static int LEFT_DOG
           
static int LEFT_L
           
static int RIGHT_DOG
           
static int RIGHT_L
           
static int SQUARE
           
static int STICK
           
static int T
           
 
Method Summary
 boolean equals(Object other)
          Returns true if two pieces are the same, that is, if their bodies contain the same points.
 Point[] getBody()
          Returns a pointer to the piece's body.
 int getHeight()
          Returns the height of the piece measured in blocks.
static Piece getPiece(int style, int rotations)
          Returns the requested piece style, rotated from its starting position by the specified number of counter-clockwise rotations.
static Piece[] getPieces()
          Returns an array containing the first rotation of each of the 7 standard tetris pieces.
 int[] getSkirt()
          Returns a pointer to the piece's skirt.
 int getWidth()
          Returns the width of the piece measured in blocks.
 Piece nextRotation()
          Returns a piece that is 90 degrees counter-clockwise rotated from the receiver.
 Piece nthRotation(int n)
          Returns a piece that is rotated counter-clockwise from the receiver by n 90-degree rotations.
 int numRotations()
          Determine the number of distinct rotations for this piece.
 int style()
          Check the "style" of piece this object represents.
 String toString()
          Returns a human-readable string representation of this piece.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LEFT_DOG

public static final int LEFT_DOG
See Also:
Constant Field Values

LEFT_L

public static final int LEFT_L
See Also:
Constant Field Values

RIGHT_DOG

public static final int RIGHT_DOG
See Also:
Constant Field Values

RIGHT_L

public static final int RIGHT_L
See Also:
Constant Field Values

SQUARE

public static final int SQUARE
See Also:
Constant Field Values

STICK

public static final int STICK
See Also:
Constant Field Values

T

public static final int T
See Also:
Constant Field Values
Method Detail

equals

public boolean equals(Object other)
Returns true if two pieces are the same, that is, if their bodies contain the same points. Interestingly, this is not the same as having exactly the same body arrays, since the points may not be in the same order in the bodies. Used internally to detect if two rotations are effectively the same.


getBody

public Point[] getBody()
Returns a pointer to the piece's body. The caller should not modify this array.


getHeight

public int getHeight()
Returns the height of the piece measured in blocks.


getPiece

public static Piece getPiece(int style,
                             int rotations)
Returns the requested piece style, rotated from its starting position by the specified number of counter-clockwise rotations.

Parameters:
style - the style of piece to look up
rotations - the number of times to rotate it

getPieces

public static Piece[] getPieces()
Returns an array containing the first rotation of each of the 7 standard tetris pieces. The next (counterclockwise) rotation can be obtained from each piece with the nextRotation() message. In this way, the client can iterate through all the rotations until eventually getting back to the first rotation.


getSkirt

public int[] getSkirt()
Returns a pointer to the piece's skirt. For each x value across the piece, the skirt gives the lowest y value in the body. This useful for computing where the piece will land. The caller should not modify this array.


getWidth

public int getWidth()
Returns the width of the piece measured in blocks.


nextRotation

public Piece nextRotation()
Returns a piece that is 90 degrees counter-clockwise rotated from the receiver.


nthRotation

public Piece nthRotation(int n)
Returns a piece that is rotated counter-clockwise from the receiver by n 90-degree rotations.

Parameters:
n - the number of rotations to apply

numRotations

public int numRotations()
Determine the number of distinct rotations for this piece.

Returns:
the number of different 90° rotations (between 1 and 4)

style

public int style()
Check the "style" of piece this object represents.

Returns:
one of Piece.T, Piece.SQUARE, Piece.STICK, Piece.LEFT_L, Piece.RIGHT_L, Piece.LEFT_DOG, Piece.RIGHT_DOG

toString

public String toString()
Returns a human-readable string representation of this piece. Note that this produces a multi-line representation.

Returns:
the printable representation of this piece