Package sac.model
Class Board
java.lang.Object
sac.model.Board
Represents the playground of Tetris.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe return status for PlacePiece -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbackup()Backup the grid.intClear all the rows in the board that is filled.voidcommit()Commit the change.dropPosition(Piece piece, Point position) Calculate the expected position of `piece` would be placed.Check what type of piece occupies the position.booleanisValidPoint(Point point) placePiece(Piece piece, Point position) Place a piece at a specific position.toString()Generates the string representation of the board.voidundo()Restore the grid before the backup.
-
Field Details
-
width
public final int widthThe width of the board. -
height
public final int heightThe height of the board.
-
-
Constructor Details
-
Board
public Board(int width, int height) Initializes a new Board object.- Parameters:
width- The width of the board.height- The height of the board.
-
-
Method Details
-
isValidPoint
-
placePiece
Place a piece at a specific position.- Parameters:
piece- The piece to be placed.position- The coordinate of the bottom-left corner of the piece.- Returns:
- ADD_OUT_BOUNDS when the placement is out of bound, ADD_BAD when the piece is placed on some position that is already occupied, ADD_ROW_FILLED when arbitrary row is filled after the piece is placed, ADD_OK in all the other cases.
-
clearRows
public int clearRows()Clear all the rows in the board that is filled.- Returns:
- The number of rows cleared.
-
dropPosition
Calculate the expected position of `piece` would be placed.- Parameters:
piece-position- The coordinate of the bottom-left corner of the piece.- Returns:
- the expected position of the bottom-left corner of the piece.
-
backup
public void backup()Backup the grid.This function should be only called by the client.
-
undo
public void undo()Restore the grid before the backup.This function should be only called by the client.
-
commit
public void commit()Commit the change. -
getGrid
Check what type of piece occupies the position.- Parameters:
pos- The position to be checked.- Returns:
- The type of piece that occupies the position. null if no piece occupies such position.
-
toString
Generates the string representation of the board.
-