is_repetition: Check for n-fold repetition in chess

View source: R/is_repetition.R

is_repetitionR Documentation

Check for n-fold repetition in chess

Description

This function checks if a position has occurred n times in a list of chessboards. See fen_to_board() for details of the board format. The repetition of a position is determined as described in The FIDE Laws of Chess, Section 9.2. That is, positions are considered to be repeated only if the pieces are on the same squares, the same side has the move, castling rights are the same, and en passant rights are the same.

Usage

is_repetition(board_list, n)

Arguments

board_list

A list of chess boards.

n

The number of repetitions to check for.

Value

TRUE if any position is repeated n times, FALSE otherwise.

Examples

# Repeat positions by moving knights back and forth
boards <- list(fen_to_board()) # starting position
boards[[2]] <- update_board('g1f3', boards[[1]])
boards[[3]] <- update_board('g8f6', boards[[2]])
boards[[4]] <- update_board('f3g1', boards[[3]])
boards[[5]] <- update_board('f6g8', boards[[4]]) # back to starting position
is_repetition(boards, 3)
boards[[6]] <- update_board('g1f3', boards[[5]])
boards[[7]] <- update_board('g8f6', boards[[6]])
boards[[8]] <- update_board('f3g1', boards[[7]])
boards[[9]] <- update_board('f6g8', boards[[8]]) # back to starting position
is_repetition(boards, 3)

dryguy/rbitr documentation built on Oct. 15, 2024, 6:18 a.m.