Skip to main content

Crate dealbreaker

Crate dealbreaker 

Source
Expand description

§dealbreaker

A solver for patience (solitaire) card games, where a game is data, not code.

Describe a game with Rules: its pack, its groups of piles and what each accepts and lets go, how the stock deals, the opening deal, any card-removal mechanic, and the win condition. Game::new compiles the rules; solve searches a position for a win, or proves there is none.

use dealbreaker::{presets, solve, Game, Outcome, SolveOptions};

let game = Game::new(presets::freecell()).unwrap();
let deal = game.deal(42);
let result = solve(&game, &deal, &SolveOptions::default());
if let Outcome::Solved(moves) = &result.outcome {
    for step in game.steps(&deal, moves) {
        println!("{step}");
    }
}

The presets module has 50 built-in games written in the same model, and every preset round-trips through JSON, so rules can live in files.

§What the solver assumes

  • It sees every card, including face-down ones: it solves the deal (“thoughtful” solitaire). Use hint() for advice that doesn’t peek.
  • “Unsolvable” means every distinct reachable position was searched. Positions are compared by 128-bit hash, so a false “unsolvable” needs a hash collision.
  • Automatic actions (turning up exposed cards, refilling from a reserve) happen after every move, as the rules say.

Re-exports§

pub use card::Card;
pub use card::Color;
pub use card::Suit;
pub use game::Action;
pub use game::FlippedCard;
pub use game::Game;
pub use game::IllegalMove;
pub use game::IllegalStep;
pub use game::Move;
pub use game::PileRef;
pub use game::RulesError;
pub use game::State;
pub use game::Step;
pub use game::StepDetails;
pub use grade::finalize_batch;
pub use grade::find_deal;
pub use grade::find_deal_with_cancel;
pub use grade::grade;
pub use grade::grade_batch;
pub use grade::grade_batch_with_cancel;
pub use grade::grade_with_cancel;
pub use grade::BatchGrade;
pub use grade::DealGrade;
pub use grade::DealMetrics;
pub use grade::Difficulty;
pub use grade::DifficultyCutoffs;
pub use grade::FindDealOptions;
pub use grade::FoundDeal;
pub use grade::GameDifficultySummary;
pub use grade::GradeOptions;
pub use hint::hint;
pub use hint::hint_with_cancel;
pub use hint::hints;
pub use hint::hints_with_cancel;
pub use hint::Hint;
pub use hint::HintOptions;
pub use hint::HintReport;
pub use hooks::Hooks;
pub use io::microsoft_deal;
pub use io::StateData;
pub use io::StateError;
pub use solver::autocomplete;
pub use solver::last_winnable;
pub use solver::last_winnable_with_cancel;
pub use solver::maximize;
pub use solver::maximize_with_cancel;
pub use solver::safe_steps;
pub use solver::solve;
pub use solver::solve_with_cancel;
pub use solver::AutoComplete;
pub use solver::LastWinnable;
pub use solver::MaxResult;
pub use solver::Outcome;
pub use solver::SolveOptions;
pub use solver::SolveResult;
pub use solver::Strategy;
pub use rules::*;

Modules§

card
Cards, suits and colours.
game
The engine: rules compiled into lookup tables, positions, legal moves, and reversible play.
grade
Deal difficulty grading and game-level difficulty calibration.
hint
Hints that respect hidden cards.
hooks
An escape hatch for rules the data model can’t express.
io
Positions as data: a JSON-friendly form, validation, and well-known deal numbering.
presets
Ready-made rules for 50 well-known games, stored as compact JSON in presets/*.json.
rng
Deterministic shuffling, so a seed always names the same deal on every platform.
rules
The rules model: a game is described as data.
solver
Search.