Square

data class Square(val file: File, val rank: Rank)

The Square class represents a square on a chessboard, defined by a File (column) and a Rank (row). It provides methods for parsing algebraic notation, determining square color, and string representation.

Overview

  • A square is defined by a file ('a' to 'h') and a rank (1 to 8).

  • Provides utility methods for parsing, color determination, and string representation.

Parsing

  • From Algebraic Notation: The Square can be constructed from algebraic notation (e.g., "e4").

  • Validates the notation and extracts the file and rank components.

Square Color

  • Dark Square: A square is dark if the sum of its file and rank indices is even.

  • Light Square: A square is light if the sum of its file and rank indices is odd.

Example Usage

val square = Square("e4")
val isDark = square.isDarkSquare() // true
val notation = square.toString() // "e4"

Constructors

Link copied to clipboard
constructor(notation: String)

Constructs a Square from algebraic notation (e.g., "e4").

constructor(file: File, rank: Rank)

Properties

Link copied to clipboard
val file: File

The file (column) of the square.

Link copied to clipboard
val rank: Rank

The rank (row) of the square.

Functions

Link copied to clipboard
internal fun isDarkSquare(): Boolean

Returns true if the square is a dark square.

Link copied to clipboard
internal fun isLightSquare(): Boolean

Returns true if the square is a light square.

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

Returns the string representation of the square in algebraic notation (e.g., "e4").