A Kotlin-based chess game logic library for handling chess rules, move generation, and game state management — designed for flexibility, extensibility, and integration with engines like Stockfish.
- Complete Chess Logic: Implements all standard chess rules, including castling, en passant, promotion, and draw conditions (threefold repetition, fifty-move rule, insufficient material).
- FEN and PGN Support: Easily load and export games using standard Forsyth-Edwards Notation (FEN) and Portable Game Notation (PGN).
- Move Generation: Efficiently generates legal moves for any given position.
- Extensible Design: The project is structured to be easily extended with new features or integrations.
- Java Development Kit (JDK)
- Gradle
- Stockfish engine (optional, for AI opponent)
- Clone the repository:
git clone https://github.com/alluhemanth/chess-core.git
- Build the project using Gradle:
./gradlew build
This library is published in Jitpack, you can add it as your dependency it with:
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
dependencies {
implementation("com.github.alluhemanth:chess-core:1.0")
}
The project includes an example main
function that demonstrates how to use the chess engine to play a game against
Stockfish.
To run the example:
- Make sure you have the Stockfish engine installed and available in your system's PATH.
- Run the
main
function insrc/main/kotlin/com/hemanth/chess/core/Example.kt
.
The ChessGame
class provides a complete chess game implementation, including board state management, move generation,
and game logic. Below is an overview of its key methods and usage.
val game = ChessGame()
game.makeSanMove("e4")
game.makeSanMove("e5")
game.makeUciMove("e2e4")
game.makeUciMove("e7e5")
val move = game.getLegalMoves().first()
game.makeMove(move)
game.undo()
game.redo()
game.getBoard()
game.getCurrentPlayer()
game.isGameOver()
val result = game.getGameResult()
val legalMoves = game.getLegalMoves()
game.getFen()
game.loadFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
val pgn = game.getPgn()
game.loadPgn("1. e4 e5 2. Nf3 Nc6")
The Example.kt
file demonstrates how to integrate ChessGame
with the Stockfish engine for AI gameplay. Below is a
simplified version:
val game = ChessGame()
val stockfish = Stockfish()
stockfish.start()
Scanner(System.`in`).use { scanner ->
while (!game.isGameOver()) {
game.getBoard()
game.getFen()
game.getCurrentPlayer()
if (game.getCurrentPlayer() == PieceColor.WHITE) {
val input = scanner.nextLine()
if (input.equals("quit", ignoreCase = true)) break
game.makeSanMove(input)
} else {
val bestMove = stockfish.getBestMove(game.getFen())
game.makeUciMove(bestMove!!)
}
}
}
stockfish.stop()
game.getGameResult()
src/main/kotlin
: Contains the core source code for the chess engine.board
: Classes related to the chessboard, squares, files, and ranks.exception
: Custom exceptions for handling errors.game
: Game state management, rules, and results.move
: Move generation and representation.piece
: Piece types, colors, and representation.utils
: Utilities for handling FEN, PGN, and SAN (Standard Algebraic Notation).
src/test/kotlin
: Contains unit tests for the project.
Detailed documentation for this project is available at chess-core documentation.
Contributions are welcome! Please see the CONTRIBUTING file for detailed guidelines on how to contribute, report issues, and suggest features.
The rook icon used in this project is attributed to:
Cburnett, CC BY-SA 3.0, via Wikimedia Commons.
This project is licensed under the MIT License. See the LICENSE file for details.