//! Extension field `boardDouble`: a second board in the shape of `board`. //! //! The hand is judged on both boards and the better judgement wins (higher equity). //! Documented approximation: the pot is split between the boards, so the exact pot share //! is the mean of both equities - taking the better one is optimistic (see MODULES.md). mod support; use rechner::wire::respond::respond; use support::{action, board_json, cards_json, confidence, field, short}; /// Hero 7h7d: a set on `SET_BOARD`, an underpair below three broadway cards on `UNDER_BOARD`. const POCKET_SEVENS: &str = "7h 7d"; const SET_BOARD: &str = "7c Ks 2d"; const UNDER_BOARD: &str = "Kc Qs Jd"; /// Named reference values (one opponent with the default range, fixed seed): /// a set on a dry flop is far ahead, an underpair below K-Q-J is far behind. const SET_EQUITY_AT_LEAST: f64 = 0.80; const UNDERPAIR_EQUITY_AT_MOST: f64 = 0.45; fn request(hero: &str, board: &str, double: Option<&str>, to_call: f64, legal: &str, extra: &str) -> String { let cards = short(board); let street = match cards.len() { 0 => "PREFLOP", 3 => "FLOP", 4 => "TURN", _ => "RIVER", }; let double = double.map_or(String::new(), |b| format!(r#","boardDouble":{}"#, board_json(&short(b)))); format!( r#"{{"variant":"NLHE","bigBlind":0.1,"seats":6,"heroSeat":5,"buttonSeat":5,"heroCards":{},"board":{},"street":"{street}","pot":1.2,"toCall":{to_call},"heroStack":6.0,"minRaiseTo":0.8,"maxRaiseTo":6.0,"legalActions":{legal},"villains":[{{"seat":2,"stack":10.4,"folded":false}}]{double}{extra}}}"#, cards_json(&short(hero)), board_json(&cards), ) } fn equity(out: &str) -> f64 { field(out, "equity").and_then(|s| s.parse().ok()).unwrap_or_else(|| panic!("no equity: {out}")) } const FACING_BET: &str = r#"["FOLD","CALL","RAISE"]"#; #[test] fn named_references_hold_for_each_board_alone() { let set = respond(&request(POCKET_SEVENS, SET_BOARD, None, 0.4, FACING_BET, "")); let under = respond(&request(POCKET_SEVENS, UNDER_BOARD, None, 0.4, FACING_BET, "")); assert!(equity(&set) >= SET_EQUITY_AT_LEAST, "{set}"); assert!(equity(&under) <= UNDERPAIR_EQUITY_AT_MOST, "{under}"); } #[test] fn one_board_improves_the_hand_the_other_does_not_the_better_judgement_wins() { let set = respond(&request(POCKET_SEVENS, SET_BOARD, None, 0.4, FACING_BET, "")); let both = respond(&request(POCKET_SEVENS, UNDER_BOARD, Some(SET_BOARD), 0.4, FACING_BET, "")); assert_eq!(equity(&both), equity(&set), "{both}"); assert_eq!(action(&both), action(&set), "{both}"); assert_eq!(field(&both, "category"), field(&set, "category"), "{both}"); } #[test] fn the_order_of_the_boards_does_not_matter() { let a = respond(&request(POCKET_SEVENS, SET_BOARD, Some(UNDER_BOARD), 0.4, FACING_BET, "")); let b = respond(&request(POCKET_SEVENS, UNDER_BOARD, Some(SET_BOARD), 0.4, FACING_BET, "")); assert_eq!(equity(&a), equity(&b)); assert_eq!(action(&a), action(&b)); } #[test] fn the_reason_names_the_board_and_the_approximation() { let out = respond(&request(POCKET_SEVENS, UNDER_BOARD, Some(SET_BOARD), 0.4, FACING_BET, "")); let reason = field(&out, "reason").unwrap_or(""); assert!(reason.contains("double board"), "{out}"); assert!(reason.contains("board 2"), "the second board was the better one: {out}"); assert!(reason.contains("pot split"), "{out}"); } #[test] fn works_on_turn_and_river_too() { for (first, second) in [("7c Ks 2d 3h", "Kc Qs Jd 9h"), ("7c Ks 2d 3h 4s", "Kc Qs Jd 9h 8c")] { let alone = respond(&request(POCKET_SEVENS, first, None, 0.4, FACING_BET, "")); let both = respond(&request(POCKET_SEVENS, second, Some(first), 0.4, FACING_BET, "")); assert_eq!(equity(&both), equity(&alone), "{both}"); } } #[test] fn a_card_on_both_boards_or_in_hand_is_refused() { for double in ["7c Qs Jd", "7d Qs Jd"] { let out = respond(&request(POCKET_SEVENS, SET_BOARD, Some(double), 0.4, FACING_BET, "")); assert_eq!(action(&out), "ABSTAIN", "{double}: {out}"); assert_eq!(confidence(&out), 0.0); } } #[test] fn a_second_board_of_another_length_is_refused() { let out = respond(&request(POCKET_SEVENS, SET_BOARD, Some("Kc Qs Jd 9h"), 0.4, FACING_BET, "")); assert_eq!(action(&out), "ABSTAIN", "{out}"); assert!(field(&out, "reason").unwrap_or("").contains("boardDouble"), "{out}"); } #[test] fn a_malformed_second_board_is_refused_with_its_path() { let line = request(POCKET_SEVENS, SET_BOARD, None, 0.4, FACING_BET, r#","boardDouble":{"FLOP":[{"rank":"X","suit":"CLUBS"}]}"#); let out = respond(&line); assert_eq!(action(&out), "ABSTAIN", "{out}"); assert!(field(&out, "reason").unwrap_or("").contains("boardDouble.FLOP"), "{out}"); } #[test] fn null_means_no_second_board() { let plain = respond(&request(POCKET_SEVENS, SET_BOARD, None, 0.4, FACING_BET, "")); let null = respond(&request(POCKET_SEVENS, SET_BOARD, None, 0.4, FACING_BET, r#","boardDouble":null"#)); assert_eq!(plain, null); } #[test] fn the_answer_stays_legal() { let check_only = r#"["CHECK","RAISE"]"#; let out = respond(&request(POCKET_SEVENS, UNDER_BOARD, Some(SET_BOARD), 0.0, check_only, "")); assert!(["CHECK", "RAISE", "ABSTAIN"].contains(&action(&out).as_str()), "{out}"); }