cs1705.tetris
Class Board

java.lang.Object
  extended bycs1705.tetris.Board

public final class Board
extends Object

Represents a Tetris board -- essentially a 2-d grid of booleans. Supports tetris pieces and row clearning. Has an "undo" feature that allows clients to add and remove pieces efficiently. Does not do any drawing or have any idea of pixels. Intead, just represents the abstract 2-d board.

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

Field Summary
static int PLACE_BAD
           
static int PLACE_OK
           
static int PLACE_OUT_BOUNDS
           
static int PLACE_ROW_FILLED
           
 
Constructor Summary
Board(int aWidth, int aHeight)
          Creates an empty board of the given width and height measured in blocks.
Board(int aWidth, int aHeight, String[] rows)
          Creates a board of the given width and height measured in blocks, and then populates it with the blocks indicated by the given string array.
 
Method Summary
 int apply(Move move)
          Attempts to add apply the given move to the board.
 boolean clearRows()
          Deletes rows that are filled all the way across, moving things above down.
 void commit()
          Puts the board in the committed state.
 boolean equals(Object other)
          Returns true if two boards are the same, meaning they have blocks in exactly the same locations.
 int getBlocksInRow(int y)
          Returns the number of filled blocks in the given row.
 int getColumnHeight(int x)
          Returns the height of the given column, which is the y value of the highest block + 1.
 int getHeight()
          Returns the height of the board in blocks.
 int getLargestHeight()
          Returns the max column height present in the board.
 int getWidth()
          Returns the width of the board in blocks.
 boolean hasBlockAt(int x, int y)
          Returns true if the given block is filled in the board.
 int place(Piece piece, int x, int y)
          Attempts to add the body of a piece to the board.
 int rowAfterDrop(Piece piece, int x)
          Given a piece and an x, returns the y value where the piece would come to rest if it were dropped straight down at that x.
 void sanityCheck()
          Checks the board for internal consistency -- used for debugging.
 String toString()
          Returns a human-readable string representation of this board.
 void undo()
          If a place() happens, optionally followed by a clearRows(), a subsequent undo() reverts the board to its state before the place().
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PLACE_BAD

public static final int PLACE_BAD
See Also:
Constant Field Values

PLACE_OK

public static final int PLACE_OK
See Also:
Constant Field Values

PLACE_OUT_BOUNDS

public static final int PLACE_OUT_BOUNDS
See Also:
Constant Field Values

PLACE_ROW_FILLED

public static final int PLACE_ROW_FILLED
See Also:
Constant Field Values
Constructor Detail

Board

public Board(int aWidth,
             int aHeight)
Creates an empty board of the given width and height measured in blocks.


Board

public Board(int aWidth,
             int aHeight,
             String[] rows)
Creates a board of the given width and height measured in blocks, and then populates it with the blocks indicated by the given string array. Only rows from the highest non-empty row down need to be present in the array. Each string in the array is interpreted as one row (in top-to-bottom order). Each character in the string is interpreted as one location, with a whitespace character indicating an empty location and a non-whitespace character indicating a block.

Method Detail

apply

public int apply(Move move)
Attempts to add apply the given move to the board. Produces the same results as place().


clearRows

public boolean clearRows()
Deletes rows that are filled all the way across, moving things above down. Returns true if any row clearing happened.


commit

public void commit()
Puts the board in the committed state. See the overview docs.


equals

public boolean equals(Object other)
Returns true if two boards are the same, meaning they have blocks in exactly the same locations.


getBlocksInRow

public int getBlocksInRow(int y)
Returns the number of filled blocks in the given row.


getColumnHeight

public int getColumnHeight(int x)
Returns the height of the given column, which is the y value of the highest block + 1. The height is 0 if the column contains no blocks.


getHeight

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


getLargestHeight

public int getLargestHeight()
Returns the max column height present in the board. For an empty board this is 0.


getWidth

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


hasBlockAt

public final boolean hasBlockAt(int x,
                                int y)
Returns true if the given block is filled in the board. Blocks outside of the valid width/height area always return true.


place

public int place(Piece piece,
                 int x,
                 int y)
Attempts to add the body of a piece to the board. Copies the piece blocks into the board grid. Returns PLACE_OK for a regular placement, or PLACE_ROW_FILLED for a regular placement that causes at least one row to be filled.

Error cases:
If part of the piece would fall out of bounds, the placement does not change the board at all, and PLACE_OUT_BOUNDS is returned. If the placement is "bad"--interfering with existing blocks in the grid--then the placement is halted partially complete and PLACE_BAD is returned. An undo() will remove the bad placement.


rowAfterDrop

public int rowAfterDrop(Piece piece,
                        int x)
Given a piece and an x, returns the y value where the piece would come to rest if it were dropped straight down at that x.


sanityCheck

public void sanityCheck()
Checks the board for internal consistency -- used for debugging.


toString

public String toString()
Returns a human-readable string representation of this board. Note that this produces a multi-line representation. If many rows near the top of the board are empty, they are omitted for brevity. Instead, only rows up through getLargestHeight() are printed, including the lowest blank row just before the highest block on the board.

Returns:
the printable representation of this board

undo

public void undo()
If a place() happens, optionally followed by a clearRows(), a subsequent undo() reverts the board to its state before the place(). If the conditions for undo() are not met, such as calling undo() twice in a row, then the second undo() does nothing. See the overview docs.