Don't deal cards to dealer if player has busted
This commit is contained in:
parent
2e31a68de3
commit
673dc50ec6
2 changed files with 22 additions and 12 deletions
|
|
@ -196,15 +196,17 @@ impl Table {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run the dealer's turn and finish the game
|
/// Run the dealer's turn and finish the game
|
||||||
pub fn dealers_turn(&mut self) -> Result<(), BlackjackError> {
|
pub fn dealers_turn(&mut self, turn: &PlayerTurn) -> Result<(), BlackjackError> {
|
||||||
if self.phase != Phase::DealerTurn {
|
if self.phase != Phase::DealerTurn {
|
||||||
return Err(BlackjackError::IncorrectAction(self.phase));
|
return Err(BlackjackError::IncorrectAction(self.phase));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if turn.hand.value() <= 21 {
|
||||||
while self.dealer_hand.value() < 17 {
|
while self.dealer_hand.value() < 17 {
|
||||||
let dealer_card = self.deal_card();
|
let dealer_card = self.deal_card();
|
||||||
self.dealer_hand.push(dealer_card);
|
self.dealer_hand.push(dealer_card);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.phase = Phase::Results;
|
self.phase = Phase::Results;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -212,7 +214,7 @@ impl Table {
|
||||||
/// Get the results
|
/// Get the results
|
||||||
pub fn results(&mut self, turn: PlayerTurn) -> Result<EndState, BlackjackError> {
|
pub fn results(&mut self, turn: PlayerTurn) -> Result<EndState, BlackjackError> {
|
||||||
if self.phase == Phase::DealerTurn {
|
if self.phase == Phase::DealerTurn {
|
||||||
self.dealers_turn()?;
|
self.dealers_turn(&turn)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.phase != Phase::Results {
|
if self.phase != Phase::Results {
|
||||||
|
|
@ -407,7 +409,7 @@ mod tests {
|
||||||
) -> anyhow::Result<EndState> {
|
) -> anyhow::Result<EndState> {
|
||||||
let (mut table, mut turn) = setup_hands(bet, dealer_cards, player_cards);
|
let (mut table, mut turn) = setup_hands(bet, dealer_cards, player_cards);
|
||||||
table.stand(&mut turn)?;
|
table.stand(&mut turn)?;
|
||||||
table.dealers_turn()?;
|
table.dealers_turn(&turn)?;
|
||||||
|
|
||||||
Ok(table.results(turn)?)
|
Ok(table.results(turn)?)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
src/main.rs
20
src/main.rs
|
|
@ -49,7 +49,11 @@ fn interactive_play(args: Args) -> anyhow::Result<()> {
|
||||||
println!("\nMoney in the bank: ${bank}");
|
println!("\nMoney in the bank: ${bank}");
|
||||||
// TODO: show normalized card count
|
// TODO: show normalized card count
|
||||||
if args.show_count {
|
if args.show_count {
|
||||||
println!("Card count: {}", table.count());
|
println!(
|
||||||
|
"Card count: {} ({})",
|
||||||
|
table.count(),
|
||||||
|
table.count() / args.decks as i16
|
||||||
|
);
|
||||||
println!("Cards in shoe: {}", table.shoe_count());
|
println!("Cards in shoe: {}", table.shoe_count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,10 +92,10 @@ fn interactive_play(args: Args) -> anyhow::Result<()> {
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
let mut turn = table.deal_hand(bet);
|
let mut turn = table.deal_hand(bet);
|
||||||
let split_turn = interactive_play_turn(&mut turn, &mut table, args.show_count)?;
|
let split_turn = interactive_play_turn(&mut turn, &mut table, &args)?;
|
||||||
let split_result = match split_turn {
|
let split_result = match split_turn {
|
||||||
Some(mut st) => {
|
Some(mut st) => {
|
||||||
interactive_play_turn(&mut st, &mut table, args.show_count)?;
|
interactive_play_turn(&mut st, &mut table, &args)?;
|
||||||
Some(table.results(st)?)
|
Some(table.results(st)?)
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
|
|
@ -122,7 +126,7 @@ fn interactive_play(args: Args) -> anyhow::Result<()> {
|
||||||
fn interactive_play_turn(
|
fn interactive_play_turn(
|
||||||
turn: &mut PlayerTurn,
|
turn: &mut PlayerTurn,
|
||||||
table: &mut Table,
|
table: &mut Table,
|
||||||
show_count: bool,
|
args: &Args,
|
||||||
) -> anyhow::Result<Option<PlayerTurn>> {
|
) -> anyhow::Result<Option<PlayerTurn>> {
|
||||||
let mut initial_play = !turn.was_split;
|
let mut initial_play = !turn.was_split;
|
||||||
let mut other_turn = None;
|
let mut other_turn = None;
|
||||||
|
|
@ -135,8 +139,12 @@ fn interactive_play_turn(
|
||||||
if turn.shuffled {
|
if turn.shuffled {
|
||||||
println!("Deck was shuffled");
|
println!("Deck was shuffled");
|
||||||
}
|
}
|
||||||
if show_count {
|
if args.show_count {
|
||||||
println!("Card count: {}", table.count());
|
println!(
|
||||||
|
"Card count: {} ({})",
|
||||||
|
table.count(),
|
||||||
|
table.count() / args.decks as i16
|
||||||
|
);
|
||||||
}
|
}
|
||||||
println!("Your bet: ${}", turn.bet);
|
println!("Your bet: ${}", turn.bet);
|
||||||
println!("Dealer showing: {}", table.dealer_showing());
|
println!("Dealer showing: {}", table.dealer_showing());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue