From c114094285b02993bfb343d0329cf2b6b89349d8 Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Fri, 4 Jul 2025 16:51:13 -0700 Subject: [PATCH] add old man simulation --- src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/main.rs b/src/main.rs index e13268f..b5ebca8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,9 @@ use std::io; use std::io::prelude::*; fn main() { + // TODO: CLI flags to choose how to play interactive_play(); + // old_man(); } fn interactive_play() { @@ -102,6 +104,62 @@ fn interactive_decision(hand: &Hand, dealer_showing: &Card) -> PlayerChoice { choice } +fn basic_strategy(hand: &Hand, dealer_showing: &Card) -> PlayerChoice { + // Source: https://en.wikipedia.org/wiki/Blackjack#Basic_strategy + // TODO: Add doubling when it's supported + let dealer_val = u8::from(dealer_showing); + if hand.has_ace() { + match hand.value() { + v if v >= 19 => PlayerChoice::Stand, + v if v == 18 && dealer_val < 9 => PlayerChoice::Stand, + _ => PlayerChoice::Hit, + } + } else { + match hand.value() { + v if v >= 17 => PlayerChoice::Stand, + v if v > 12 && dealer_val < 7 => PlayerChoice::Stand, + v if v == 12 && (4..=6).contains(&dealer_val) => PlayerChoice::Stand, + _ => PlayerChoice::Hit, + } + } +} + +/// An cab driver to the LAS airport was a former dealer and told me about this retired guy who +/// would come play Blackjack every day at 7am like clockwork. He'd bring $400 to the table, play +/// basic strategy, and would walk away once he was $100 up. He made about $3000/mo off of this, in +/// addition to points/comps which he would use to eat brunch for free. +/// +/// Does this actually work, or was the driver full of it? +fn old_man() { + let mut bank: u64 = 100_000; + const PER_DAY: u32 = 400; + const MIN_BET: u32 = 15; + // Walk away when we're up by this much + const WALK_AWAY: u32 = 100; + const DAYS: u8 = 30; + + println!("Starting with bank: ${bank}"); + // Let's simulate this for 30 days + for day in 1..=DAYS { + let mut in_hand: u32 = PER_DAY; + bank -= in_hand as u64; + + let mut game = Game::new().with_decks(6); + let mut rounds = 0; + while in_hand > MIN_BET && in_hand < (PER_DAY + WALK_AWAY) { + let bet = MIN_BET; + in_hand -= bet; + let result = game.play(bet, basic_strategy); + in_hand += u32::try_from(result.player_winnings).unwrap(); + rounds += 1; + } + println!("Day {day}, after {rounds} rounds, in-hand: ${in_hand}"); + bank += in_hand as u64; + } + + println!("Ending with: ${bank}"); +} + fn read_input() -> String { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap();