From 2e31a68de32184ea4c2b2cf0bab3896f93bd4702 Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Thu, 10 Jul 2025 21:48:27 -0700 Subject: [PATCH] auto-play dealer turn if not done by results() call; idiomatically handle split turns --- src/game.rs | 4 ++++ src/main.rs | 42 +++++++++++++++++------------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/game.rs b/src/game.rs index 2007af2..d877e29 100644 --- a/src/game.rs +++ b/src/game.rs @@ -211,6 +211,10 @@ impl Table { /// Get the results pub fn results(&mut self, turn: PlayerTurn) -> Result { + if self.phase == Phase::DealerTurn { + self.dealers_turn()?; + } + if self.phase != Phase::Results { return Err(BlackjackError::IncorrectAction(self.phase)); } diff --git a/src/main.rs b/src/main.rs index 1e6c48c..34a50cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,22 +89,20 @@ fn interactive_play(args: Args) -> anyhow::Result<()> { let mut turn = table.deal_hand(bet); let split_turn = interactive_play_turn(&mut turn, &mut table, args.show_count)?; - if split_turn.is_none() { - table.dealers_turn()?; - let result = table.results(turn)?; - term.clear_screen()?; - print_result(&result); - } else { - let mut split_turn = split_turn.unwrap(); - interactive_play_turn(&mut split_turn, &mut table, args.show_count)?; - table.dealers_turn()?; - let result = table.results(turn)?; - let split_result = table.results(split_turn)?; + let split_result = match split_turn { + Some(mut st) => { + interactive_play_turn(&mut st, &mut table, args.show_count)?; + Some(table.results(st)?) + } + None => None, + }; - term.clear_screen()?; - print_result(&result); + term.clear_screen()?; + let result = table.results(turn)?; + print_result(&result); + if let Some(r) = split_result { println!(); - print_result(&split_result); + print_result(&r); } table.end_game()?; @@ -382,18 +380,12 @@ fn old_man(args: Args) -> anyhow::Result<()> { { let mut turn = table.deal_hand(MIN_BET); let split_turn = basic_strategy_play_turn(&mut turn, &mut table)?; - if split_turn.is_none() { - table.dealers_turn()?; - table.results(turn)?; - // No need to handle result since we take chips off table at end of day - } else { - let mut split_turn = split_turn.unwrap(); - basic_strategy_play_turn(&mut split_turn, &mut table)?; - table.dealers_turn()?; - // No need to handle result since we take chips off table at end of day - table.results(turn)?; - table.results(split_turn)?; + if let Some(mut st) = split_turn { + basic_strategy_play_turn(&mut st, &mut table)?; + table.results(st)?; } + table.results(turn)?; + // No need to handle result since we take chips off table at end of day table.end_game()?; rounds += 1;