Package sac.model

Class Board

java.lang.Object
sac.model.Board

public class Board extends Object
Represents the playground of Tetris.
  • Field Details

    • width

      public final int width
      The width of the board.
    • height

      public final int height
      The 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

      public boolean isValidPoint(Point point)
    • placePiece

      public Board.PlacePieceStatus placePiece(Piece piece, Point position)
      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

      public Point dropPosition(Piece piece, Point position)
      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

      public Piece.PieceType getGrid(Point pos)
      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

      public String toString()
      Generates the string representation of the board.
      Overrides:
      toString in class Object
      Returns:
      A string representation of the board.