import com.backend.domain.ChessGame;
import com.backend.models.*;
// Create new game
ChessGame game = new ChessGame();
// Make a move
Position from = new Position(2, 5); // e2
Position to = new Position(4, 5); // e4
ChessPiece result = game.MoveController(from, to);
// Check if move was valid
if (result.type() != ChessPieceType.Invalid) {
System.out.println("Move successful!");
}Make a move on the board.
- Parameters: Source and target positions (1-indexed)
- Returns:
ChessPiece- captured piece, or Invalid if move is illegal - Side effects: Updates game state, switches turn
Make a move with pawn promotion.
- Parameters: Source, target, and promotion piece type
- Returns:
ChessPiece- captured piece, or Invalid if move is illegal
Get all valid moves for a piece.
- Parameters: Position of piece (1-indexed)
- Returns: Array of valid target positions
Get current player's turn.
- Returns:
Color.WhiteorColor.Black
Get current game state.
- Returns:
GameStateenum (Free, Check, Checkmate, DrawByStalemate, etc.)
Get board representation for display.
- Returns: Array of
ChessPieceResponsefor rendering
Get list of all moves made.
- Returns:
List<Move>- move history
Get captured pieces for a player.
- Returns:
Set<ChessPiece>- pieces captured by the color
Import position from FEN notation.
- Parameters: FEN string
- Throws:
IllegalArgumentExceptionfor invalid FEN
Export current position to FEN.
- Returns: FEN string
Export game in PGN format.
- Returns: PGN formatted string
Undo last move.
- Returns:
trueif successful,falseif nothing to undo
Redo previously undone move.
- Returns:
trueif successful,falseif nothing to redo
Check if undo is available.
- Returns:
boolean
Check if redo is available.
- Returns:
boolean
Find best move using minimax algorithm.
import com.backend.ai.ChessAI;
ChessAI.AIMove move = ChessAI.findBestMove(game);
if (move != null) {
game.MoveController(move.from, move.to);
}Find best move with custom search depth.
- Parameters: game state, search depth (default: 3)
- Returns:
AIMovewith from/to positions and evaluation score
Parse FEN string to board state.
import com.backend.util.FENParser;
FENParser.FENParseResult result = FENParser.parseFEN(fen);
// Access result.board, result.activeColor, result.castlingRights, etc.Generate FEN from board state.
String fen = FENParser.generateFEN(
board, activeColor,
whiteKingMoved, blackKingMoved,
whiteKingsideRookMoved, whiteQueensideRookMoved,
blackKingsideRookMoved, blackQueensideRookMoved,
enPassantTarget, halfMoveClock, fullMoveNumber
);Immutable record representing a chess piece.
type()- ChessPieceType (Pawn, Knight, Bishop, Rock, Queen, King)color()- Color (White, Black, None)
Represents board position.
row- Row index (1-8 for user-facing APIs, 0-7 internally)col- Column index (1-8 for user-facing APIs, 0-7 internally)
Represents a chess move with metadata.
getFrom()- Source positiongetTo()- Target positiongetPiece()- Piece that movedgetCapturedPiece()- Piece captured (if any)isCapture()- Whether this was a captureisPawnMove()- Whether a pawn movedisEnPassant()- Whether this was en passantisCastling()- Whether this was castling
Free- Normal playCheck- King in checkCheckmate- Game over, checkmateDrawByStalemate- Game over, stalemateDrawByFiftyMove- Draw by 50-move ruleDrawByRepetition- Draw by threefold repetition
Evaluate board position for a color.
import com.backend.ai.BoardEvaluator;
int score = BoardEvaluator.evaluate(board, Color.White);
// Positive = White winning, Negative = Black winningChessGame game = new ChessGame();
// White moves e2-e4
game.MoveController(new Position(2, 5), new Position(4, 5));
// Black moves e7-e5
game.MoveController(new Position(7, 5), new Position(5, 5));
// Check game state
if (game.getGameState() == GameState.Check) {
System.out.println("Check!");
}// Import from FEN
String fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
game.importFromFEN(fen);
// Play some moves...
// Export current position
String currentFEN = game.exportToFEN();
System.out.println(currentFEN);ChessGame game = new ChessGame();
while (game.getGameState() == GameState.Free ||
game.getGameState() == GameState.Check) {
if (game.getTurn() == Color.White) {
// Human move (get from UI)
game.MoveController(userFrom, userTo);
} else {
// AI move
ChessAI.AIMove aiMove = ChessAI.findBestMove(game);
if (aiMove != null) {
game.MoveController(aiMove.from, aiMove.to);
System.out.println("AI moved from " + aiMove.from +
" to " + aiMove.to +
" (score: " + aiMove.score + ")");
}
}
}
System.out.println("Game over: " + game.getGameState());// Make moves
game.MoveController(new Position(2, 5), new Position(4, 5));
game.MoveController(new Position(7, 5), new Position(5, 5));
// Undo last move
if (game.canUndo()) {
game.undo();
}
// Redo
if (game.canRedo()) {
game.redo();
}The chess engine classes are not thread-safe. If using in a multi-threaded environment:
- Use one
ChessGameinstance per game/thread - Or synchronize access externally
BoardEvaluatormethods are stateless and thread-safe
- Move validation: O(n) where n = number of pieces
- AI move generation (depth 3): ~0.5-2 seconds typical
- FEN parsing: O(1) for fixed board size
- Undo/redo: O(1) with memory overhead for snapshots