Package com.chess.backend.services
Class ChessGameService
java.lang.Object
com.chess.backend.services.ChessGameService
Game service to initialize new Game and do operations on it.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ChessGame
createNewGame
(String[] playerNames) create new GameexecutedMove
(int gameID, int[] previousPiecePosition, int[] newPiecePosition) move piece from one position to anotherstatic String
getActivePlayerName
(ChessGame game) Get the name of the active player.Returns the ChessBoard of a game objectgetBoard
(int gameID) Returns the ChessBoard by gameIDReturns a game object by gameIDstatic Integer
Generate new ID for a game objectgetPlayerTurn
(int gameID) returns activate playerint[][]
getPossibleMoves
(ChessGame game, int[] piecePosition) returns possible moves for a piece position return value looks like that [[x,y], [2,2], [2,3], [3,3]]static void
setActivePlayer
(ChessGame chessGame, Player activePlayer) Helper method to set the active player and update all cannon pieces.static void
switchActive
(ChessGame game) Method to switch active players after moveboolean
validateMove
(int gameID, int[] previousPiecePosition, int[] newPiecePosition) check if piece can move to the target position
-
Constructor Details
-
ChessGameService
@Autowired public ChessGameService(@Qualifier("GameRepositoryClass") IGameRepository gameRepository)
-
-
Method Details
-
getNewGameID
Generate new ID for a game object- Returns:
- id
-
createNewGame
create new Game- Parameters:
playerNames
- players names- Returns:
- ChessGame
-
getGame
Returns a game object by gameID- Parameters:
gameID
- The gameID key under which the game object is stored in the gameRepository- Returns:
- ChessGame
-
getBoard
Returns the ChessBoard by gameID- Parameters:
gameID
- The gameID key under which the game object is stored in the gameRepository- Returns:
- ChessBoard nested ArrayList
-
getAllSquaresFromChessboard
Returns the ChessBoard of a game object- Parameters:
game
- A game object- Returns:
- ChessBoard nested ArrayList
-
validateMove
public boolean validateMove(int gameID, int[] previousPiecePosition, int[] newPiecePosition) check if piece can move to the target position- Parameters:
gameID
- game idpreviousPiecePosition
- [x,y] positionnewPiecePosition
- [x,y] position- Returns:
- true if valid
-
getPossibleMoves
returns possible moves for a piece position return value looks like that [[x,y], [2,2], [2,3], [3,3]]- Parameters:
game
- A game objectpiecePosition
- Integer array representing a position- Returns:
- Two-dimensional integer array of moves.
-
executedMove
move piece from one position to another- Parameters:
gameID
- game idpreviousPiecePosition
- [x,y] positionnewPiecePosition
- [x,y] position- Returns:
- if verified game id and valid move return the Activate player name otherwise return empty string
-
getActivePlayerName
Get the name of the active player.- Returns:
- The name of the active player.
-
switchActive
Method to switch active players after move- Parameters:
game
- Game object
-
getPlayerTurn
returns activate player- Parameters:
gameID
- game id- Returns:
- player name
-
setActivePlayer
Helper method to set the active player and update all cannon pieces.- Parameters:
chessGame
- Game objectactivePlayer
- New active player
-