Hearts
This crate models the mechanics of Hearts: strongly typed cards and tricks, and a rules-driven state machine for complete four-player rounds and games — passing, the 2♣ opening, following suit, breaking hearts, and shooting the moon.
Hearts ranks the ace HIGH: the A♥ beats the K♥. Rank therefore encodes
2 = 2 through A = 14, matching crates for ace-high games such as
contract-bridge and unlike the ace-low gin-rummy; these crates share
patterns, never code.
Hands are written as four dot-separated suit groups in ascending order,
clubs first: "23A.456.789.T" holds ♣2 ♣3 ♣A ♦4 ♦5 ♦6 ♥7 ♥8 ♥9 ♠10.
Modules
hand: cards and card sets —Rank,Card,Holding,Hand(plusSuitat the crate root)seat: the four seats and the pass rotation —Seat,PassDirectiontrick: four cards in play order —Trickround: one deal from the pass to the last trick —Round,Phase,RoundResultgame: the scoreboard across deals —Game,FinalScorerules: game configuration —Rulesdeck(featurerand): shuffled dealing —Deck
Feature flags
rand: shuffled dealing (Deck,Round::deal,Game::deal)serde: serialization for the game-state types (cards, hands, seats, tricks, rounds, results, games); a mid-gameRoundsnapshot is validated on deserialization by replaying its recorded history through the state machine
Quick start
Playing a fixed deal needs no features:
use ;
// The sorted deck dealt round-robin; a hold deal skips the passing phase.
let mut hands = ;
for in ALL.into_iter.enumerate
let mut round = from_deal?;
while let Some = round.turn
let result = round.result.unwrap;
assert_eq!;
# Ok::
A complete bot-vs-bot game with the rand feature:
#
#
#
#
Examples
playout: narrate one fixed deal —cargo run --example playoutsimulate: greedy bots play a full game —cargo run --features rand --example simulate