Class Board
java.lang.Object
be.belegkarnil.game.board.spectrangle.Board
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionprotected final BagThe bag linked to the board initialized during constructionstatic final intAn array index to specify the position that contains the base piece color (always base at bottom)static final intThe default reference board sizestatic final intAn array index to specify the position that contains the left piece color (always base at bottom)static final intThe default factor/multiple which means there are no bonusstatic final intThe initial value of positions when there are not yet a piecestatic final intAn array index to specify the position that contains the right piece color (always base at bottom) -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionColor[]applyRotate(Piece piece, int rotation) Compute the rotation of aPiece, always consider the triangle base at bottombooleanKnow if aPiecewith a given rotation can be put at a given positionclone()Clone the current board (use clone on the bag, seeBag.clone())intcountColumns(int row) Count the number of column that the board has at a given rowintCount the number of rows that the board hasgetBag()getBaseColor(int x, int y) Get the base color (always consider base triangle at bottom) at a positiongetBaseColor(Point position) Get the base color (always consider base triangle at bottom) at a positionfinal PointgetBaseNeighbour(Point position) Get the base position of a given positionfinal intgetBaseValue(Point position) Get the piece value at the base neighbor of a given positionintgetFactor(int x, int y) Get the factor/multiplier value at a given positionintGet the factor/multiplier value at a given positiongetLeftColor(int x, int y) Get the left color (always consider base triangle at bottom) at a positiongetLeftColor(Point position) Get the left color (always consider base triangle at bottom) at a positionfinal PointgetLeftNeighbour(Point position) Get the left position of a given positionfinal intgetLeftValue(Point position) Get the piece value at the left neighbor of a given positiongetNeighbours(Point position) Get the neighbors position of a positiongetRightColor(int x, int y) Get the right color (always consider base triangle at bottom) at a positiongetRightColor(Point position) Get the right color (always consider base triangle at bottom) at a positionfinal PointgetRightNeighbour(Point position) Get the right position of a given positionfinal intgetRightValue(Point position) Get the piece value at the right neighbor of a given positionintgetSize()Get the reference size of this boardintgetValue(int x, int y) Get value at a given positionintGet value at a given positionbooleaninBounds(int x, int y) Know if a position is contained in the bounds of the boardbooleanKnow if a position is contained in the bounds of the boardbooleanisBonus(int x, int y) Know if the position provide a bonus (i.e.booleanKnow if the position provide a bonus (i.e.booleanKnow if the board has not yet aPieceon itbooleanisFree(int x, int y) Know if a position is free (i.e.booleanKnow if a position is free (i.e.protected intPut a rotatedPieceat a given positionprotected voidreset()Reset the board (i.e.
-
Field Details
-
BASE_COLOR
public static final int BASE_COLORAn 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_COLORAn 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_COLORAn 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_BONUSThe default factor/multiple which means there are no bonus- See Also:
-
NO_VALUE
public static final int NO_VALUEThe initial value of positions when there are not yet a piece- See Also:
-
DEFAULT_SIZE
public static final int DEFAULT_SIZEThe default reference board size- See Also:
-
bag
The bag linked to the board initialized during construction
-
-
Constructor Details
-
Board
public Board()Construct a board with a reference size ofDEFAULT_SIZE -
Board
public Board(int size) Construct a board of the given size- Parameters:
size- the board size (i.e. reference size)
-
Board
Constructor that clones the board (seeclone())- 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) positiony- the y-axis (row) position- Returns:
- true iff the position is on the board
-
inBounds
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 noPieceat that position)- Parameters:
x- the x-axis (column) positiony- the y-axis (row) position- Returns:
- true iff the position is free
-
isFree
Know if a position is free (i.e. there are noPieceat that position)- Parameters:
position- the position on the board- Returns:
- true iff the position is free
-
isBonus
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) positiony- the y-axis (row) position- Returns:
- true iff the position is a bonus position on the board
-
canPlace
Know if aPiecewith a given rotation can be put at a given position- Parameters:
piece- The piece to placeposition- The position on the boardrotation- The rotation to apply on the piece- Returns:
- true iff the rotated piece can be put at the given position
-
applyRotate
Compute the rotation of aPiece, always consider the triangle base at bottom- Parameters:
piece- The piece to apply the rotationrotation- 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, andBASE_COLOR)
-
place
Put a rotatedPieceat a given position- Parameters:
piece- The piece to put on the boardposition- The position at which to put the piecerotation- The rotation to apply- Returns:
- the increment of score but positioning the piece (i.e. factor * max(1,number of adjacents) * piece value)
-
getNeighbours
Get the neighbors position of a position- Parameters:
position- the position on the board- Returns:
- A
Listof Point that represents valid (i.e. seeinBounds(Point)) position adjacent to the given position
-
getLeftNeighbour
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
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
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
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)), otherwiseNO_VALUE
-
getRightValue
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)), otherwiseNO_VALUE
-
getBaseValue
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)), otherwiseNO_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) positiony- the y-axis (row) position- Returns:
- The factor (multiplier of the board) value iff exists, otherwise
NO_BONUS
-
getFactor
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) positiony- the y-axis (row) position- Returns:
- The piece value iff exists, otherwise
NO_VALUE
-
getValue
Get value at a given position- Parameters:
position- the position on the board- Returns:
- The piece value iff exists, otherwise
NO_VALUE
-
getBaseColor
Get the base color (always consider base triangle at bottom) at a position- Parameters:
x- the x-axis (column) positiony- the y-axis (row) position- Returns:
- The base/bottom color
-
getLeftColor
Get the left color (always consider base triangle at bottom) at a position- Parameters:
x- the x-axis (column) positiony- the y-axis (row) position- Returns:
- The left color
-
getRightColor
Get the right color (always consider base triangle at bottom) at a position- Parameters:
x- the x-axis (column) positiony- the y-axis (row) position- Returns:
- The right color
-
getBaseColor
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
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
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
- Returns:
- the bag
-
isFirstMove
public boolean isFirstMove()Know if the board has not yet aPieceon it -
clone
Clone the current board (use clone on the bag, seeBag.clone())- Overrides:
clonein classObject- Returns:
- a clone of the board state
- Throws:
CloneNotSupportedException
-