//! The 169 hold'em classes, their combos and the exact card-removal weights between two classes. //! //! Reference counts: 13 pairs x 6 + 78 suited x 4 + 78 offsuit x 12 = 1326 combos (any textbook); //! ordered disjoint combo pairs of two hands = 1326 x 1225. mod cfr_support; use cfr_support::id; use rechner::card::{Card, Rank}; use rechner::cfr::chance_table::chance_table; use rechner::cfr::hand_class::{ class_combos, class_exists, class_name, class_of, class_parse, classes_in, CLASS_COUNT, }; use rechner::variant::DeckRule; use std::collections::HashSet; fn card(text: &str) -> Card { Card::parse_short(text).expect("card") } #[test] fn there_are_169_distinct_classes_named_round_trip() { let names: HashSet = (0..CLASS_COUNT).filter_map(class_name).collect(); assert_eq!(names.len(), 169); for c in 0..CLASS_COUNT { let name = class_name(c).expect("name"); assert_eq!(class_parse(&name), Some(c), "{name}"); } assert_eq!(class_name(CLASS_COUNT), None); assert_eq!(class_parse("AKx"), None); assert_eq!(class_parse("AAs"), None, "pairs carry no suitedness"); assert_eq!(class_parse(""), None); } #[test] fn combo_counts_are_6_4_12_and_total_1326() { assert_eq!(class_combos(id("AA")).len(), 6); assert_eq!(class_combos(id("AKs")).len(), 4); assert_eq!(class_combos(id("AKo")).len(), 12); let total: usize = (0..CLASS_COUNT).map(|c| class_combos(c).len()).sum(); assert_eq!(total, 1326); let mut all: Vec<[Card; 2]> = (0..CLASS_COUNT).flat_map(class_combos).collect(); all.iter_mut().for_each(|h| h.sort()); let distinct: HashSet<[Card; 2]> = all.into_iter().collect(); assert_eq!(distinct.len(), 1326, "classes partition the combos"); } #[test] fn class_of_cards_is_symmetric_and_rejects_duplicates() { let (a, b) = (card("Ah"), card("Kh")); assert_eq!(class_of(a, b), Some(id("AKs"))); assert_eq!(class_of(b, a), Some(id("AKs"))); assert_eq!(class_of(card("Ah"), card("Kd")), Some(id("AKo"))); assert_eq!(class_of(card("7c"), card("7s")), Some(id("77"))); assert_eq!(class_of(a, a), None); } #[test] fn combos_belong_to_their_class() { for c in 0..CLASS_COUNT { for h in class_combos(c) { assert_eq!(class_of(h[0], h[1]), Some(c)); } } } #[test] fn short_deck_has_81_classes_and_630_combos() { let ids = classes_in(DeckRule::SHORT); assert_eq!(ids.len(), 81); assert!(ids.iter().all(|c| class_exists(DeckRule::SHORT, *c))); assert!(!class_exists(DeckRule::SHORT, id("52o"))); assert!(class_exists(DeckRule::SHORT, id("A6s"))); let combos: usize = ids .iter() .map(|c| class_combos(*c).iter().filter(|h| h.iter().all(|k| k.rank >= Rank::Six)).count()) .sum(); assert_eq!(combos, 630); assert_eq!(classes_in(DeckRule::FULL).len(), 169); } #[test] fn chance_weights_are_exact_with_card_removal() { let table = chance_table(DeckRule::FULL); // Each ordered disjoint pair of combos counts once: 1326 * 1225 in total. assert_eq!(table.total(), 1326 * 1225); let sum: f64 = (0..CLASS_COUNT) .flat_map(|i| (0..CLASS_COUNT).map(move |j| (i, j))) .map(|(i, j)| table.probability(i, j)) .sum(); assert!((sum - 1.0).abs() < 1e-12); // AA vs AA: every AA combo has exactly one disjoint partner. assert_eq!(table.disjoint_combos(id("AA"), id("AA")), 6); // AA vs KK: 6 x 6, no shared card. assert_eq!(table.disjoint_combos(id("AA"), id("KK")), 36); // AA vs AKs: AhAs leaves AdKd and AcKc; six AA combos x 2 = 12. assert_eq!(table.disjoint_combos(id("AA"), id("AKs")), 12); for (a, b) in [("AKo", "QQ"), ("T9s", "98s"), ("72o", "AA")] { assert_eq!(table.disjoint_combos(id(a), id(b)), table.disjoint_combos(id(b), id(a))); } } #[test] fn short_deck_chance_weights_total_630_x_561() { let table = chance_table(DeckRule::SHORT); assert_eq!(table.total(), 630 * 561); assert_eq!(table.disjoint_combos(id("52o"), id("AA")), 0, "absent class has no weight"); }