added split

This commit is contained in:
Niklas Kapelle 2024-05-21 16:48:42 +02:00
parent 338b5d442a
commit 6c21eb960f
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
2 changed files with 80 additions and 25 deletions

View File

@ -64,6 +64,7 @@ pub enum PlayMoves {
Hit, Hit,
Stand, Stand,
DoubleDown, DoubleDown,
Split,
Deal(usize), 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) => { (GameState::PlayerTurn(player_index, hand_index), PlayMoves::DoubleDown) => {
let Some(player) = self.players.get_mut(*player_index) else { let Some(player) = self.players.get_mut(*player_index) else {
// Player does not exists // Player does not exists
@ -184,7 +230,6 @@ impl BlackjackGame {
} }
(GameState::Over, PlayMoves::Deal(player_count)) (GameState::Over, PlayMoves::Deal(player_count))
| (GameState::Starting, PlayMoves::Deal(player_count)) => { | (GameState::Starting, PlayMoves::Deal(player_count)) => {
// Create players // Create players
for _ in 0..player_count { for _ in 0..player_count {
self.players.push(Player::new()); self.players.push(Player::new());
@ -233,7 +278,7 @@ impl BlackjackGame {
self.dealer_turn(); self.dealer_turn();
} }
return true; true
} }
fn dealer_turn(&mut self) { fn dealer_turn(&mut self) {
@ -278,13 +323,12 @@ impl BlackjackGame {
} }
// Get next valid player // Get next valid player
if let Some(next_hand) = self.players.iter().enumerate().find_map(|p| { if let Some(next_hand) = self
if let Some(h) = p.1.next_playing_hand(){ .players
Some((p.0,h)) .iter()
}else{ .enumerate()
None .find_map(|p| p.1.next_playing_hand().map(|h| (p.0, h)))
} {
}) {
return Some((next_hand.0, next_hand.1)); return Some((next_hand.0, next_hand.1));
} }
} }

View File

@ -13,7 +13,7 @@ pub fn play() -> Result<(), Box<dyn std::error::Error>> {
if !game.play(play_move) { if !game.play(play_move) {
println!("You can't do that"); println!("You can't do that");
} }
}, }
GameState::Over => { GameState::Over => {
print_full_state(&game); print_full_state(&game);
println!("Game over"); println!("Game over");
@ -21,14 +21,14 @@ pub fn play() -> Result<(), Box<dyn std::error::Error>> {
} }
GameState::Starting => { GameState::Starting => {
game.play(PlayMoves::Deal(2)); game.play(PlayMoves::Deal(2));
}, }
} }
} }
} }
fn get_move(player_index: usize) -> Result<PlayMoves, io::Error> { fn get_move(player_index: usize) -> Result<PlayMoves, io::Error> {
loop { 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(); let mut buffer = String::new();
stdin().read_line(&mut buffer)?; stdin().read_line(&mut buffer)?;
@ -37,19 +37,30 @@ fn get_move(player_index: usize) -> Result<PlayMoves, io::Error> {
"h" | "H" => return Ok(PlayMoves::Hit), "h" | "H" => return Ok(PlayMoves::Hit),
"s" | "S" => return Ok(PlayMoves::Stand), "s" | "S" => return Ok(PlayMoves::Stand),
"d" | "D" => return Ok(PlayMoves::DoubleDown), "d" | "D" => return Ok(PlayMoves::DoubleDown),
"p" | "P" => return Ok(PlayMoves::Split),
_ => {} _ => {}
} }
} }
} }
fn print_full_state(game: &BlackjackGame) { 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(); let player = game.get_player(player_index).unwrap();
println!("Player {}:", player_index); println!("Player {}:", player_index);
for hand in player.get_hands().iter().enumerate() { 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!(
"{}: {} ({}) - {:?}",
hand.0,
hand.1.get_hand(),
hand.1.get_hand().get_blackjack_value(),
hand.1.get_state()
);
} }
} }
} }