On this page:
chess-board?
empty-chess-board
starting-chess-board
chess-board
into-chess-board
chess-board-ref
chess-board-ref-square
7.5

2 Chess Boards

 (require chess/board) package: chess

A chess board is an 8-by-8 rectangular grid of squares, and a collection of chess pieces placed on those squares.

procedure

(chess-board? v)  boolean?

  v : any/c
A predicate for chess boards.

The empty chess board, which contains no pieces.

Example:

The standard starting chess board.

Example:

procedure

(chess-board square piece ... ...)  chess-board?

  square : chess-square?
  piece : colored-chess-piece?
Constructs a chess board with each piece placed on its corresponding square.

Examples:
> (define board
    (chess-board f2 white-pawn
                 g2 white-pawn
                 h2 white-pawn
                 f1 white-rook
                 g1 white-king))
> (chess-board-pict board)

image

A reducer that collects occupied chess squares into a chess board.

Examples:
> (define board
    (for/reducer into-chess-board
                 ([rank (in-chess-ranks)]
                  [file (in-chess-files)]
                  [piece (in-immutable-set colored-chess-pieces)])
      (define square (chess-square #:rank rank #:file file))
      (chess-square-occupy square piece)))
> (chess-board-pict board)

image

procedure

(chess-board-ref board square)  (or/c colored-chess-piece? #f)

  board : chess-board?
  square : chess-square?
Returns the chess piece at square in board, or #f if the board is empty at that square.

Examples:
> (chess-board-ref starting-chess-board a1)

(colored-chess-piece #:owner #<white> #:type #<rook>)

> (chess-board-ref starting-chess-board e8)

(colored-chess-piece #:owner #<black> #:type #<king>)

> (chess-board-ref empty-chess-board a1)

#f

procedure

(chess-board-ref-square board square)

  (or/c chess-square? occupied-chess-square?)
  board : chess-board?
  square : chess-square?
Like chess-board-ref, but returns an occupied chess square containing the piece at square in board. If that square is empty, then square is returned unchanged.

Examples:
> (chess-board-ref-square starting-chess-board c8)

(occupied-chess-square

 #:file (chess-file 2)

 #:piece (colored-chess-piece #:owner #<black> #:type #<bishop>)

 #:rank (chess-rank 7))

> (chess-board-ref-square starting-chess-board e4)

#<chess-square:e4>