From 6c21eb960fb64313d8889e6f2cf9c5bc1a7d3021 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Tue, 21 May 2024 16:48:42 +0200 Subject: [PATCH] added split --- src/blackjack.rs | 74 ++++++++++++++++++++++++++++++++-------- src/console_blackjack.rs | 31 +++++++++++------ 2 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/blackjack.rs b/src/blackjack.rs index d038c27..9988c3d 100644 --- a/src/blackjack.rs +++ b/src/blackjack.rs @@ -64,6 +64,7 @@ pub enum PlayMoves { Hit, Stand, DoubleDown, + Split, Deal(usize), } @@ -130,6 +131,51 @@ impl BlackjackGame { } }; } + (GameState::PlayerTurn(player_index, hand_index), PlayMoves::Split) => { + let Some(player) = self.players.get_mut(*player_index) else { + // Player does not exists + return false; + }; + + let Some(hand) = player.hands.get_mut(*hand_index) else { + // Hand does not exist + return false; + }; + + if hand.state != HandState::Playing { + // Hand is not playing + return false; + } + + if hand.hand.count() != 2 { + // Can only split with 2 cards + 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); + } + } + } (GameState::PlayerTurn(player_index, hand_index), PlayMoves::DoubleDown) => { let Some(player) = self.players.get_mut(*player_index) else { // Player does not exists @@ -184,20 +230,19 @@ impl BlackjackGame { } (GameState::Over, PlayMoves::Deal(player_count)) | (GameState::Starting, PlayMoves::Deal(player_count)) => { - // Create players - for _ in 0..player_count{ + for _ in 0..player_count { self.players.push(Player::new()); } // Create one hand for the players - for player in self.players.iter_mut(){ + for player in self.players.iter_mut() { player.hands.push(PlayingHand::new()); } // Add one card to each hand the players have - for player in self.players.iter_mut(){ - for hand in player.hands.iter_mut(){ + for player in self.players.iter_mut() { + for hand in player.hands.iter_mut() { hand.hand.add_card(self.shoe.pop_card().unwrap()); } } @@ -206,8 +251,8 @@ impl BlackjackGame { self.dealer_hand.add_card(self.shoe.pop_card().unwrap()); // Add the 2nd card to the players hand - for player in self.players.iter_mut(){ - for hand in player.hands.iter_mut(){ + for player in self.players.iter_mut() { + for hand in player.hands.iter_mut() { hand.hand.add_card(self.shoe.pop_card().unwrap()); if hand.hand.is_backjack() { @@ -233,7 +278,7 @@ impl BlackjackGame { self.dealer_turn(); } - return true; + true } fn dealer_turn(&mut self) { @@ -278,13 +323,12 @@ impl BlackjackGame { } // Get next valid player - if let Some(next_hand) = self.players.iter().enumerate().find_map(|p| { - if let Some(h) = p.1.next_playing_hand(){ - Some((p.0,h)) - }else{ - None - } - }) { + if let Some(next_hand) = self + .players + .iter() + .enumerate() + .find_map(|p| p.1.next_playing_hand().map(|h| (p.0, h))) + { return Some((next_hand.0, next_hand.1)); } } diff --git a/src/console_blackjack.rs b/src/console_blackjack.rs index 14a7df7..5075f56 100644 --- a/src/console_blackjack.rs +++ b/src/console_blackjack.rs @@ -7,13 +7,13 @@ pub fn play() -> Result<(), Box> { loop { match game.get_state() { - GameState::PlayerTurn(player_index,_) => { + GameState::PlayerTurn(player_index, _) => { print_full_state(&game); let play_move = get_move(*player_index)?; - if !game.play(play_move){ + if !game.play(play_move) { println!("You can't do that"); } - }, + } GameState::Over => { print_full_state(&game); println!("Game over"); @@ -21,14 +21,14 @@ pub fn play() -> Result<(), Box> { } GameState::Starting => { game.play(PlayMoves::Deal(2)); - }, + } } } } fn get_move(player_index: usize) -> Result { loop { - println!("Turn {}: (H)it (S)tand (D)double",player_index); + println!("Turn {}: (H)it (S)tand (D)double, S(p)lit", player_index); let mut buffer = String::new(); stdin().read_line(&mut buffer)?; @@ -37,19 +37,30 @@ fn get_move(player_index: usize) -> Result { "h" | "H" => return Ok(PlayMoves::Hit), "s" | "S" => return Ok(PlayMoves::Stand), "d" | "D" => return Ok(PlayMoves::DoubleDown), + "p" | "P" => return Ok(PlayMoves::Split), _ => {} } } } fn print_full_state(game: &BlackjackGame) { - println!("Dealer: {} ({})", game.get_dealer_hand(), game.get_dealer_hand().get_blackjack_value()); + println!( + "Dealer: {} ({})", + game.get_dealer_hand(), + game.get_dealer_hand().get_blackjack_value() + ); - for player_index in 0..game.get_player_count(){ + for player_index in 0..game.get_player_count() { let player = game.get_player(player_index).unwrap(); - println!("Player {}:",player_index); - for hand in player.get_hands().iter().enumerate(){ - println!("{}: {} ({}) - {:?}",hand.0,hand.1.get_hand(),hand.1.get_hand().get_blackjack_value(),hand.1.get_state()); + println!("Player {}:", player_index); + for hand in player.get_hands().iter().enumerate() { + println!( + "{}: {} ({}) - {:?}", + hand.0, + hand.1.get_hand(), + hand.1.get_hand().get_blackjack_value(), + hand.1.get_state() + ); } } }