Class Board

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

public class Board extends Object implements Cloneable
This class represents the board of the Tak game. This class includes some utility methods to compute neighbors positions, check if a piece can be put, and move of a Piece.
Author:
Belegkarnil
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Define an amount used to express that is invalid (i.e.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a Board.Size.HUGE Board
    Board(Board board)
    Construct a copy of the current board
    Construct a board of the given size
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canMove(Color player, int srcRow, int srcColumn, int[] amount, int dstRow, int dstColumn)
    Know if a player can make the given move
    boolean
    canMove(Color player, Point src, int[] amount, Point dst)
    Know if a player can make the given move
    boolean
    canPlace(Piece piece, int row, int column)
    Know if a Piece can be placed at a given position on the board
    boolean
    canPlace(Piece piece, Point point)
    Know if a Piece can be placed at a given position on the board
    Clone (see Cloneable) the current board
    protected static final int
    computeAmount(int[] amount)
    Compute total amount of a move action (path)
    int
    Count how much cells are free
    int
    Count the initial capstones (depending on the board size) that a player has when the game starts
    int
    Count the initial stones (depending on the board size) that a player has when the game starts
    int
    Count how much times a type of Piece (color and type) is on the board
    int
    Get the load limit of a stack defined in the rules (i.e.
    Get neighbors positions of a cell in the board
    int
    Get the board size
    getStack(int row, int col)
    Get the stack of Pieces at a given position (bottom is at position 0, top at stack length -1)
    getStack(Point position)
    Get the stack of Pieces at a given position (bottom is at position 0, top at stack length -1)
    getTop(int row, int col)
    Get the Piece (at top of a stack) at a given position
    getTop(Point position)
    Get the Piece (at top of a stack) at a given position
    boolean
    inBounds(int row, int column)
    Know if the position is on the board
    boolean
    inBounds(Point point)
    Know if the position is on the board
    boolean
    Know if the board is completed (no cells are free)
    boolean
    isFree(int row, int column)
    Know if the position is free on the board
    boolean
    isFree(Point point)
    Know if the position is free on the board
    static boolean
    isStraightPath(int srcRow, int srcColumn, int dstRow, int dstColumn)
    Know if exists a straight path (horizontal or vertical) between two positions
    static boolean
    Know if exists a straight path (horizontal or vertical) between two positions
    boolean
    isUnderControl(Color player, int row, int column)
    Know if the position is under the control of a player
    boolean
    isUnderControl(Color player, Point point)
    Know if the position is under the control of a player
    protected Color
    move(Point src, int[] amount, Point dst)
    Move some Pieces on the board according to the given path
    boolean
    pathExists(int row, int column, Color color)
    Know if a path exists to link left to right or top to down
    boolean
    pathExists(Point point, Color color)
    Know if a path exists to link left to right or top to down
    protected Color
    place(Piece piece, Point point)
    Place a Piece on the board at a given position
    protected void
    Reset the board, remove all pieces

    Methods inherited from class java.lang.Object

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

    • INVALID_AMOUNT

      public static final int INVALID_AMOUNT
      Define an amount used to express that is invalid (i.e. 0)
      See Also:
  • Constructor Details

    • Board

      public Board()
      Construct a Board.Size.HUGE Board
    • Board

      public Board(Board.Size size)
      Construct a board of the given size
      Parameters:
      size - the board Board.Size
    • Board

      public Board(Board board)
      Construct a copy of the current board
      Parameters:
      board - The board to copy
  • Method Details

    • reset

      protected void reset()
      Reset the board, remove all pieces
    • inBounds

      public boolean inBounds(Point point)
      Know if the position is on the board
      Parameters:
      point - The position to check if it is on the board
      Returns:
      true iff the position is on the board, false otherwise
    • inBounds

      public boolean inBounds(int row, int column)
      Know if the position is on the board
      Parameters:
      row - The y coordinate of position to check if it is on the board
      column - The x coordinate of position to check if it is on the board
      Returns:
      true iff the position is on the board, false otherwise
    • isFree

      public boolean isFree(Point point)
      Know if the position is free on the board
      Parameters:
      point - The position to check if it is free on the board
      Returns:
      true iff the position is free on the board, false otherwise
    • isFree

      public boolean isFree(int row, int column)
      Know if the position is free on the board
      Parameters:
      row - The y coordinate of position to check if it is free on the board
      column - The x coordinate of position to check if it is free on the board
      Returns:
      true iff the position is free on the board, false otherwise
    • isUnderControl

      public boolean isUnderControl(Color player, Point point)
      Know if the position is under the control of a player
      Parameters:
      player - The color of the player to check
      point - The position to check
      Returns:
      true iff the position is under the control of the player, false otherwise
    • isUnderControl

      public boolean isUnderControl(Color player, int row, int column)
      Know if the position is under the control of a player
      Parameters:
      player - The color of the player to check
      row - The y coordinate of position to check
      column - The x coordinate of position to check
      Returns:
      true iff the position is under the control of the player, false otherwise
    • canPlace

      public boolean canPlace(Piece piece, Point point)
      Know if a Piece can be placed at a given position on the board
      Parameters:
      piece - The Piece to place
      point - The position to check
      Returns:
      true iff the piece can be placed, false otherwise
    • canPlace

      public boolean canPlace(Piece piece, int row, int column)
      Know if a Piece can be placed at a given position on the board
      Parameters:
      piece - The Piece to place
      row - The y coordinate of position to check
      column - The x coordinate of position to check
      Returns:
      true iff the piece can be placed, false otherwise
    • isCompleted

      public boolean isCompleted()
      Know if the board is completed (no cells are free)
      Returns:
      true iff countEmpty() == 0, false otherwise
    • countEmpty

      public int countEmpty()
      Count how much cells are free
      Returns:
      Amount of free (empty) cells of the board
    • place

      protected Color place(Piece piece, Point point)
      Place a Piece on the board at a given position
      Parameters:
      piece - the Piece to place
      point - the board position to place the piece
      Returns:
      Color of the player (see Constants) that completed a path, null otherwise
    • getNeighbors

      public List<Point> getNeighbors(Point cell)
      Get neighbors positions of a cell in the board
      Parameters:
      cell - A position on the board
      Returns:
      The list of existing (i.e. inBounds(.)) positions
    • pathExists

      public boolean pathExists(Point point, Color color)
      Know if a path exists to link left to right or top to down
      Parameters:
      point - A point on the path (i.e. new Piece or Piece moved)
      color - The color of the Player that attempted to create a path
      Returns:
      true iff a path exists from point with the given color, false otherwise
    • pathExists

      public boolean pathExists(int row, int column, Color color)
      Know if a path exists to link left to right or top to down
      Parameters:
      row - is the y coordinate of a point on the path (i.e. new Piece or Piece moved)
      column - is the x coordinate of a point on the path (i.e. new Piece or Piece moved)
      color - The color of the Player that attempted to create a path
      Returns:
      true iff a path exists from point with the given color, false otherwise
    • canMove

      public boolean canMove(Color player, Point src, int[] amount, Point dst)
      Know if a player can make the given move
      Parameters:
      player - The Player's Color (see Constants)
      src - The initial position of the path
      amount - The amount of Pieces taken from initial position and placed along the path
      dst - The final position of the path
      Returns:
      true iff the move is valid, false otherwise
    • computeAmount

      protected static final int computeAmount(int[] amount)
      Compute total amount of a move action (path)
      Parameters:
      amount - The amount array of a move action
      Returns:
      total amount iff move seems valid (i.e. each amount > 0 and amount.length > 0), false otherwise
    • canMove

      public boolean canMove(Color player, int srcRow, int srcColumn, int[] amount, int dstRow, int dstColumn)
      Know if a player can make the given move
      Parameters:
      player - The Player's Color (see Constants)
      srcRow - is the y coordinate of the initial position of the path
      srcColumn - is the x coordinate of the initial position of the path
      amount - The amount of Pieces taken from initial position and placed along the path
      dstRow - is the y coordinate of the final position of the path
      dstColumn - is the x coordinate of the final position of the path
      Returns:
      true iff the move is valid, false otherwise
    • isStraightPath

      public static boolean isStraightPath(int srcRow, int srcColumn, int dstRow, int dstColumn)
      Know if exists a straight path (horizontal or vertical) between two positions
      Parameters:
      srcRow - is the y coordinate of the initial position of the path
      srcColumn - is the x coordinate of the initial position of the path
      dstRow - is the y coordinate of the final position of the path
      dstColumn - is the x coordinate of the final position of the path
      Returns:
      true iff the path is going straight, false otherwise
    • isStraightPath

      public static boolean isStraightPath(Point src, Point dst)
      Know if exists a straight path (horizontal or vertical) between two positions
      Parameters:
      src - The initial position of the path
      dst - The final position of the path
      Returns:
      true iff the path is going straight, false otherwise
    • move

      protected Color move(Point src, int[] amount, Point dst)
      Move some Pieces on the board according to the given path
      Parameters:
      src - The initial position of the path
      amount - The amount of Pieces taken from initial position and placed along the path
      dst - The final position of the path
      Returns:
      Color of the player (see Constants) that completed a path, null otherwise
    • getLoadLimit

      public int getLoadLimit()
      Get the load limit of a stack defined in the rules (i.e. board size)
      Returns:
      The maximum load of each stack
    • getSize

      public int getSize()
      Get the board size
      Returns:
      The board size
    • countPieces

      public int countPieces(Piece piece)
      Count how much times a type of Piece (color and type) is on the board
      Parameters:
      piece - A Piece (color and type) that defines the type
      Returns:
      the counter
    • getStack

      public Piece[] getStack(int row, int col)
      Get the stack of Pieces at a given position (bottom is at position 0, top at stack length -1)
      Parameters:
      row - The y coordinate of the position to fetch the stack
      col - The x coordinate of the position to fetch the stack
      Returns:
      A copy of Pieces that are at the given position
    • getStack

      public Piece[] getStack(Point position)
      Get the stack of Pieces at a given position (bottom is at position 0, top at stack length -1)
      Parameters:
      position - The position to fetch the stack
      Returns:
      A copy of Pieces that are at the given position
    • getTop

      public Piece getTop(Point position)
      Get the Piece (at top of a stack) at a given position
      Parameters:
      position - The position to fetch the stack
      Returns:
      A copy of the Piece that is at the given position
    • getTop

      public Piece getTop(int row, int col)
      Get the Piece (at top of a stack) at a given position
      Parameters:
      row - The y coordinate of the position to fetch the stack
      col - The x coordinate of the position to fetch the stack
      Returns:
      A copy of the Piece that is at the given position
    • countInitialCapstones

      public int countInitialCapstones()
      Count the initial capstones (depending on the board size) that a player has when the game starts
      Returns:
      the number of capstones (see Piece)
    • countInitialStones

      public int countInitialStones()
      Count the initial stones (depending on the board size) that a player has when the game starts
      Returns:
      the number of stones (see Piece)
    • clone

      public Board clone()
      Clone (see Cloneable) the current board
      Overrides:
      clone in class Object
      Returns:
      A copy of the current boar