net.percederberg.tetris
Class Figure

java.lang.Object
  |
  +--net.percederberg.tetris.Figure

public class Figure
extends java.lang.Object

A class representing a Tetris square figure. Each figure consists of four connected squares in one of seven possible constellations. The figures may be rotated in 90 degree steps and have sideways and downwards movability.

Each figure instance can have two states, either attached to a square board or not. When attached, all move and rotation operations are checked so that collisions do not occur with other squares on the board. When not attached, any rotation can be made (and will be kept when attached to a new board).

Version:
1.2
Author:
Per Cederberg, per@percederberg.net

Field Summary
static int LEFT_ANGLE_FIGURE
          A figure constant used to create a figure forming a left angle.
static int LINE_FIGURE
          A figure constant used to create a figure forming a line.
static int RIGHT_ANGLE_FIGURE
          A figure constant used to create a figure forming a right angle.
static int S_FIGURE
          A figure constant used to create a figure forming an "S".
static int SQUARE_FIGURE
          A figure constant used to create a figure forming a square.
static int TRIANGLE_FIGURE
          A figure constant used to create a figure forming a triangle.
static int Z_FIGURE
          A figure constant used to create a figure forming a "Z".
 
Constructor Summary
Figure(int type)
          Creates a new figure of one of the seven predefined types.
 
Method Summary
 boolean attach(SquareBoard board, boolean center)
          Attaches the figure to a specified square board.
 void detach()
          Detaches this figure from its square board.
 int getRotation()
          Returns the current figure rotation (orientation).
 boolean hasLanded()
          Checks if the figure has landed.
 boolean isAllVisible()
          Checks if the figure is fully visible on the square board.
 boolean isAttached()
          Checks if this figure is attached to a square board.
 void moveAllWayDown()
          Moves the figure all the way down.
 void moveDown()
          Moves the figure one step down.
 void moveLeft()
          Moves the figure one step to the left.
 void moveRight()
          Moves the figure one step to the right.
 void rotateClockwise()
          Rotates the figure clockwise.
 void rotateCounterClockwise()
          Rotates the figure counter-clockwise.
 void rotateRandom()
          Rotates the figure randomly.
 void setRotation(int rotation)
          Sets the figure rotation (orientation).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SQUARE_FIGURE

public static final int SQUARE_FIGURE
A figure constant used to create a figure forming a square.

LINE_FIGURE

public static final int LINE_FIGURE
A figure constant used to create a figure forming a line.

S_FIGURE

public static final int S_FIGURE
A figure constant used to create a figure forming an "S".

Z_FIGURE

public static final int Z_FIGURE
A figure constant used to create a figure forming a "Z".

RIGHT_ANGLE_FIGURE

public static final int RIGHT_ANGLE_FIGURE
A figure constant used to create a figure forming a right angle.

LEFT_ANGLE_FIGURE

public static final int LEFT_ANGLE_FIGURE
A figure constant used to create a figure forming a left angle.

TRIANGLE_FIGURE

public static final int TRIANGLE_FIGURE
A figure constant used to create a figure forming a triangle.
Constructor Detail

Figure

public Figure(int type)
       throws java.lang.IllegalArgumentException
Creates a new figure of one of the seven predefined types. The figure will not be attached to any square board and default colors and orientations will be assigned.
Parameters:
type - the figure type (one of the figure constants)
Throws:
java.lang.IllegalArgumentException - if the figure type specified is not recognized
See Also:
SQUARE_FIGURE, LINE_FIGURE, S_FIGURE, Z_FIGURE, RIGHT_ANGLE_FIGURE, LEFT_ANGLE_FIGURE, TRIANGLE_FIGURE
Method Detail

isAttached

public boolean isAttached()
Checks if this figure is attached to a square board.
Returns:
true if the figure is already attached, or false otherwise

attach

public boolean attach(SquareBoard board,
                      boolean center)
Attaches the figure to a specified square board. The figure will be drawn either at the absolute top of the board, with only the bottom line visible, or centered onto the board. In both cases, the squares on the new board are checked for collisions. If the squares are already occupied, this method returns false and no attachment is made.

The horizontal and vertical coordinates will be reset for the figure, when centering the figure on the new board. The figure orientation (rotation) will be kept, however. If the figure was previously attached to another board, it will be detached from that board before attaching to the new board.

Parameters:
board - the square board to attach to
center - the centered position flag
Returns:
true if the figure could be attached, or false otherwise

detach

public void detach()
Detaches this figure from its square board. The figure will not be removed from the board by this operation, resulting in the figure being left intact.

isAllVisible

public boolean isAllVisible()
Checks if the figure is fully visible on the square board. If the figure isn't attached to a board, false will be returned.
Returns:
true if the figure is fully visible, or false otherwise

hasLanded

public boolean hasLanded()
Checks if the figure has landed. If this method returns true, the moveDown() or the moveAllWayDown() methods should have no effect. If no square board is attached, this method will return true.
Returns:
true if the figure has landed, or false otherwise

moveLeft

public void moveLeft()
Moves the figure one step to the left. If such a move is not possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, nothing is done.

moveRight

public void moveRight()
Moves the figure one step to the right. If such a move is not possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, nothing is done.

moveDown

public void moveDown()
Moves the figure one step down. If such a move is not possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, nothing is done.

moveAllWayDown

public void moveAllWayDown()
Moves the figure all the way down. The limits of the move are either the square board bottom, or squares not being empty. If no move is possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, nothing is done.

getRotation

public int getRotation()
Returns the current figure rotation (orientation).
Returns:
the current figure rotation

setRotation

public void setRotation(int rotation)
Sets the figure rotation (orientation). If the desired rotation is not possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, the rotation is performed directly.
Parameters:
rotation - the new figure orientation

rotateRandom

public void rotateRandom()
Rotates the figure randomly. If such a rotation is not possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, the rotation is performed directly.

rotateClockwise

public void rotateClockwise()
Rotates the figure clockwise. If such a rotation is not possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, the rotation is performed directly.

rotateCounterClockwise

public void rotateCounterClockwise()
Rotates the figure counter-clockwise. If such a rotation is not possible with respect to the square board, nothing is done. The square board will be changed as the figure moves, clearing the previous cells. If no square board is attached, the rotation is performed directly.