compute_hot_deals/
main.rs

1use dds_bridge::contract::Strain;
2use dds_bridge::deal::{Card, Deal, Hand, Seat, SmallSet as _, Suit};
3use dds_bridge::solver;
4use std::process::ExitCode;
5
6/// Generate a hand with one card of each rank
7fn get_random_average_hand(rng: &mut (impl rand::Rng + ?Sized)) -> Hand {
8    let bits: u64 = rng.random();
9
10    (0..13).fold(Hand::EMPTY, |mut hand, i| {
11        // SAFETY: we are slicing valid consecutive 2 bits from `bits`
12        let suit: Suit = unsafe { core::mem::transmute((bits >> (2 * i) & 3) as u8) };
13        hand.insert(Card::new(suit, i + 2));
14        hand
15    })
16}
17
18fn get_random_symmetric_deal(rng: &mut (impl rand::Rng + ?Sized)) -> Deal {
19    let hand = get_random_average_hand(rng);
20    let sequence = [
21        hand.0[1], hand.0[2], hand.0[3], hand.0[0], hand.0[1], hand.0[2],
22    ];
23    let north = hand;
24    let east = Hand(sequence[0..4].try_into().expect("Invalid hand"));
25    let south = Hand(sequence[1..5].try_into().expect("Invalid hand"));
26    let west = Hand(sequence[2..6].try_into().expect("Invalid hand"));
27    Deal([north, east, south, west])
28}
29
30fn compute_deal(
31    rng: &mut (impl rand::Rng + ?Sized),
32) -> Result<(Deal, solver::TricksTable), solver::Error> {
33    const N: usize = dds_bridge_sys::MAXNOOFTABLES as usize;
34    loop {
35        let deals: [_; N] = core::array::from_fn(|_| get_random_symmetric_deal(rng));
36        // SAFETY: `N` is exactly the maximum length of a deal segment.
37        let tables = unsafe { solver::solve_deal_segment(&deals, solver::StrainFlags::all())? };
38
39        for (i, &table) in tables.results[..N].iter().enumerate() {
40            let tricks = solver::TricksTable::from(table);
41            let pars = solver::calculate_pars(tricks, solver::Vulnerability::all())?;
42
43            if pars[0].score + pars[1].score > 0 {
44                return Ok((deals[i], tricks));
45            }
46        }
47    }
48}
49
50#[doc = include_str!("README.md")]
51fn main() -> Result<ExitCode, solver::Error> {
52    let deals = match std::env::args().nth(1) {
53        Some(string) => {
54            if let Ok(n) = string.parse::<usize>() {
55                n
56            } else {
57                eprintln!("{}", include_str!("README.md"));
58                return Ok(ExitCode::FAILURE);
59            }
60        }
61        None => 1,
62    };
63
64    for _ in 0..deals {
65        let (deal, tricks) = compute_deal(&mut rand::rng())?;
66        println!(
67            "{} {:X}",
68            deal.display(Seat::North),
69            tricks.hex(Seat::North, Strain::SYS)
70        );
71    }
72
73    Ok(ExitCode::SUCCESS)
74}