bet retries on invalid input

This commit is contained in:
Nick Pegg 2025-07-04 14:37:29 -07:00
parent 0a72da768e
commit 10d00c8ef5
2 changed files with 25 additions and 2 deletions

View file

@ -29,6 +29,7 @@ pub struct EndState {
pub player_winnings: u64, pub player_winnings: u64,
pub dealer_cards: Vec<Card>, pub dealer_cards: Vec<Card>,
pub player_cards: Vec<Card>, pub player_cards: Vec<Card>,
pub shuffled: bool,
} }
impl EndState { impl EndState {
@ -37,12 +38,14 @@ impl EndState {
player_winnings: u64, player_winnings: u64,
dealer_cards: Vec<Card>, dealer_cards: Vec<Card>,
player_cards: Vec<Card>, player_cards: Vec<Card>,
shuffled: bool,
) -> Self { ) -> Self {
Self { Self {
result, result,
player_winnings, player_winnings,
dealer_cards, dealer_cards,
player_cards, player_cards,
shuffled,
} }
} }
} }
@ -107,8 +110,10 @@ impl Game {
{ {
// Shuffle the deck if we've played over 75% of cards // Shuffle the deck if we've played over 75% of cards
let discard_size = self.discard.read().unwrap().len(); let discard_size = self.discard.read().unwrap().len();
let mut shuffled = false;
if self.shoe.len() < (self.shoe.len() + discard_size) * 1 / 4 { if self.shoe.len() < (self.shoe.len() + discard_size) * 1 / 4 {
self.shuffle(); self.shuffle();
shuffled = true;
} }
// Deal cards // Deal cards
@ -124,6 +129,7 @@ impl Game {
0, 0,
dealer_hand.cards(), dealer_hand.cards(),
player_hand.cards(), player_hand.cards(),
shuffled,
); );
} else if player_hand.is_blackjack() { } else if player_hand.is_blackjack() {
let returns = bet as f32 * self.blackjack_returns; let returns = bet as f32 * self.blackjack_returns;
@ -132,6 +138,7 @@ impl Game {
returns as u64, returns as u64,
dealer_hand.cards(), dealer_hand.cards(),
player_hand.cards(), player_hand.cards(),
shuffled,
); );
} }
@ -153,6 +160,7 @@ impl Game {
0, 0,
dealer_hand.cards(), dealer_hand.cards(),
player_hand.cards(), player_hand.cards(),
shuffled,
); );
} }
} }
@ -168,6 +176,7 @@ impl Game {
(bet * 2).into(), (bet * 2).into(),
dealer_hand.cards(), dealer_hand.cards(),
player_hand.cards(), player_hand.cards(),
shuffled,
) )
} else if dealer_hand.value() < player_hand.value() { } else if dealer_hand.value() < player_hand.value() {
EndState::new( EndState::new(
@ -175,6 +184,7 @@ impl Game {
(bet * 2).into(), (bet * 2).into(),
dealer_hand.cards(), dealer_hand.cards(),
player_hand.cards(), player_hand.cards(),
shuffled,
) )
} else if dealer_hand.value() == player_hand.value() { } else if dealer_hand.value() == player_hand.value() {
EndState::new( EndState::new(
@ -182,6 +192,7 @@ impl Game {
bet.into(), bet.into(),
dealer_hand.cards(), dealer_hand.cards(),
player_hand.cards(), player_hand.cards(),
shuffled,
) )
} else { } else {
EndState::new( EndState::new(
@ -189,6 +200,7 @@ impl Game {
0, 0,
dealer_hand.cards(), dealer_hand.cards(),
player_hand.cards(), player_hand.cards(),
shuffled,
) )
} }
} }

View file

@ -9,6 +9,8 @@ fn main() {
} }
fn interactive_play() { fn interactive_play() {
// TODO: Persist bank between plays
// TODO: Make a way to reset bank
let mut bank: u64 = 1_000; let mut bank: u64 = 1_000;
let mut game = Game::new().with_decks(6); let mut game = Game::new().with_decks(6);
let mut last_bet: Option<u32> = None; let mut last_bet: Option<u32> = None;
@ -26,12 +28,18 @@ fn interactive_play() {
if input == "" { if input == "" {
bet = last_bet.unwrap(); bet = last_bet.unwrap();
} else { } else {
bet = input.parse().unwrap(); match input.parse() {
Ok(b) => bet = b,
Err(_) => continue,
}
} }
} else { } else {
print!("Your bet? "); print!("Your bet? ");
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
bet = read_input().parse().unwrap(); match read_input().parse() {
Ok(b) => bet = b,
Err(_) => continue,
}
} }
if bet as u64 <= bank { if bet as u64 <= bank {
break; break;
@ -58,6 +66,9 @@ fn interactive_play() {
PlayResult::DealerBlackjack => println!("Dealer got blackjack"), PlayResult::DealerBlackjack => println!("Dealer got blackjack"),
PlayResult::Bust => println!("You busted"), PlayResult::Bust => println!("You busted"),
} }
if result.shuffled {
println!("Deck was shuffled at beginning of round");
}
bank += result.player_winnings; bank += result.player_winnings;
} }