add card count
This commit is contained in:
parent
821d2114d5
commit
dfdbf72188
2 changed files with 20 additions and 7 deletions
19
src/game.rs
19
src/game.rs
|
|
@ -52,6 +52,9 @@ pub struct Game {
|
||||||
shoe: Vec<Card>,
|
shoe: Vec<Card>,
|
||||||
/// Discard pile. When the shoe runs out, this gets mixed with the shoe and reshuffled.
|
/// Discard pile. When the shoe runs out, this gets mixed with the shoe and reshuffled.
|
||||||
discard: Arc<RwLock<Vec<Card>>>,
|
discard: Arc<RwLock<Vec<Card>>>,
|
||||||
|
/// The current card count, see: https://en.wikipedia.org/wiki/Blackjack#Card_counting
|
||||||
|
count: i16,
|
||||||
|
|
||||||
hit_on_soft_17: bool,
|
hit_on_soft_17: bool,
|
||||||
|
|
||||||
/// Returns when hitting blackjack
|
/// Returns when hitting blackjack
|
||||||
|
|
@ -69,6 +72,7 @@ impl Game {
|
||||||
rng: rand::rng(),
|
rng: rand::rng(),
|
||||||
shoe: Vec::new(),
|
shoe: Vec::new(),
|
||||||
discard: Arc::new(RwLock::new(Vec::new())),
|
discard: Arc::new(RwLock::new(Vec::new())),
|
||||||
|
count: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,7 +112,7 @@ impl Game {
|
||||||
player_decision: F,
|
player_decision: F,
|
||||||
) -> EndState
|
) -> EndState
|
||||||
where
|
where
|
||||||
F: Fn(&Hand, &Card) -> PlayerChoice,
|
F: Fn(&Hand, &Card, i16) -> PlayerChoice,
|
||||||
{
|
{
|
||||||
// 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();
|
||||||
|
|
@ -148,7 +152,7 @@ impl Game {
|
||||||
|
|
||||||
// Player turn
|
// Player turn
|
||||||
loop {
|
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 {
|
match decision {
|
||||||
PlayerChoice::Stand => break,
|
PlayerChoice::Stand => break,
|
||||||
|
|
@ -226,7 +230,15 @@ impl Game {
|
||||||
|
|
||||||
/// Deal a single card from the shoe
|
/// Deal a single card from the shoe
|
||||||
fn deal(self: &mut Self) -> Card {
|
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
|
/// Reset the shoe - combine shoe and discard and shuffle
|
||||||
|
|
@ -235,5 +247,6 @@ impl Game {
|
||||||
self.shoe.append(&mut discard);
|
self.shoe.append(&mut discard);
|
||||||
assert_eq!(discard.len(), 0);
|
assert_eq!(discard.len(), 0);
|
||||||
self.shoe.shuffle(&mut self.rng);
|
self.shoe.shuffle(&mut self.rng);
|
||||||
|
self.count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn interactive_play() {
|
fn interactive_play() {
|
||||||
// TODO: Persist bank between plays
|
|
||||||
// TODO: Make a way to reset bank
|
// TODO: Make a way to reset bank
|
||||||
//
|
//
|
||||||
let term = Term::stdout();
|
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();
|
let term = Term::stdout();
|
||||||
term.clear_screen().unwrap();
|
term.clear_screen().unwrap();
|
||||||
|
|
||||||
println!("Dealer showing: {dealer_showing}");
|
println!("Dealer showing: {dealer_showing}");
|
||||||
println!("Your hand: {hand}\n");
|
println!("Your hand: {hand}");
|
||||||
|
println!();
|
||||||
|
|
||||||
if hand.value() == 21 {
|
if hand.value() == 21 {
|
||||||
println!("You have 21, standing.");
|
println!("You have 21, standing.");
|
||||||
|
|
@ -127,7 +127,7 @@ fn interactive_decision(hand: &Hand, dealer_showing: &Card) -> PlayerChoice {
|
||||||
choice
|
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
|
// Source: https://en.wikipedia.org/wiki/Blackjack#Basic_strategy
|
||||||
let dv = u8::from(dealer_showing);
|
let dv = u8::from(dealer_showing);
|
||||||
let can_dd = hand.cards().len() == 2;
|
let can_dd = hand.cards().len() == 2;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue