From 4387e2cb14a1a98186ee196ef4f1596aa31088a6 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Mon, 13 May 2024 18:07:41 +0200 Subject: [PATCH] double down --- src/blackjack.rs | 14 ++++++++++++-- src/console_blackjack.rs | 11 ++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/blackjack.rs b/src/blackjack.rs index 3156629..8bcaef1 100644 --- a/src/blackjack.rs +++ b/src/blackjack.rs @@ -10,7 +10,7 @@ pub struct BlackjackGame { pub enum PlayResult { DealerBlackJack, // Dealer has a blackjack. Player does not PlayerBlackJack, // Player has a blackjack. Dealer does not - PushBlackjack, // Player and dealer have a blockjack + PushBlackjack, // Player and dealer have a blackjack Push, // Dealer has a Blackjack and so does the Player Bust, // Player bust DealerBust, // Dealer bust @@ -24,6 +24,7 @@ pub enum PlayResult { pub enum PlayMoves { Hit, Stand, + DoubleDown, Bet, // At the Start of the game } @@ -97,7 +98,16 @@ impl BlackjackGame { } std::cmp::Ordering::Less => PlayResult::Continue, }; - } + }, + (GameState::PlayerTurn, PlayMoves::DoubleDown) => { + if self.player_hand.count() > 2{ + return PlayResult::InvalidMove; + } + + self.player_hand.add_card(self.shoe.pop_card().unwrap()); + self.state = GameState::Over; + return self.dealer_play(); + }, (GameState::PlayerTurn, PlayMoves::Stand) => { self.state = GameState::Over; return self.dealer_play(); diff --git a/src/console_blackjack.rs b/src/console_blackjack.rs index bb8c3aa..2f075ea 100644 --- a/src/console_blackjack.rs +++ b/src/console_blackjack.rs @@ -12,12 +12,16 @@ pub fn play() -> Result<(), Box> { print_full_state(&game); println!("Dealer wins with blackjack!"); } - crate::blackjack::PlayResult::PlayerBlackJack => println!("Player has a blackjack!"), + crate::blackjack::PlayResult::PlayerBlackJack => { + print_full_state(&game); + println!("Player has a blackjack!"); + }, crate::blackjack::PlayResult::PushBlackjack => { print_full_state(&game); println!("Player and Dealer have a blackjack!"); } crate::blackjack::PlayResult::DealerBust => { + print_full_state(&game); println!("Dealer busts. Player wins!"); } crate::blackjack::PlayResult::StandPlayerLose => { @@ -36,7 +40,7 @@ pub fn play() -> Result<(), Box> { } crate::blackjack::PlayResult::Push => { print_full_state(&game); - println!("Player and dealer have the same value"); + println!("Player and dealer have the same value. Push!"); } crate::blackjack::PlayResult::Bust => { print_full_state(&game); @@ -66,7 +70,7 @@ pub fn play() -> Result<(), Box> { fn get_move() -> Result { loop { - println!("(H)it (S)tand"); + println!("(H)it (S)tand (D)double"); let mut buffer = String::new(); stdin().read_line(&mut buffer)?; @@ -74,6 +78,7 @@ fn get_move() -> Result { match buffer.trim() { "h" | "H" => return Ok(PlayMoves::Hit), "s" | "S" => return Ok(PlayMoves::Stand), + "d" | "D" => return Ok(PlayMoves::DoubleDown), _ => {} } }