Class ChessboardService

java.lang.Object
com.chess.backend.services.ChessboardService

@Component public class ChessboardService extends Object
Stateless Chessboard Service to initialize new Chessboard and does operations on Chessboard object
  • Constructor Details

    • ChessboardService

      public ChessboardService()
  • Method Details

    • initNewGameBoard

      public static Chessboard initNewGameBoard(List<Player> players)
      create new Chessboard from Players object
      Parameters:
      players - Array of Player object
      Returns:
      Chessboard object
    • setPiece

      public static void setPiece(int posX, int posY, ArrayList<ArrayList<Square>> squares, Piece piece)
      private function that takes two positions, ie x,y and set piece in Square[x][y]
      Parameters:
      posX - position x
      posY - position y
      squares - 2D array of Square object
      piece - Piece object
    • removePiece

      public static void removePiece(int posX, int posY, ArrayList<ArrayList<Square>> squares)
      private function that takes two positions, ie x,y and set piece in Square[x][y]
      Parameters:
      posX - position x
      posY - position y
      squares - 2D array of Square object
    • searchSquaresByPiece

      public static ArrayList<Square> searchSquaresByPiece(ArrayList<ArrayList<Square>> squares, PieceType pieceType, Color color, Player player)
      filter squares according to piece type, color or player
      Parameters:
      squares - 2D Square Array
      pieceType - PieceType object
      color - Color object
      player - Player object
      Returns:
      ArrayList of Square object
    • getOccupiedSquares

      public static ArrayList<Square> getOccupiedSquares(ArrayList<ArrayList<Square>> squares)
      return list of Squares with pieces on it
      Parameters:
      squares - 2D Array of Square object
      Returns:
      ArrayList of Square object
    • getMaxY

      public static int getMaxY(ArrayList<ArrayList<Square>> squares)
    • getMaxX

      public static int getMaxX(ArrayList<ArrayList<Square>> squares)
    • getBottom

      public static int getBottom(ArrayList<ArrayList<Square>> squares)
    • getTop

      public static int getTop(ArrayList<ArrayList<Square>> squares)
      Replaces Chessboard.top field
      Parameters:
      squares -
      Returns:
      get
    • move

      public static ArrayList<EventObject> move(Chessboard chessboard, int fromX, int fromY, int toX, int toY)
      Apply move to chessboard.
      Parameters:
      chessboard - The chessboard.
      fromX - From position x.
      fromY - From position Y.
      toX - To position X.
      toY - To position Y.
      Returns:
      List of performed events.
    • getSquare

      public static Square getSquare(Chessboard chessboard, Position position)
      gets Position object and Chessboard and returns the matched Square object to this position.
      Parameters:
      chessboard - Chessboard object
      position - Position object
      Returns:
      matched Square object
    • initClean

      public static void initClean(Chessboard chessboard)
    • initClean

      public static void initClean(ArrayList<ArrayList<Square>> squares)
    • removeOtherPieceTypes

      public static void removeOtherPieceTypes(Chessboard chessboard, PieceType pieceType)
    • getPieceByPosition

      public static Piece getPieceByPosition(Chessboard chessboard, int x, int y)
    • getPieceByPosition

      public static Piece getPieceByPosition(Chessboard chessboard, Position position)
    • setCommonPiecePlayer

      public static void setCommonPiecePlayer(Chessboard chessboard, PieceType pieceType, Player player)
      Sets one common player for all pieces of a given pieceType (e.g. cannon)
      Parameters:
      chessboard - The chessboard context
      pieceType - PieceType on which the common player should be applied
      player - Player that should be applied on all pieces of given pieceType.
    • getValidMovesForPiece

      public static ArrayList<Square> getValidMovesForPiece(Chessboard chessboard, Piece piece, Player player)
      Gets all valid moves for a piece. Valid means that the move is possible for the piece and doesn't end up in a check for the piece owner.
      Parameters:
      chessboard - The chessboard.
      piece - The piece for which the moves are to be determined.
      Returns:
      A list of valid moves.
    • isCheck

      public static boolean isCheck(Chessboard chessboard, Player player)
      Checks if a king of a given player can be captured be an enemy piece.
      Parameters:
      chessboard - The chessboard.
      player - The player to be examined.
      Returns:
      True if the player's king can be captured and false if not.
    • hasPlayerValidMoves

      public static boolean hasPlayerValidMoves(Chessboard chessboard, Player player)
      Check if a player can perform minimum one valid move.
      Parameters:
      chessboard - The chessboard.
      player - The player who should be checked.
      Returns:
      True if the player has minimum one valid move and false if not.
    • getCaptureKingPlayers

      public static ArrayList<Player> getCaptureKingPlayers(Chessboard chessboard, Player player)
      Get all players whose kings can be captured by a given player.
      Parameters:
      chessboard - The chessboard.
      player - The given player.
      Returns:
      A list of players whose kings can be captured by the player.