From 673dc50ec6a6d1ca08fb154268def13af5553827 Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Fri, 11 Jul 2025 16:54:10 -0700 Subject: [PATCH 1/2] Don't deal cards to dealer if player has busted --- src/game.rs | 14 ++++++++------ src/main.rs | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/game.rs b/src/game.rs index d877e29..84d4d54 100644 --- a/src/game.rs +++ b/src/game.rs @@ -196,14 +196,16 @@ impl Table { } /// Run the dealer's turn and finish the game - pub fn dealers_turn(&mut self) -> Result<(), BlackjackError> { + pub fn dealers_turn(&mut self, turn: &PlayerTurn) -> Result<(), BlackjackError> { if self.phase != Phase::DealerTurn { return Err(BlackjackError::IncorrectAction(self.phase)); } - while self.dealer_hand.value() < 17 { - let dealer_card = self.deal_card(); - self.dealer_hand.push(dealer_card); + if turn.hand.value() <= 21 { + while self.dealer_hand.value() < 17 { + let dealer_card = self.deal_card(); + self.dealer_hand.push(dealer_card); + } } self.phase = Phase::Results; Ok(()) @@ -212,7 +214,7 @@ impl Table { /// Get the results pub fn results(&mut self, turn: PlayerTurn) -> Result { if self.phase == Phase::DealerTurn { - self.dealers_turn()?; + self.dealers_turn(&turn)?; } if self.phase != Phase::Results { @@ -407,7 +409,7 @@ mod tests { ) -> anyhow::Result { let (mut table, mut turn) = setup_hands(bet, dealer_cards, player_cards); table.stand(&mut turn)?; - table.dealers_turn()?; + table.dealers_turn(&turn)?; Ok(table.results(turn)?) } diff --git a/src/main.rs b/src/main.rs index 34a50cf..6820099 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,11 @@ fn interactive_play(args: Args) -> anyhow::Result<()> { println!("\nMoney in the bank: ${bank}"); // TODO: show normalized card count if args.show_count { - println!("Card count: {}", table.count()); + println!( + "Card count: {} ({})", + table.count(), + table.count() / args.decks as i16 + ); println!("Cards in shoe: {}", table.shoe_count()); } @@ -88,10 +92,10 @@ fn interactive_play(args: Args) -> anyhow::Result<()> { println!(); let mut turn = table.deal_hand(bet); - let split_turn = interactive_play_turn(&mut turn, &mut table, args.show_count)?; + let split_turn = interactive_play_turn(&mut turn, &mut table, &args)?; let split_result = match split_turn { Some(mut st) => { - interactive_play_turn(&mut st, &mut table, args.show_count)?; + interactive_play_turn(&mut st, &mut table, &args)?; Some(table.results(st)?) } None => None, @@ -122,7 +126,7 @@ fn interactive_play(args: Args) -> anyhow::Result<()> { fn interactive_play_turn( turn: &mut PlayerTurn, table: &mut Table, - show_count: bool, + args: &Args, ) -> anyhow::Result> { let mut initial_play = !turn.was_split; let mut other_turn = None; @@ -135,8 +139,12 @@ fn interactive_play_turn( if turn.shuffled { println!("Deck was shuffled"); } - if show_count { - println!("Card count: {}", table.count()); + if args.show_count { + println!( + "Card count: {} ({})", + table.count(), + table.count() / args.decks as i16 + ); } println!("Your bet: ${}", turn.bet); println!("Dealer showing: {}", table.dealer_showing()); From bf5f289720a34e1b618739da2b10bd34f0448877 Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Fri, 11 Jul 2025 16:59:09 -0700 Subject: [PATCH 2/2] both having blackjack should be a tie --- src/game.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/game.rs b/src/game.rs index 84d4d54..405f5d5 100644 --- a/src/game.rs +++ b/src/game.rs @@ -228,12 +228,12 @@ impl Table { returns: 0, }; #[allow(clippy::if_same_then_else)] - if self.dealer_hand.is_blackjack() { - end_state.result = PlayResult::DealerBlackjack; - } else if turn.hand.is_blackjack() { + if turn.hand.is_blackjack() && !self.dealer_hand.is_blackjack() { end_state.result = PlayResult::Blackjack; - let bj_winnings = (turn.bet as f32 * self.blackjack_returns) as u32; - end_state.returns = turn.bet + bj_winnings; + let winnings = (turn.bet as f32 * self.blackjack_returns) as u32; + end_state.returns = turn.bet + winnings; + } else if !turn.hand.is_blackjack() && self.dealer_hand.is_blackjack() { + end_state.result = PlayResult::DealerBlackjack; } else if turn.hand.value() > 21 { end_state.result = PlayResult::Bust; } else if self.dealer_hand.value() > 21 { @@ -494,8 +494,8 @@ mod tests { Vec::from([("♣", "A"), ("♣", "10")]), ) .unwrap(); - assert_eq!(result.result, PlayResult::DealerBlackjack); - assert_eq!(result.returns, 0); + assert_eq!(result.result, PlayResult::Push); + assert_eq!(result.returns, 10); } #[test]