added split
This commit is contained in:
parent
338b5d442a
commit
6c21eb960f
@ -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,7 +230,6 @@ impl BlackjackGame {
|
||||
}
|
||||
(GameState::Over, PlayMoves::Deal(player_count))
|
||||
| (GameState::Starting, PlayMoves::Deal(player_count)) => {
|
||||
|
||||
// Create players
|
||||
for _ in 0..player_count {
|
||||
self.players.push(Player::new());
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ pub fn play() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
||||
}
|
||||
GameState::Starting => {
|
||||
game.play(PlayMoves::Deal(2));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_move(player_index: usize) -> Result<PlayMoves, io::Error> {
|
||||
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<PlayMoves, io::Error> {
|
||||
"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() {
|
||||
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!(
|
||||
"{}: {} ({}) - {:?}",
|
||||
hand.0,
|
||||
hand.1.get_hand(),
|
||||
hand.1.get_hand().get_blackjack_value(),
|
||||
hand.1.get_state()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user