pub struct ProblemBuilder<P: PieceKind, C: ColorKind> { /* private fields */ }Expand description
Fluent builder for Problem.
Construct via Problem::builder. Every method consumes self
and returns the updated builder, so calls chain. Finalise with
ProblemBuilder::build.
The builder is an alternative to direct struct-literal
construction of Problem; both produce the same value.
use chess_startpos_rs::{Constraint, CountOp, Problem, SquareColor};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
enum Card { Ace, King, Queen }
let problem: Problem<Card> = Problem::builder()
.squares(6)
.alternating_colors(SquareColor::Light, SquareColor::Dark)
.pieces([Card::Ace, Card::King, Card::Queen])
.constraint(Constraint::Count { piece: Card::Ace, op: CountOp::Eq, value: 2 })
.constraint(Constraint::Count { piece: Card::King, op: CountOp::Eq, value: 2 })
.constraint(Constraint::Count { piece: Card::Queen, op: CountOp::Eq, value: 2 })
.build();
assert_eq!(problem.count(), 90); // 6! / (2! · 2! · 2!)Implementations§
Source§impl<P: PieceKind, C: ColorKind> ProblemBuilder<P, C>
impl<P: PieceKind, C: ColorKind> ProblemBuilder<P, C>
Sourcepub fn colors(self, colors: Vec<C>) -> Self
pub fn colors(self, colors: Vec<C>) -> Self
Sets square_colors to colors. The slice’s length should
match num_squares; mismatch isn’t a build-time error but
will produce empty results from the solver.
Sourcepub fn alternating_colors(self, first: C, second: C) -> Selfwhere
C: Copy,
pub fn alternating_colors(self, first: C, second: C) -> Selfwhere
C: Copy,
Sets square_colors to a length-num_squares sequence
alternating between first (even indices) and second (odd
indices). Call after .squares(n); otherwise produces an
empty colour vector.
Sourcepub fn uniform_colors(self, c: C) -> Selfwhere
C: Copy,
pub fn uniform_colors(self, c: C) -> Selfwhere
C: Copy,
Sets square_colors to a length-num_squares sequence with
every square coloured c. Call after .squares(n).
Sourcepub fn colors_fn<F>(self, f: F) -> Self
pub fn colors_fn<F>(self, f: F) -> Self
Sets square_colors to (0..num_squares).map(f).collect().
The closure receives each 0-based square index and returns
its colour. Call after .squares(n).
use chess_startpos_rs::{Problem, SquareColor};
// 6-square board split into halves: first 3 Light, last 3 Dark.
let problem: Problem<u8> = Problem::builder()
.squares(6)
.colors_fn(|i| if i < 3 { SquareColor::Light } else { SquareColor::Dark })
.build();
assert_eq!(problem.square_colors.len(), 6);Sourcepub fn pieces<I: IntoIterator<Item = P>>(self, alphabet: I) -> Self
pub fn pieces<I: IntoIterator<Item = P>>(self, alphabet: I) -> Self
Replaces the alphabet with alphabet. Duplicates are silently
deduplicated by the solver.
Sourcepub fn constraint(self, c: Constraint<P, C>) -> Self
pub fn constraint(self, c: Constraint<P, C>) -> Self
Appends a constraint. Multiple calls AND-compose at
build time.
Sourcepub fn build(self) -> Problem<P, C>
pub fn build(self) -> Problem<P, C>
Finalises into a Problem without validating it. The
accumulated constraints are wrapped in Constraint::And (a
single constraint is stored bare; zero constraints become
And(vec![]), which is always satisfied).
To check internal consistency before solving, use
try_build instead, or call
Problem::validate on the returned value.
Sourcepub fn try_build(self) -> Result<Problem<P, C>, ValidationError>
pub fn try_build(self) -> Result<Problem<P, C>, ValidationError>
Like build but runs Problem::validate on
the result, returning the validation error instead of the
problem if it fails.
use chess_startpos_rs::{chess, Problem, SquareColor, ValidationError};
// Builder forgot to call `.alternating_colors(...)` after
// `.squares(8)`; `square_colors` ends up length 0.
// That's allowed iff no constraint references colours — but
// here we omit colours *and* don't add any constraints, so
// it's fine. Now break it by mismatching the colour vec:
let bad: Result<Problem<chess::Piece>, _> = Problem::builder()
.squares(8)
.colors(vec![SquareColor::Light]) // length 1, not 8
.pieces([chess::Piece::King])
.try_build();
assert!(matches!(
bad,
Err(ValidationError::ColorLengthMismatch { expected: 8, actual: 1 }),
));Trait Implementations§
Source§impl<P: Clone + PieceKind, C: Clone + ColorKind> Clone for ProblemBuilder<P, C>
impl<P: Clone + PieceKind, C: Clone + ColorKind> Clone for ProblemBuilder<P, C>
Source§fn clone(&self) -> ProblemBuilder<P, C>
fn clone(&self) -> ProblemBuilder<P, C>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more