The chess engine core consists of standalone, reusable components that implement complete chess game logic. These components can be extracted into a separate library for use in other projects.
Located in backend/src/main/java/com/backend/domain/:
-
Chessboard.java- Core chess board logic- Move validation for all piece types
- Special moves (castling, en passant, promotion)
- Check and checkmate detection
- Stalemate and draw detection
- No dependencies on Spring or web frameworks
-
ChessGame.java- High-level game state management- Turn management
- Captured pieces tracking
- Move history
- Game state (check, checkmate, draw)
- Undo/redo functionality
- FEN import/export
- No dependencies on Spring or web frameworks
Located in backend/src/main/java/com/backend/models/:
ChessPiece.java- Immutable chess piece representationChessPieceType.java- Enum of piece typesColor.java- Enum for piece colorsPosition.java- Board position (row, col)Move.java- Move representation with metadataGameState.java- Enum for game statesGameStateSnapshot.java- Immutable game state snapshot for undo/redo
Located in backend/src/main/java/com/backend/util/:
-
FENParser.java- FEN notation parser and generator- Parse FEN strings to board state
- Generate FEN from board state
- No external dependencies
-
PGNExporter.java- PGN format exporter- Export games to standard PGN format
- Move notation generation
Located in backend/src/main/java/com/backend/ai/:
-
BoardEvaluator.java- Static board position evaluation- Material evaluation
- Positional bonuses
- No external dependencies
-
ChessAI.java- Minimax AI with alpha-beta pruning- Configurable search depth
- Move generation and selection
- Depends only on domain and model classes
-
Create a new module
chess-engine-core:chess-engine-core/ ├── build.gradle.kts └── src/main/java/com/chessengine/ ├── domain/ │ ├── Chessboard.java │ └── ChessGame.java ├── models/ │ ├── ChessPiece.java │ ├── Position.java │ ├── Move.java │ └── ... ├── util/ │ ├── FENParser.java │ └── PGNExporter.java └── ai/ ├── BoardEvaluator.java └── ChessAI.java -
Update root
settings.gradle.kts:rootProject.name = "chess-engine-reference" include("chess-engine-core") include("backend")
-
Add dependency in
backend/build.gradle.kts:dependencies { implementation(project(":chess-engine-core")) // ... other dependencies } -
Move core classes to
chess-engine-coremodule -
Update package names and imports in backend
-
Create a new repository
chess-engine-core -
Copy core classes (domain, models, util, ai) to new project
-
Create build configuration:
// build.gradle.kts plugins { java `maven-publish` } group = "com.chessengine" version = "1.0.0" publishing { publications { create<MavenPublication>("maven") { from(components["java"]) } } }
-
Publish to Maven Central or private repository
-
Add dependency in consuming projects:
dependencies { implementation("com.chessengine:chess-engine-core:1.0.0") }
-
Create standalone project with core classes
-
Build JAR:
./gradlew jar
-
Include JAR in other projects' classpath
The chess engine core has ZERO external dependencies beyond Java 17 standard library.
- Complete chess rules implementation
- Move validation and generation
- Check/checkmate/stalemate detection
- FEN import/export
- PGN export
- Undo/redo support
- Basic AI with minimax
- REST API endpoints (in
backend/controllers) - Spring Boot configuration
- Web UI
- Database persistence
- Network play
import com.backend.domain.ChessGame;
import com.backend.models.Position;
public class Example {
public static void main(String[] args) {
// Create a new game
ChessGame game = new ChessGame();
// Make a move (e2 to e4)
Position from = new Position(2, 5);
Position to = new Position(4, 5);
game.MoveController(from, to);
// Get valid moves for a piece
Position[] validMoves = game.getValidMovesController(new Position(1, 7));
// Export to FEN
String fen = game.exportToFEN();
// Undo last move
game.undo();
// Get AI suggested move
import com.backend.ai.ChessAI;
ChessAI.AIMove aiMove = ChessAI.findBestMove(game);
if (aiMove != null) {
game.MoveController(aiMove.from, aiMove.to);
}
}
}All core components have comprehensive unit tests in backend/src/test/java/:
ChessBoardTest.java- Board and move validation testsGameStateTest.java- Check and checkmate testsCastlingTest.java- Castling rules testsDrawDetectionTest.java- Draw condition testsUndoRedoTest.java- Undo/redo functionality testsFENParserTest.java- FEN parsing testsPGNExporterTest.java- PGN export tests
These tests can be moved along with the core classes to ensure the library works correctly.
- Reusability - Use chess engine in multiple projects
- Separation of Concerns - Pure game logic separate from API/UI
- Testability - Core logic tested independently
- Portability - Can be used in different frameworks (Spring, Jakarta, etc.)
- Version Control - Independent versioning for chess engine
- Distribution - Can be published as Maven/Gradle artifact
The chess engine is currently embedded within the Spring Boot backend but is architected with clean separation:
- ✅ No Spring dependencies in core classes
- ✅ No web/HTTP dependencies in core classes
- ✅ All core logic is pure Java
- ✅ Comprehensive test coverage
- ✅ Well-documented code
- ⏳ Not yet extracted to separate module (can be done following this guide)
For extracting the chess engine:
- Short term: Keep current structure, but be aware of the clean boundaries
- Medium term: Create multi-module Gradle project (Option 1)
- Long term: Publish as standalone artifact to Maven Central (Option 2)
The current architecture makes any of these transitions straightforward.