Position

class Position

Class to represent a position on the chessboard. It is immutable.

This class provides all the necessary information to perform conventional logical operations based on a position, primarily through bitboard representations. This class cannot be instantiated directly but must be accessed via the factory function positionOf, which ensures the provided FEN string (if any) is well-formed and valid.

This is a facade created to enable exporting the code to JS, though it can also be used directly within the JS modules of any KMP project.

Properties

Link copied to clipboard

Array of Bitboards. The index in the array is equal to the ordinal value minus one of the corresponding piece (e.g., Black Knight distribution is at index BN.ordinal - 1).

Link copied to clipboard

True if Black can castle kingside (short castling). False otherwise.

Link copied to clipboard

True if Black can castle queenside (long castling). False otherwise.

Link copied to clipboard

True if the current side to move is in check. False otherwise.

Link copied to clipboard

True if the position is a checkmate (the current side is in check and has no legal moves). False otherwise.

Link copied to clipboard

A list of tuples, where each tuple represents a legal move from this position and the resulting new position (Tuple). This list effectively defines the legal branches of the game tree from the current position.

Link copied to clipboard

True if the position is a forced draw. This is true if the position results in a stalemate or lackOfMaterial. False otherwise.

Link copied to clipboard

The square index (0-63) where a pawn can be captured en passant. Returns -1 if no en passant capture is possible.

Link copied to clipboard

The square exposed to an en passant capture, if one exists. Returns null if no en passant capture is possible in the current position.

Link copied to clipboard
val fen: String

The FEN (Forsyth-Edwards Notation) string representation of the position.

Link copied to clipboard

True if the position has reached or exceeded 50 half-moves without a pawn move or capture.

Link copied to clipboard

True if the game state is concluded (terminal position), either due to a forced draw or checkmate. False otherwise.

Link copied to clipboard

The number of half-moves since the last pawn move or capture. This is used for the 50-move rule.

Link copied to clipboard

True if the position is a draw due to insufficient mating material (e.g., King vs. King). False otherwise.

Link copied to clipboard

The full move number in the game (starts at 1 and is incremented after Black's move).

Link copied to clipboard

The side (WHITE or BLACK) whose turn it is to move.

Link copied to clipboard

A 64-element array where the index corresponds to the square order defined in Square (A1=0, H8=63). The value of each element is the ordinal of the Piece occupying that square (0 for EMPTY, 1 for WP, and so on).

Link copied to clipboard

True if the position is a stalemate (the current side is not in check but has no legal moves). False otherwise.

Link copied to clipboard

True if White can castle kingside (short castling). False otherwise.

Link copied to clipboard

True if White can castle queenside (long castling). False otherwise.

Link copied to clipboard

True if it is White's turn to move. False if it is Black's turn.

Link copied to clipboard

The Zobrist hash key of the position. This is used for efficient position lookup and repetition detection.

Functions

Link copied to clipboard

Determines if Black has insufficient material to win the game. Returns true if the current pieces for Black cannot potentially lead to a checkmate.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
fun isLegal(move: Move): Boolean

Returns true if the evaluated Move is legal in the current position, and false otherwise.

Link copied to clipboard
fun move(move: Move): Position

Retrieves the new Position that results from executing the provided legal Move. Throws a MoveException if the provided move is not legal in the current position.

Link copied to clipboard
fun moveFromString(move: String, notation: Notation = UCI): Position

Retrieves the new Position that results from executing the move specified in the given notation. Throws a MoveException if the move is not legal. If no notation is provided, UCI notation is assumed.

Link copied to clipboard
fun pieceAt(square: Square): Piece

Retrieves the piece object that occupies the given square.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard

Determines if White has insufficient material to win the game. Returns true if the current pieces for White cannot potentially lead to a checkmate.