Package com.chess.backend.services
Class ChessboardService
java.lang.Object
com.chess.backend.services.ChessboardService
Stateless Chessboard Service to initialize new Chessboard
and does operations on Chessboard object
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
getCaptureKingPlayers
(Chessboard chessboard, Player player) Get all players whose kings can be captured by a given player.static int
static int
getOccupiedSquares
(ArrayList<ArrayList<Square>> squares) return list of Squares with pieces on itstatic Piece
getPieceByPosition
(Chessboard chessboard, int x, int y) static Piece
getPieceByPosition
(Chessboard chessboard, Position position) static Square
getSquare
(Chessboard chessboard, Position position) gets Position object and Chessboard and returns the matched Square object to this position.static int
Replaces Chessboard.top fieldgetValidMovesForPiece
(Chessboard chessboard, Piece piece, Player player) Gets all valid moves for a piece.static boolean
hasPlayerValidMoves
(Chessboard chessboard, Player player) Check if a player can perform minimum one valid move.static void
initClean
(Chessboard chessboard) static void
static Chessboard
initNewGameBoard
(List<Player> players) create new Chessboard from Players objectstatic boolean
isCheck
(Chessboard chessboard, Player player) Checks if a king of a given player can be captured be an enemy piece.static ArrayList<EventObject>
move
(Chessboard chessboard, int fromX, int fromY, int toX, int toY) Apply move to chessboard.static void
removeOtherPieceTypes
(Chessboard chessboard, PieceType pieceType) 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]searchSquaresByPiece
(ArrayList<ArrayList<Square>> squares, PieceType pieceType, Color color, Player player) filter squares according to piece type, color or playerstatic void
setCommonPiecePlayer
(Chessboard chessboard, PieceType pieceType, Player player) Sets one common player for all pieces of a given pieceType (e.g.static void
private function that takes two positions, ie x,y and set piece in Square[x][y]
-
Constructor Details
-
ChessboardService
public ChessboardService()
-
-
Method Details
-
initNewGameBoard
create new Chessboard from Players object- Parameters:
players
- Array of Player object- Returns:
- Chessboard object
-
setPiece
private function that takes two positions, ie x,y and set piece in Square[x][y]- Parameters:
posX
- position xposY
- position ysquares
- 2D array of Square objectpiece
- Piece object
-
removePiece
private function that takes two positions, ie x,y and set piece in Square[x][y]- Parameters:
posX
- position xposY
- position ysquares
- 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 ArraypieceType
- PieceType objectcolor
- Color objectplayer
- Player object- Returns:
- ArrayList of Square object
-
getOccupiedSquares
return list of Squares with pieces on it- Parameters:
squares
- 2D Array of Square object- Returns:
- ArrayList of Square object
-
getMaxY
-
getMaxX
-
getBottom
-
getTop
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
gets Position object and Chessboard and returns the matched Square object to this position.- Parameters:
chessboard
- Chessboard objectposition
- Position object- Returns:
- matched Square object
-
initClean
-
initClean
-
removeOtherPieceTypes
-
getPieceByPosition
-
getPieceByPosition
-
setCommonPiecePlayer
Sets one common player for all pieces of a given pieceType (e.g. cannon)- Parameters:
chessboard
- The chessboard contextpieceType
- PieceType on which the common player should be appliedplayer
- 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
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
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
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.
-