Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSDoc comments and documentation #184

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove redundant symbol in jsdoc
  • Loading branch information
StevenStavrakis committed Jun 3, 2024
commit 730933a72b705c7d7f6a58717f47334199cb8738
78 changes: 41 additions & 37 deletions src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export class PositionError extends Error { }

/**
* Calculates the attacking squares for a given square and attacker color.
* @param {Square} square - The target square.
* @param {Color} attacker - The attacking color.
* @param {Board} board - The chess board.
* @param {SquareSet} occupied - The occupied squares on the board.
* @param {Square} square The target square.
* @param {Color} attacker The attacking color.
* @param {Board} board The chess board.
* @param {SquareSet} occupied The occupied squares on the board.
* @returns {SquareSet} The squares from which the target square is attacked.
*/
const attacksTo = (square: Square, attacker: Color, board: Board, occupied: SquareSet): SquareSet =>
Expand Down Expand Up @@ -136,10 +136,10 @@ export class Castles {

/**
* Adds castling rights for the given color and side.
* @param {Color} color - The color.
* @param {CastlingSide} side - The castling side.
* @param {Square} king - The king's square.
* @param {Square} rook - The rook's square.
* @param {Color} color The color.
* @param {CastlingSide} side The castling side.
* @param {Square} king The king's square.
* @param {Square} rook The rook's square.
*/
private add(color: Color, side: CastlingSide, king: Square, rook: Square): void {
const kingTo = kingCastlesTo(color, side);
Expand All @@ -155,7 +155,7 @@ export class Castles {

/**
* Creates a Castles instance from the given setup.
* @param {Setup} setup - The chess setup.
* @param {Setup} setup The chess setup.
* @returns {Castles} The Castles instance derived from the setup.
*/
static fromSetup(setup: Setup): Castles {
Expand All @@ -176,7 +176,7 @@ export class Castles {

/**
* Discards castling rights for the rook on the given square.
* @param {Square} square - The square of the rook.
* @param {Square} square The square of the rook.
*/
discardRook(square: Square): void {
if (this.castlingRights.has(square)) {
Expand All @@ -191,7 +191,7 @@ export class Castles {

/**
* Discards castling rights for the given color.
* @param {Color} color - The color to discard castling rights for.
* @param {Color} color The color to discard castling rights for.
*/
discardColor(color: Color): void {
this.castlingRights = this.castlingRights.diff(SquareSet.backrank(color));
Expand All @@ -204,11 +204,11 @@ export class Castles {
* TODO: Not sure what this is
* Represents the context of a chess position.
* @interface
* @property {Square | undefined} king - The square of the king.
* @property {SquareSet} blockers - The set of blocking squares.
* @property {SquareSet} checkers - The set of checking squares.
* @property {boolean} variantEnd - Whether the variant has ended.
* @property {boolean} mustCapture - Whether a capture is required.
* @property {Square | undefined} king The square of the king.
* @property {SquareSet} blockers The set of blocking squares.
* @property {SquareSet} checkers The set of checking squares.
* @property {boolean} variantEnd Whether the variant has ended.
* @property {boolean} mustCapture Whether a capture is required.
*/
export interface Context {
king: Square | undefined;
Expand All @@ -224,49 +224,53 @@ export interface Context {
*/
export abstract class Position {
/**
* The current Board.
* The board state of the position.
* @type {Board}
*/
board: Board;

/**
* Represents the taken pieces.
* The pocket pieces (captured pieces) of the position, if any.
* @type {Material | undefined}
*/
pockets: Material | undefined;

/**
* The current turn.
* The color of the side to move in the position.
* @type {Color}
*/
turn: Color;

/**
* TODO: Not sure what this is
* The castling rights of the position.
* @type {Castles}
*/
castles: Castles;

/**
* TODO: Not sure what this is
* The en passant square of the position, if any.
* @type {Square | undefined}
*/
epSquare: Square | undefined;

/**
* TODO: Not sure what this is
* The remaining checks count of the position, if applicable.
* @type {RemainingChecks | undefined}
*/
remainingChecks: RemainingChecks | undefined;

/**
* Number of times pieces have been moved.
* For example: 1.e4 is a halfmove, but 1.e4 e5 is a fullmove.
* The number of halfmoves since the last pawn advance or capture.
* @type {number}
*/
halfmoves: number;

/**
* Number of times both sides have played a move.
* For example: 1.e4 e5 1.e5 is a fullmove, but 1.e4 e5 1.e5 e6 is two fullmoves.
* The number of fullmoves (each player's turn counts as one fullmove).
* @type {number}
*/
fullmoves: number;

/**
* Creates a new Position instance.
* @param {Rules} rules - The chess rules.
*/
protected constructor(readonly rules: Rules) { }

/**
Expand All @@ -285,7 +289,7 @@ export abstract class Position {

/**
* Sets up the position from the given setup without validation.
* @param {Setup} setup - The chess setup.
* @param {Setup} setup The chess setup.
* @protected
*/
protected setupUnchecked(setup: Setup) {
Expand Down Expand Up @@ -314,9 +318,9 @@ export abstract class Position {

/**
* Calculates the attacking squares for the king on the given square by the given color.
* @param {Square} square - The square of the king.
* @param {Color} attacker - The attacking color.
* @param {SquareSet} occupied - The occupied squares on the board.
* @param {Square} square The square of the king.
* @param {Color} attacker The attacking color.
* @param {SquareSet} occupied The occupied squares on the board.
* @returns {SquareSet} The squares from which the king is attacked.
*/
kingAttackers(square: Square, attacker: Color, occupied: SquareSet): SquareSet {
Expand All @@ -325,8 +329,8 @@ export abstract class Position {

/**
* Executes a capture at the given square.
* @param {Square} square - The square where the capture occurs.
* @param {Piece} captured - The captured piece.
* @param {Square} square The square where the capture occurs.
* @param {Piece} captured The captured piece.
* @protected
*/
protected playCaptureAt(square: Square, captured: Piece): void {
Expand Down Expand Up @@ -718,7 +722,7 @@ export class Chess extends Position {
/**
* Create a new, unchecked chess game from a setup.
* TODO: There is validation, but I'm not sure what it is.
* @param {Setup} setup - The chess setup.
* @param {Setup} setup The chess setup.
* @returns Chess or an error.
*/
static fromSetup(setup: Setup): Result<Chess, PositionError> {
Expand Down
30 changes: 15 additions & 15 deletions src/fen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const parseBoardFen = (boardPart: string): Result<Board, FenError> => {
/**
* Parses the pockets part of a FEN (Forsyth-Edwards Notation) string and returns a Material object.
*
* @param {string} pocketPart - The pockets part of the FEN string.
* @param {string} pocketPart The pockets part of the FEN string.
* @returns {Result<Material, FenError>} The parsed Material object if successful, or a FenError if parsing fails.
*
* @throws {FenError} Throws a FenError if the pockets part is invalid.
Expand Down Expand Up @@ -117,8 +117,8 @@ export const parsePockets = (pocketPart: string): Result<Material, FenError> =>
/**
* Parses the castling part of a FEN string and returns the corresponding castling rights as a SquareSet.
*
* @param {Board} board - The chess board.
* @param {string} castlingPart - The castling part of the FEN string.
* @param {Board} board The chess board.
* @param {string} castlingPart The castling part of the FEN string.
* @returns {Result<SquareSet, FenError>} The castling rights as a SquareSet if parsing is successful, or a FenError if parsing fails.
*/
export const parseCastlingFen = (board: Board, castlingPart: string): Result<SquareSet, FenError> => {
Expand Down Expand Up @@ -152,7 +152,7 @@ export const parseCastlingFen = (board: Board, castlingPart: string): Result<Squ
*
* Parses the remaining checks part of a FEN string and returns the corresponding RemainingChecks object.
*
* @param {string} part - The remaining checks part of the FEN string.
* @param {string} part The remaining checks part of the FEN string.
* @returns {Result<RemainingChecks, FenError>} The RemainingChecks object if parsing is successful, or a FenError if parsing fails.
*
* @example
Expand Down Expand Up @@ -195,7 +195,7 @@ export const parseRemainingChecks = (part: string): Result<RemainingChecks, FenE
/**
* Parses a FEN (Forsyth-Edwards Notation) string and returns a Setup object.
*
* @param {string} fen - The FEN string to parse.
* @param {string} fen The FEN string to parse.
* @returns {Result<Setup, FenError>} The parsed Setup object if successful, or a FenError if parsing fails.
*
* @throws {FenError} Throws a FenError if the FEN string is invalid.
Expand Down Expand Up @@ -306,7 +306,7 @@ export interface FenOpts {
/**
* Parses a string representation of a chess piece and returns the corresponding Piece object.
*
* @param {string} str - The string representation of the piece.
* @param {string} str The string representation of the piece.
* @returns {Piece | undefined} The parsed Piece object, or undefined if the string is invalid.
*
* @example
Expand All @@ -327,7 +327,7 @@ export const parsePiece = (str: string): Piece | undefined => {
/**
* Converts a Piece object to its string representation.
*
* @param {Piece} piece - The Piece object to convert.
* @param {Piece} piece The Piece object to convert.
* @returns {string} The string representation of the piece.
*
* @example
Expand All @@ -347,7 +347,7 @@ export const makePiece = (piece: Piece): string => {
/**
* Converts a Board object to its FEN (Forsyth-Edwards Notation) string representation.
*
* @param {Board} board - The Board object to convert.
* @param {Board} board The Board object to convert.
* @returns {string} The FEN string representation of the board.
*/
export const makeBoardFen = (board: Board): string => {
Expand Down Expand Up @@ -382,7 +382,7 @@ export const makeBoardFen = (board: Board): string => {
/**
* Converts a MaterialSide object to its string representation.
*
* @param {MaterialSide} material - The MaterialSide object to convert.
* @param {MaterialSide} material The MaterialSide object to convert.
* @returns {string} The string representation of the material.
*/
export const makePocket = (material: MaterialSide): string =>
Expand All @@ -391,7 +391,7 @@ export const makePocket = (material: MaterialSide): string =>
/**
* Converts a Material object to its string representation.
*
* @param {Material} pocket - The Material object to convert.
* @param {Material} pocket The Material object to convert.
* @returns {string} The string representation of the pocket.
*/
export const makePockets = (pocket: Material): string =>
Expand All @@ -400,8 +400,8 @@ export const makePockets = (pocket: Material): string =>
/**
* Converts the castling rights of a board to its FEN string representation.
*
* @param {Board} board - The Board object.
* @param {SquareSet} castlingRights - The castling rights as a SquareSet.
* @param {Board} board The Board object.
* @param {SquareSet} castlingRights The castling rights as a SquareSet.
* @returns {string} The FEN string representation of the castling rights.
*/
export const makeCastlingFen = (board: Board, castlingRights: SquareSet): string => {
Expand All @@ -428,7 +428,7 @@ export const makeCastlingFen = (board: Board, castlingRights: SquareSet): string
/**
* Converts a RemainingChecks object to its string representation.
*
* @param {RemainingChecks} checks - The RemainingChecks object to convert.
* @param {RemainingChecks} checks The RemainingChecks object to convert.
* @returns {string} The string representation of the remaining checks.
*/
export const makeRemainingChecks = (checks: RemainingChecks): string => `${checks.white}+${checks.black}`;
Expand All @@ -437,8 +437,8 @@ export const makeRemainingChecks = (checks: RemainingChecks): string => `${check
/**
* Converts a Setup object to its FEN string representation.
*
* @param {Setup} setup - The Setup object to convert.
* @param {FenOpts} [opts] - Optional FEN formatting options.
* @param {Setup} setup The Setup object to convert.
* @param {FenOpts} [opts] Optional FEN formatting options.
* @returns {string} The FEN string representation of the setup.
*/
export const makeFen = (setup: Setup, opts?: FenOpts): string =>
Expand Down
20 changes: 10 additions & 10 deletions src/san.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { charToRole, defined, makeSquare, opposite, parseSquare, roleToChar, squ
* Generates the SAN (Standard Algebraic Notation) representation of a move
* in the given position without the move suffix (#, +).
*
* @param {Position} pos - The chess position.
* @param {Move} move - The move to generate the SAN for.
* @param {Position} pos The chess position.
* @param {Move} move The move to generate the SAN for.
* @returns {string} The SAN representation of the move.
*/
const makeSanWithoutSuffix = (pos: Position, move: Move): string => {
Expand Down Expand Up @@ -64,8 +64,8 @@ const makeSanWithoutSuffix = (pos: Position, move: Move): string => {
* Generates the SAN (Standard Algebraic Notation) representation of a move
* in the given position and plays the move on the position.
*
* @param {Position} pos - The chess position.
* @param {Move} move - The move to generate the SAN for and play.
* @param {Position} pos The chess position.
* @param {Move} move The move to generate the SAN for and play.
* @returns {string} The SAN representation of the move with the move suffix.
*/
export const makeSanAndPlay = (pos: Position, move: Move): string => {
Expand All @@ -80,8 +80,8 @@ export const makeSanAndPlay = (pos: Position, move: Move): string => {
* Generates the SAN (Standard Algebraic Notation) representation of a variation
* (sequence of moves) in the given position.
*
* @param {Position} pos - The starting position of the variation.
* @param {Move[]} variation - The sequence of moves in the variation.
* @param {Position} pos The starting position of the variation.
* @param {Move[]} variation The sequence of moves in the variation.
* @returns {string} The SAN representation of the variation.
*/
export const makeSanVariation = (pos: Position, variation: Move[]): string => {
Expand All @@ -105,8 +105,8 @@ export const makeSanVariation = (pos: Position, variation: Move[]): string => {
* Generates the SAN (Standard Algebraic Notation) representation of a move
* in the given position without modifying the position.
*
* @param {Position} pos - The chess position.
* @param {Move} move - The move to generate the SAN for.
* @param {Position} pos The chess position.
* @param {Move} move The move to generate the SAN for.
* @returns {string} The SAN representation of the move.
*/
export const makeSan = (pos: Position, move: Move): string => makeSanAndPlay(pos.clone(), move);
Expand All @@ -115,8 +115,8 @@ export const makeSan = (pos: Position, move: Move): string => makeSanAndPlay(pos
* Parses a SAN (Standard Algebraic Notation) string and returns the corresponding move
* in the given position.
*
* @param {Position} pos - The chess position.
* @param {string} san - The SAN string to parse.
* @param {Position} pos The chess position.
* @param {string} san The SAN string to parse.
* @returns {Move | undefined} The parsed move, or undefined if the SAN is invalid or ambiguous.
*/
export const parseSan = (pos: Position, san: string): Move | undefined => {
Expand Down
Loading