checkers

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2026 License: MIT Imports: 1 Imported by: 0

README

checkers

A pure-Go American checkers (English draughts) rules engine — no protocol, no I/O, standard library only.

LegalMoves generates every legal move as a path of squares; captures are forced (if any jump exists, only jump paths are returned), multi-jumps extend until no further jump is possible, and promotion ends a jump sequence. Validate checks a submitted path by membership, and Winner reports a loss for the side with no legal move — ideal for both-sides-validate multiplayer where neither client is trusted.

b := checkers.Start()
moves := checkers.LegalMoves(b, checkers.Black) // Black moves first
if err := checkers.Validate(b, checkers.Black, moves[0]); err == nil {
    b = checkers.Apply(b, checkers.Black, moves[0])
}
if winner, over := checkers.Winner(b, checkers.White); over {
    _ = winner
}

Board is the 32 dark squares in PDN order; a Move is a path ([from, to] for a slide, [from, land1, land2, …] for a jump chain).

Install

go get github.com/richardwooding/checkers

Extracted from kibitz.

License

MIT

Documentation

Overview

Package checkers is a pure-Go American checkers (English draughts) rules engine — no protocol, no I/O, standard library only.

Extracted from https://github.com/richardwooding/kibitz

Squares are the 32 dark squares, 0-indexed in PDN order: square 0 is the top-left dark square (Black's back row), row-major. Black sits on rows 0–2 (squares 0–11) and moves DOWN (increasing rows); White sits on rows 5–7 (squares 20–31) and moves up. Black moves first.

Captures are forced: if any jump exists, only jump paths are legal, and a jump continues while the same piece can keep jumping. Promotion mid-path ends the move (a new king may not continue the jump sequence).

Index

Constants

This section is empty.

Variables

View Source
var ErrIllegalMove = errors.New("checkers: illegal move")

Functions

func Validate

func Validate(b Board, side Side, m Move) error

Validate checks a move by membership in the legal set.

Types

type Board

type Board [32]int8

Cell values: 0 empty; +1 black man, +2 black king; -1 white man, -2 white king. (Positive = Black = P1, mirroring "P1 moves first".)

func Apply

func Apply(b Board, side Side, m Move) Board

Apply replays a (validated) move and returns the new board.

func Start

func Start() Board

Start is the standard opening position.

type Move

type Move []int8

Move is a path of square indices: [from, to] for a simple move, or [from, over₁-landing, over₂-landing, …] for a jump sequence.

func LegalMoves

func LegalMoves(b Board, side Side) []Move

LegalMoves generates all legal moves for side. If any capture exists, only (complete) capture paths are returned.

type Side

type Side uint8

Side: 0 = Black (P1), 1 = White (P2).

const (
	Black Side = 0
	White Side = 1
)

func Winner

func Winner(b Board, toMove Side) (Side, bool)

Winner: the side to move loses when it has no legal moves (no pieces or fully blocked). Returns (winner, true) in that case.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL