Compare commits
No commits in common. "bf5f289720a34e1b618739da2b10bd34f0448877" and "2e31a68de32184ea4c2b2cf0bab3896f93bd4702" have entirely different histories.
bf5f289720
...
2e31a68de3
2 changed files with 19 additions and 29 deletions
28
src/game.rs
28
src/game.rs
|
|
@ -196,16 +196,14 @@ impl Table {
|
|||
}
|
||||
|
||||
/// Run the dealer's turn and finish the game
|
||||
pub fn dealers_turn(&mut self, turn: &PlayerTurn) -> Result<(), BlackjackError> {
|
||||
pub fn dealers_turn(&mut self) -> Result<(), BlackjackError> {
|
||||
if self.phase != Phase::DealerTurn {
|
||||
return Err(BlackjackError::IncorrectAction(self.phase));
|
||||
}
|
||||
|
||||
if turn.hand.value() <= 21 {
|
||||
while self.dealer_hand.value() < 17 {
|
||||
let dealer_card = self.deal_card();
|
||||
self.dealer_hand.push(dealer_card);
|
||||
}
|
||||
while self.dealer_hand.value() < 17 {
|
||||
let dealer_card = self.deal_card();
|
||||
self.dealer_hand.push(dealer_card);
|
||||
}
|
||||
self.phase = Phase::Results;
|
||||
Ok(())
|
||||
|
|
@ -214,7 +212,7 @@ impl Table {
|
|||
/// Get the results
|
||||
pub fn results(&mut self, turn: PlayerTurn) -> Result<EndState, BlackjackError> {
|
||||
if self.phase == Phase::DealerTurn {
|
||||
self.dealers_turn(&turn)?;
|
||||
self.dealers_turn()?;
|
||||
}
|
||||
|
||||
if self.phase != Phase::Results {
|
||||
|
|
@ -228,12 +226,12 @@ impl Table {
|
|||
returns: 0,
|
||||
};
|
||||
#[allow(clippy::if_same_then_else)]
|
||||
if turn.hand.is_blackjack() && !self.dealer_hand.is_blackjack() {
|
||||
end_state.result = PlayResult::Blackjack;
|
||||
let winnings = (turn.bet as f32 * self.blackjack_returns) as u32;
|
||||
end_state.returns = turn.bet + winnings;
|
||||
} else if !turn.hand.is_blackjack() && self.dealer_hand.is_blackjack() {
|
||||
if self.dealer_hand.is_blackjack() {
|
||||
end_state.result = PlayResult::DealerBlackjack;
|
||||
} else if turn.hand.is_blackjack() {
|
||||
end_state.result = PlayResult::Blackjack;
|
||||
let bj_winnings = (turn.bet as f32 * self.blackjack_returns) as u32;
|
||||
end_state.returns = turn.bet + bj_winnings;
|
||||
} else if turn.hand.value() > 21 {
|
||||
end_state.result = PlayResult::Bust;
|
||||
} else if self.dealer_hand.value() > 21 {
|
||||
|
|
@ -409,7 +407,7 @@ mod tests {
|
|||
) -> anyhow::Result<EndState> {
|
||||
let (mut table, mut turn) = setup_hands(bet, dealer_cards, player_cards);
|
||||
table.stand(&mut turn)?;
|
||||
table.dealers_turn(&turn)?;
|
||||
table.dealers_turn()?;
|
||||
|
||||
Ok(table.results(turn)?)
|
||||
}
|
||||
|
|
@ -494,8 +492,8 @@ mod tests {
|
|||
Vec::from([("♣", "A"), ("♣", "10")]),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(result.result, PlayResult::Push);
|
||||
assert_eq!(result.returns, 10);
|
||||
assert_eq!(result.result, PlayResult::DealerBlackjack);
|
||||
assert_eq!(result.returns, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
20
src/main.rs
20
src/main.rs
|
|
@ -49,11 +49,7 @@ fn interactive_play(args: Args) -> anyhow::Result<()> {
|
|||
println!("\nMoney in the bank: ${bank}");
|
||||
// TODO: show normalized card count
|
||||
if args.show_count {
|
||||
println!(
|
||||
"Card count: {} ({})",
|
||||
table.count(),
|
||||
table.count() / args.decks as i16
|
||||
);
|
||||
println!("Card count: {}", table.count());
|
||||
println!("Cards in shoe: {}", table.shoe_count());
|
||||
}
|
||||
|
||||
|
|
@ -92,10 +88,10 @@ fn interactive_play(args: Args) -> anyhow::Result<()> {
|
|||
println!();
|
||||
|
||||
let mut turn = table.deal_hand(bet);
|
||||
let split_turn = interactive_play_turn(&mut turn, &mut table, &args)?;
|
||||
let split_turn = interactive_play_turn(&mut turn, &mut table, args.show_count)?;
|
||||
let split_result = match split_turn {
|
||||
Some(mut st) => {
|
||||
interactive_play_turn(&mut st, &mut table, &args)?;
|
||||
interactive_play_turn(&mut st, &mut table, args.show_count)?;
|
||||
Some(table.results(st)?)
|
||||
}
|
||||
None => None,
|
||||
|
|
@ -126,7 +122,7 @@ fn interactive_play(args: Args) -> anyhow::Result<()> {
|
|||
fn interactive_play_turn(
|
||||
turn: &mut PlayerTurn,
|
||||
table: &mut Table,
|
||||
args: &Args,
|
||||
show_count: bool,
|
||||
) -> anyhow::Result<Option<PlayerTurn>> {
|
||||
let mut initial_play = !turn.was_split;
|
||||
let mut other_turn = None;
|
||||
|
|
@ -139,12 +135,8 @@ fn interactive_play_turn(
|
|||
if turn.shuffled {
|
||||
println!("Deck was shuffled");
|
||||
}
|
||||
if args.show_count {
|
||||
println!(
|
||||
"Card count: {} ({})",
|
||||
table.count(),
|
||||
table.count() / args.decks as i16
|
||||
);
|
||||
if show_count {
|
||||
println!("Card count: {}", table.count());
|
||||
}
|
||||
println!("Your bet: ${}", turn.bet);
|
||||
println!("Dealer showing: {}", table.dealer_showing());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue