Class ChessGameService

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

@Service public class ChessGameService extends Object
Game service to initialize new Game and do operations on it.
  • Constructor Details

    • ChessGameService

      @Autowired public ChessGameService(@Qualifier("GameRepositoryClass") IGameRepository gameRepository)
  • Method Details

    • getNewGameID

      public static Integer getNewGameID()
      Generate new ID for a game object
      Returns:
      id
    • createNewGame

      public static ChessGame createNewGame(String[] playerNames)
      create new Game
      Parameters:
      playerNames - players names
      Returns:
      ChessGame
    • getGame

      public ChessGame getGame(Integer gameID)
      Returns a game object by gameID
      Parameters:
      gameID - The gameID key under which the game object is stored in the gameRepository
      Returns:
      ChessGame
    • getBoard

      public ArrayList<ArrayList<Square>> getBoard(int gameID)
      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

      public static ArrayList<ArrayList<Square>> getAllSquaresFromChessboard(ChessGame game)
      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 id
      previousPiecePosition - [x,y] position
      newPiecePosition - [x,y] position
      Returns:
      true if valid
    • getPossibleMoves

      public int[][] 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]]
      Parameters:
      game - A game object
      piecePosition - Integer array representing a position
      Returns:
      Two-dimensional integer array of moves.
    • executedMove

      public String executedMove(int gameID, int[] previousPiecePosition, int[] newPiecePosition)
      move piece from one position to another
      Parameters:
      gameID - game id
      previousPiecePosition - [x,y] position
      newPiecePosition - [x,y] position
      Returns:
      if verified game id and valid move return the Activate player name otherwise return empty string
    • getActivePlayerName

      public static String getActivePlayerName(ChessGame game)
      Get the name of the active player.
      Returns:
      The name of the active player.
    • switchActive

      public static void switchActive(ChessGame game)
      Method to switch active players after move
      Parameters:
      game - Game object
    • getPlayerTurn

      public String getPlayerTurn(int gameID)
      returns activate player
      Parameters:
      gameID - game id
      Returns:
      player name
    • setActivePlayer

      public static void setActivePlayer(ChessGame chessGame, Player activePlayer)
      Helper method to set the active player and update all cannon pieces.
      Parameters:
      chessGame - Game object
      activePlayer - New active player