From d34a72cf3ba0cc467006f1e743a3d85fd4b5def6 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Wed, 22 May 2024 14:07:04 +0200 Subject: [PATCH] moved split check to hand --- src/blackjack.rs | 37 +++++++++++++++---------------------- src/hand.rs | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/blackjack.rs b/src/blackjack.rs index 9988c3d..a9b4076 100644 --- a/src/blackjack.rs +++ b/src/blackjack.rs @@ -152,29 +152,22 @@ impl BlackjackGame { return false; } - if let Some(card_0) = hand.hand.get_card(0) { - if let Some(card_1) = hand.hand.get_card(1) { - if card_0.index.get_blackjack_value(true) - != card_1.index.get_blackjack_value(true) - { - // Cards are not the same value - return false; - } - - // Split the hands - - let mut new_hand = PlayingHand::new(); - - // Add card from the current hand and a card from the shoe - new_hand.hand.add_card(hand.hand.pop_card().unwrap()); - new_hand.hand.add_card(self.shoe.pop_card().unwrap()); - - // Add card to current hand - hand.hand.add_card(self.shoe.pop_card().unwrap()); - - player.hands.push(new_hand); - } + if !hand.hand.is_valid_for_bj_split() { + return false; } + + // Split the hands + + let mut new_hand = PlayingHand::new(); + + // Add card from the current hand and a card from the shoe + new_hand.hand.add_card(hand.hand.pop_card().unwrap()); + new_hand.hand.add_card(self.shoe.pop_card().unwrap()); + + // Add card to current hand + hand.hand.add_card(self.shoe.pop_card().unwrap()); + + player.hands.push(new_hand); } (GameState::PlayerTurn(player_index, hand_index), PlayMoves::DoubleDown) => { let Some(player) = self.players.get_mut(*player_index) else { diff --git a/src/hand.rs b/src/hand.rs index ea2d75a..273f4ca 100644 --- a/src/hand.rs +++ b/src/hand.rs @@ -40,6 +40,27 @@ impl Hand { self.cards.len() == 2 && self.get_blackjack_value() == 21 } + /** + * Returns true if this hand could be split in a blackjack game. + */ + pub fn is_valid_for_bj_split(&self) -> bool { + if self.cards.len() != 2 { + return false; + } + + if let Some(card_0) = self.get_card(0) { + if let Some(card_1) = self.get_card(1) { + if card_0.index.get_blackjack_value(true) != card_1.index.get_blackjack_value(true) + { + // Cards are not the same value + return false; + } + } + } + + true + } + /** * Put another hand into this. * Leaves the other hand empty.