ChessGame

class ChessGame(board: Board = Board.Companion.initialBoard(), gameState: GameState = GameState.Companion.newGame(), moveHistory: MutableList<Move> = mutableListOf(), currentMoveIndex: Int = -1, positionHistory: MutableList<String> = mutableListOf())

The ChessGame class represents a complete chess game, managing the board state, move history, and game logic. It provides methods for making moves, undoing/redoing moves, loading/saving positions in FEN/PGN formats, and querying the game's status. This class encapsulates the rules and mechanics of chess, ensuring valid gameplay and tracking the progression of the game.

Overview

  • Tracks the current Board state and GameState, including turn, castling rights, en passant targets, and clocks.

  • Maintains a history of moves and positions for undo/redo functionality and repetition detection.

  • Supports legal move generation, move validation, and game result determination.

  • Provides utility methods for interacting with the game using SAN (Standard Algebraic Notation) and UCI (Universal Chess Interface).

Internal Implementation Details

State Management

  • The current board state is stored in the board property, and the game state in gameState.

  • Move history is tracked in moveHistory, with currentMoveIndex indicating the active move.

  • Position history is stored as FEN strings in positionHistory for repetition detection and undo/redo functionality.

Move Handling

  • The system validates moves against the list of legal moves generated by LegalMoveGenerator.

  • It handles special moves such as castling, en passant, and pawn promotion seamlessly.

  • It supports undo and redo by replaying moves from the initial position.

Example of Usage

val game = ChessGame()
println(game.getBoard()) // Print initial board
game.makeSanMove("e4")
game.makeSanMove("e5")
println(game.getFen()) // Print FEN after two moves

Example of Internal Access

val game = ChessGame()
val legalMoves = game.getLegalMoves()
val move = legalMoves.first()
game.makeMove(move)
println(game.getFen())

Constructors

Link copied to clipboard
constructor(board: Board = Board.Companion.initialBoard(), gameState: GameState = GameState.Companion.newGame(), moveHistory: MutableList<Move> = mutableListOf(), currentMoveIndex: Int = -1, positionHistory: MutableList<String> = mutableListOf())

Functions

Link copied to clipboard

Returns the current Board state.

Link copied to clipboard

Returns the io.github.alluhemanth.chess.core.piece.PieceColor of the player whose turn it is.

Link copied to clipboard
fun getFen(): String

Returns the FEN (Forsyth-Edwards Notation) string for the current position.

Link copied to clipboard

Returns the current io.github.alluhemanth.chess.core.game.GameResult, determining if the game is ongoing, drawn, or won.

Link copied to clipboard

Returns the current GameState.

Link copied to clipboard

Returns a list of all legal moves for the current player.

Link copied to clipboard
fun getPgn(): String

Returns the PGN (Portable Game Notation) string for the current game.

Link copied to clipboard

Returns true if the game is over, false otherwise.

Link copied to clipboard
fun loadFen(fen: String)

Loads a position from a FEN string, resetting move and position history.

Link copied to clipboard
fun loadPgn(pgn: String)

Loads a game from a PGN string, resetting the board and game state.

Link copied to clipboard
fun makeMove(move: Move): Boolean

Makes the given move if it is legal, updating the board and game state.

Link copied to clipboard

Makes a move from a SAN (Standard Algebraic Notation) string.

Link copied to clipboard

Makes a move from a UCI (Universal Chess Interface) string.

Link copied to clipboard
fun redo(): Boolean

Redo the previously undone move, if possible.

Link copied to clipboard
fun undo(): Boolean

Undo the last move, if possible.