Skip to main content

Crate chess_startpos_rs

Crate chess_startpos_rs 

Source
Expand description

Generate, count, and sample chess back-rank arrangements under composable constraints (Chess960, Chess2880, custom presets).

The crate is generic — parameterised over a user-defined piece kind, a user-defined colour set, and board size — and built around a small set of composable constraint primitives with And / Or / Not combinators. An opinionated chess module ships ready-to-use presets for the canonical chess shuffle variants (Chess960, Chess-2880, full shuffle).

§Mental model

  • pieces is the alphabet — the set of distinct kinds available. Duplicate entries are silently deduplicated.
  • num_squares is the board size.
  • square_colors labels each square with a colour from the user-defined colour set. Generic; defaults to SquareColor.
  • Constraints filter the arrangements you want. The solver enumerates length-num_squares sequences over the alphabet and keeps the ones satisfying the root constraint.

The constraint vocabulary is Count, CountOnColor, At, NotAt, Order, and Relative, composable with And / Or / Not. See Constraint for each variant’s semantics.

§Quick start — chess presets

use chess_startpos_rs::chess;

assert_eq!(chess::shuffle().count(), 5040);
assert_eq!(chess::chess_2880().count(), 2880);
assert_eq!(chess::chess_960().count(), 960);

§Custom piece kinds, boards, and colours

use chess_startpos_rs::{alternating, Constraint, CountOp, Problem, SquareColor};

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
enum Tile { A, B }

let problem: Problem<Tile> = Problem {
    num_squares: 3,
    square_colors: alternating(3, SquareColor::Light, SquareColor::Dark),
    pieces: vec![Tile::A, Tile::B],                // alphabet
    constraint: Constraint::And(vec![
        Constraint::Count { piece: Tile::A, op: CountOp::Eq, value: 2 },
        Constraint::Count { piece: Tile::B, op: CountOp::Eq, value: 1 },
        Constraint::At { piece: Tile::B, square: 1 },
    ]),
};
assert_eq!(problem.count(), 1);
assert_eq!(problem.at(0), Some(vec![Tile::A, Tile::B, Tile::A]));

Modules§

chess
Opinionated chess preset module — batteries included for the canonical 8-square back-rank arrangements.

Structs§

Problem
A constraint-satisfaction problem: a fixed board (size + per-square colour from a user-defined colour set), an alphabet of available piece kinds, and a constraint to satisfy.
ProblemBuilder
Fluent builder for Problem.

Enums§

Constraint
A single constraint over an arrangement of pieces.
CountOp
Comparison operator for count constraints.
SquareColor
Colour of a square — the default binary partition used by chess (and the default type parameter C for Constraint / crate::Problem).
ValidationError
Reasons a Problem can fail validate.

Traits§

ColorKind
Marker trait for colour kinds usable as the type parameter of Problem / Constraint.
PieceKind
Marker trait for piece kinds usable as the type parameter of Problem / Constraint.

Functions§

alternating
Returns a Vec<C> of length n alternating between first and second, starting with first.
uniform
Returns a Vec<C> of length n where every entry is c.