diff --git a/src/game.rs b/src/game.rs index bd6ecb9..b4995b3 100644 --- a/src/game.rs +++ b/src/game.rs @@ -52,6 +52,9 @@ pub struct Game { shoe: Vec, /// Discard pile. When the shoe runs out, this gets mixed with the shoe and reshuffled. discard: Arc>>, + /// The current card count, see: https://en.wikipedia.org/wiki/Blackjack#Card_counting + count: i16, + hit_on_soft_17: bool, /// Returns when hitting blackjack @@ -69,6 +72,7 @@ impl Game { rng: rand::rng(), shoe: Vec::new(), discard: Arc::new(RwLock::new(Vec::new())), + count: 0, } } @@ -108,7 +112,7 @@ impl Game { player_decision: F, ) -> EndState where - F: Fn(&Hand, &Card) -> PlayerChoice, + F: Fn(&Hand, &Card, i16) -> PlayerChoice, { // Shuffle the deck if we've played over 75% of cards let discard_size = self.discard.read().unwrap().len(); @@ -148,7 +152,7 @@ impl Game { // Player turn loop { - let decision = player_decision(&player_hand, &dealer_hand.cards()[0]); + let decision = player_decision(&player_hand, &dealer_hand.cards()[0], self.count); match decision { PlayerChoice::Stand => break, @@ -226,7 +230,15 @@ impl Game { /// Deal a single card from the shoe fn deal(self: &mut Self) -> Card { - self.shoe.pop().unwrap() + let card = self.shoe.pop().unwrap(); + + if u8::from(&card) < 6 { + self.count += 1; + } else if u8::from(&card) >= 10 { + self.count -= 1; + } + + card } /// Reset the shoe - combine shoe and discard and shuffle @@ -235,5 +247,6 @@ impl Game { self.shoe.append(&mut discard); assert_eq!(discard.len(), 0); self.shoe.shuffle(&mut self.rng); + self.count = 0; } } diff --git a/src/main.rs b/src/main.rs index 3aeb43b..6e9f777 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ fn main() { } fn interactive_play() { - // TODO: Persist bank between plays // TODO: Make a way to reset bank // let term = Term::stdout(); @@ -93,12 +92,13 @@ fn interactive_play() { } } -fn interactive_decision(hand: &Hand, dealer_showing: &Card) -> PlayerChoice { +fn interactive_decision(hand: &Hand, dealer_showing: &Card, _count: i16) -> PlayerChoice { let term = Term::stdout(); term.clear_screen().unwrap(); println!("Dealer showing: {dealer_showing}"); - println!("Your hand: {hand}\n"); + println!("Your hand: {hand}"); + println!(); if hand.value() == 21 { println!("You have 21, standing."); @@ -127,7 +127,7 @@ fn interactive_decision(hand: &Hand, dealer_showing: &Card) -> PlayerChoice { choice } -fn basic_strategy(hand: &Hand, dealer_showing: &Card) -> PlayerChoice { +fn basic_strategy(hand: &Hand, dealer_showing: &Card, _count: i16) -> PlayerChoice { // Source: https://en.wikipedia.org/wiki/Blackjack#Basic_strategy let dv = u8::from(dealer_showing); let can_dd = hand.cards().len() == 2;