java.lang.Object
be.belegkarnil.game.board.spectrangle.Board
All Implemented Interfaces:
Cloneable

public class Board extends Object implements Cloneable
This class represents the board of the Spectrangle game. It includes factor/multiple, the Bag, the piece (colors) that are already on the board. This class includes some utility methods to compute neighbors positions, check if a piece can be put, and rotation of a piece.
Author:
Belegkarnil
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Bag
    The bag linked to the board initialized during construction
    static final int
    An array index to specify the position that contains the base piece color (always base at bottom)
    static final int
    The default reference board size
    static final int
    An array index to specify the position that contains the left piece color (always base at bottom)
    static final int
    The default factor/multiple which means there are no bonus
    static final int
    The initial value of positions when there are not yet a piece
    static final int
    An array index to specify the position that contains the right piece color (always base at bottom)
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    Construct a board with a reference size of DEFAULT_SIZE
     
    Board(int size)
    Construct a board of the given size
    protected
    Board(Board board)
    Constructor that clones the board (see clone())
  • Method Summary

    Modifier and Type
    Method
    Description
    applyRotate(Piece piece, int rotation)
    Compute the rotation of a Piece, always consider the triangle base at bottom
    boolean
    canPlace(Piece piece, Point position, int rotation)
    Know if a Piece with a given rotation can be put at a given position
    Clone the current board (use clone on the bag, see Bag.clone())
    int
    countColumns(int row)
    Count the number of column that the board has at a given row
    int
    Count the number of rows that the board has
    Get the current Bag linked to the board and used by a Game
    getBaseColor(int x, int y)
    Get the base color (always consider base triangle at bottom) at a position
    getBaseColor(Point position)
    Get the base color (always consider base triangle at bottom) at a position
    final Point
    Get the base position of a given position
    final int
    getBaseValue(Point position)
    Get the piece value at the base neighbor of a given position
    int
    getFactor(int x, int y)
    Get the factor/multiplier value at a given position
    int
    getFactor(Point position)
    Get the factor/multiplier value at a given position
    getLeftColor(int x, int y)
    Get the left color (always consider base triangle at bottom) at a position
    getLeftColor(Point position)
    Get the left color (always consider base triangle at bottom) at a position
    final Point
    Get the left position of a given position
    final int
    getLeftValue(Point position)
    Get the piece value at the left neighbor of a given position
    getNeighbours(Point position)
    Get the neighbors position of a position
    getRightColor(int x, int y)
    Get the right color (always consider base triangle at bottom) at a position
    getRightColor(Point position)
    Get the right color (always consider base triangle at bottom) at a position
    final Point
    Get the right position of a given position
    final int
    getRightValue(Point position)
    Get the piece value at the right neighbor of a given position
    int
    Get the reference size of this board
    int
    getValue(int x, int y)
    Get value at a given position
    int
    getValue(Point position)
    Get value at a given position
    boolean
    inBounds(int x, int y)
    Know if a position is contained in the bounds of the board
    boolean
    inBounds(Point position)
    Know if a position is contained in the bounds of the board
    boolean
    isBonus(int x, int y)
    Know if the position provide a bonus (i.e.
    boolean
    isBonus(Point position)
    Know if the position provide a bonus (i.e.
    boolean
    Know if the board has not yet a Piece on it
    boolean
    isFree(int x, int y)
    Know if a position is free (i.e.
    boolean
    isFree(Point position)
    Know if a position is free (i.e.
    protected int
    place(Piece piece, Point position, int rotation)
    Put a rotated Piece at a given position
    protected void
    Reset the board (i.e.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BASE_COLOR

      public static final int BASE_COLOR
      An array index to specify the position that contains the base piece color (always base at bottom)
      See Also:
    • LEFT_COLOR

      public static final int LEFT_COLOR
      An array index to specify the position that contains the left piece color (always base at bottom)
      See Also:
    • RIGHT_COLOR

      public static final int RIGHT_COLOR
      An array index to specify the position that contains the right piece color (always base at bottom)
      See Also:
    • NO_BONUS

      public static final int NO_BONUS
      The default factor/multiple which means there are no bonus
      See Also:
    • NO_VALUE

      public static final int NO_VALUE
      The initial value of positions when there are not yet a piece
      See Also:
    • DEFAULT_SIZE

      public static final int DEFAULT_SIZE
      The default reference board size
      See Also:
    • bag

      protected final Bag bag
      The bag linked to the board initialized during construction
  • Constructor Details

    • Board

      public Board()
      Construct a board with a reference size of DEFAULT_SIZE
    • Board

      public Board(int size)
      Construct a board of the given size
      Parameters:
      size - the board size (i.e. reference size)
    • Board

      protected Board(Board board) throws CloneNotSupportedException
      Constructor that clones the board (see clone())
      Parameters:
      board - The board to clone
      Throws:
      CloneNotSupportedException
  • Method Details

    • reset

      protected void reset()
      Reset the board (i.e. Piece values, factors/multipliers, bag, and reset the state of the first move)
    • getSize

      public int getSize()
      Get the reference size of this board
      Returns:
      the reference size
    • inBounds

      public boolean inBounds(int x, int y)
      Know if a position is contained in the bounds of the board
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      true iff the position is on the board
    • inBounds

      public boolean inBounds(Point position)
      Know if a position is contained in the bounds of the board
      Parameters:
      position - the position on the board
      Returns:
      true iff the position is on the board
    • isFree

      public boolean isFree(int x, int y)
      Know if a position is free (i.e. there are no Piece at that position)
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      true iff the position is free
    • isFree

      public boolean isFree(Point position)
      Know if a position is free (i.e. there are no Piece at that position)
      Parameters:
      position - the position on the board
      Returns:
      true iff the position is free
    • isBonus

      public boolean isBonus(Point position)
      Know if the position provide a bonus (i.e. factor/multiple)
      Parameters:
      position - the position on the board
      Returns:
      true iff the position is a bonus position on the board
    • isBonus

      public boolean isBonus(int x, int y)
      Know if the position provide a bonus (i.e. factor/multiple)
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      true iff the position is a bonus position on the board
    • canPlace

      public boolean canPlace(Piece piece, Point position, int rotation)
      Know if a Piece with a given rotation can be put at a given position
      Parameters:
      piece - The piece to place
      position - The position on the board
      rotation - The rotation to apply on the piece
      Returns:
      true iff the rotated piece can be put at the given position
    • applyRotate

      public Color[] applyRotate(Piece piece, int rotation)
      Compute the rotation of a Piece, always consider the triangle base at bottom
      Parameters:
      piece - The piece to apply the rotation
      rotation - The rotation is an integer: 0 means no rotation, 1 for a single clockwise rotation, and 2 for two clockwise rotations
      Returns:
      an array of three Color (see indices: LEFT_COLOR, RIGHT_COLOR, and BASE_COLOR)
    • place

      protected int place(Piece piece, Point position, int rotation)
      Put a rotated Piece at a given position
      Parameters:
      piece - The piece to put on the board
      position - The position at which to put the piece
      rotation - The rotation to apply
      Returns:
      the increment of score but positioning the piece (i.e. factor * max(1,number of adjacents) * piece value)
    • getNeighbours

      public List<Point> getNeighbours(Point position)
      Get the neighbors position of a position
      Parameters:
      position - the position on the board
      Returns:
      A List of Point that represents valid (i.e. see inBounds(Point)) position adjacent to the given position
    • getLeftNeighbour

      public final Point getLeftNeighbour(Point position)
      Get the left position of a given position
      Parameters:
      position - the position on the board
      Returns:
      A Point on the left of the given position (Note: can be an invalid position)
    • getRightNeighbour

      public final Point getRightNeighbour(Point position)
      Get the right position of a given position
      Parameters:
      position - the position on the board
      Returns:
      A Point on the right of the given position (Note: can be an invalid position)
    • getBaseNeighbour

      public final Point getBaseNeighbour(Point position)
      Get the base position of a given position
      Parameters:
      position - the position on the board
      Returns:
      A Point on the base of the given position (Note: can be an invalid position)
    • getLeftValue

      public final int getLeftValue(Point position)
      Get the piece value at the left neighbor of a given position
      Parameters:
      position - the position on the board
      Returns:
      The piece value of the base neighbor iff exists (see getBaseNeighbour(Point)), otherwise NO_VALUE
    • getRightValue

      public final int getRightValue(Point position)
      Get the piece value at the right neighbor of a given position
      Parameters:
      position - the position on the board
      Returns:
      The piece value of the base neighbor iff exists (see getBaseNeighbour(Point)), otherwise NO_VALUE
    • getBaseValue

      public final int getBaseValue(Point position)
      Get the piece value at the base neighbor of a given position
      Parameters:
      position - the position on the board
      Returns:
      The piece value of the base neighbor iff exists (see getBaseNeighbour(Point)), otherwise NO_VALUE
    • countRows

      public int countRows()
      Count the number of rows that the board has
      Returns:
      the number of rows
    • countColumns

      public int countColumns(int row)
      Count the number of column that the board has at a given row
      Parameters:
      row - the row at which to count the number of columns
      Returns:
      the number of columns
    • getFactor

      public int getFactor(int x, int y)
      Get the factor/multiplier value at a given position
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      The factor (multiplier of the board) value iff exists, otherwise NO_BONUS
    • getFactor

      public int getFactor(Point position)
      Get the factor/multiplier value at a given position
      Parameters:
      position - the position on the board
      Returns:
      The factor (multiplier of the board) value iff exists, otherwise NO_BONUS
    • getValue

      public int getValue(int x, int y)
      Get value at a given position
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      The piece value iff exists, otherwise NO_VALUE
    • getValue

      public int getValue(Point position)
      Get value at a given position
      Parameters:
      position - the position on the board
      Returns:
      The piece value iff exists, otherwise NO_VALUE
    • getBaseColor

      public Color getBaseColor(int x, int y)
      Get the base color (always consider base triangle at bottom) at a position
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      The base/bottom color
    • getLeftColor

      public Color getLeftColor(int x, int y)
      Get the left color (always consider base triangle at bottom) at a position
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      The left color
    • getRightColor

      public Color getRightColor(int x, int y)
      Get the right color (always consider base triangle at bottom) at a position
      Parameters:
      x - the x-axis (column) position
      y - the y-axis (row) position
      Returns:
      The right color
    • getBaseColor

      public Color getBaseColor(Point position)
      Get the base color (always consider base triangle at bottom) at a position
      Parameters:
      position - the position on the board
      Returns:
      The base/bottom color
    • getLeftColor

      public Color getLeftColor(Point position)
      Get the left color (always consider base triangle at bottom) at a position
      Parameters:
      position - the position on the board
      Returns:
      The left color
    • getRightColor

      public Color getRightColor(Point position)
      Get the right color (always consider base triangle at bottom) at a position
      Parameters:
      position - the position on the board
      Returns:
      The right color
    • getBag

      public Bag getBag()
      Get the current Bag linked to the board and used by a Game
      Returns:
      the bag
    • isFirstMove

      public boolean isFirstMove()
      Know if the board has not yet a Piece on it
      Returns:
      true iff there are no Player that has already put a Piece
    • clone

      public Object clone() throws CloneNotSupportedException
      Clone the current board (use clone on the bag, see Bag.clone())
      Overrides:
      clone in class Object
      Returns:
      a clone of the board state
      Throws:
      CloneNotSupportedException