casino 0.3.0

A casino built right into your terminal
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use crate::cards::{Card, Rank, Shoe};
use crate::money::Money;
use crate::Casino;
use anyhow::Result;
use anyhow::{bail, Context};
use colored::*;
use inquire::{Confirm, Select, Text};
use num::rational::Ratio;
use serde::{Deserialize, Serialize};
use spinners::{Spinner, Spinners};
use std::fmt;
use std::thread::sleep;
use std::time::Duration;

#[derive(Clone, Debug, Default)]
pub struct Hand {
    pub cards: Vec<Card>,
    pub hidden_count: usize,
    pub standing: bool,
    pub doubling_down: bool,
}

impl Hand {
    pub fn new() -> Self {
        Hand::default()
    }

    pub fn new_hidden(hidden_count: usize) -> Self {
        Hand {
            hidden_count,
            ..Default::default()
        }
    }

    pub fn push(&mut self, card: Card) {
        self.cards.push(card);
    }

    pub fn face_card(&self) -> &Card {
        &self.cards[1]
    }

    pub fn is_natural_blackjack(&self) -> bool {
        self.cards.len() == 2 && self.blackjack_sum() == 21
    }

    pub fn blackjack_sum(&self) -> u8 {
        let mut sum = 0;
        for card in self.cards.iter() {
            sum += card.blackjack_value();
        }

        let has_ace = self.cards.iter().any(|c| matches!(&c.rank, Rank::Ace));

        if has_ace && sum <= 11 {
            sum += 10;
        }

        sum
    }

    pub fn is_bust(&self) -> bool {
        self.blackjack_sum() > 21
    }

    pub fn can_double_down(&self) -> bool {
        let player_sum = self.blackjack_sum();
        self.cards.len() == 2 && !self.doubling_down && (player_sum == 10 || player_sum == 11)
    }

    pub fn can_split(&self) -> bool {
        self.cards.len() == 2 && self.cards[0].rank == self.cards[1].rank
    }

    pub fn split(&mut self) -> Hand {
        let moved_card = self.cards.pop().expect("Hand needs cards to split!");

        let mut other_hand = Hand::default();
        other_hand.push(moved_card);

        other_hand
    }

    pub fn is_finished(&self) -> bool {
        self.standing || self.blackjack_sum() > 21
    }
}

impl fmt::Display for Hand {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut hand_str: String = "".to_owned();

        for (i, card) in self.cards.iter().enumerate() {
            if i < self.hidden_count {
                hand_str.push_str("🂠 ");
            } else {
                hand_str.push_str(&card.to_string());
            }
        }

        hand_str.push_str(" (");

        for (i, card) in self.cards.iter().enumerate() {
            if i < self.hidden_count {
                hand_str.push('?');
            } else {
                hand_str.push_str(&card.blackjack_value().to_string());
            }

            if i < self.cards.len() - 1 {
                hand_str.push_str(" + ");
            }
        }

        if self.hidden_count > 0 {
            hand_str.push_str(" = ?)");
        } else {
            hand_str.push_str(&format!(" = {})", self.blackjack_sum()));
        }

        write!(f, "{hand_str}")
    }
}

pub trait BlackjackValue {
    fn blackjack_value(&self) -> u8;
}

impl BlackjackValue for Card {
    fn blackjack_value(&self) -> u8 {
        match &self.rank {
            Rank::Ace => 1,
            Rank::Two => 2,
            Rank::Three => 3,
            Rank::Four => 4,
            Rank::Five => 5,
            Rank::Six => 6,
            Rank::Seven => 7,
            Rank::Eight => 8,
            Rank::Nine => 9,
            Rank::Ten | Rank::Jack | Rank::Queen | Rank::King => 10,
        }
    }
}

#[derive(Debug, Default)]
pub struct Blackjack {
    config: BlackjackConfig,
    shoe: Shoe,
    dealer_hand: Hand,
    player_hands: Vec<Hand>,
    bet: Money,
    insurance: bool,
    splitting: bool,
    doubling_down: bool,
    current_hand: usize,
}

impl Blackjack {
    pub fn new(config: BlackjackConfig) -> Self {
        Self {
            config: config.clone(),
            shoe: Shoe::new(config.shoe_count, config.shuffle_at_penetration),
            dealer_hand: Hand::new_hidden(1),
            player_hands: vec![Hand::new()],
            bet: Money::ZERO,
            insurance: false,
            splitting: false,
            doubling_down: false,
            current_hand: 0,
        }
    }

    pub fn set_shoe(&mut self, shoe: Shoe) {
        self.shoe = shoe;
    }

    pub fn set_bet(&mut self, bet: Money) {
        assert!(bet.is_sign_positive());
        assert!(!bet.is_zero());

        self.bet = bet;
    }

    pub fn current_hand_index(&self) -> usize {
        self.current_hand
    }

    pub fn current_player_hand(&self) -> &Hand {
        &self.player_hands[self.current_hand]
    }

    pub fn card_to_dealer(&mut self) {
        let card = self.shoe.draw_card();
        self.dealer_hand.push(card);
    }

    fn card_to_player(&mut self) {
        let card = self.shoe.draw_card();
        self.player_hands[self.current_hand].push(card);
    }

    pub fn hit(&mut self) {
        self.card_to_player();
    }

    pub fn initial_deal(&mut self) {
        assert!(
            self.dealer_hand.cards.is_empty(),
            "Can't do the initial deal when cards have already been dealt."
        );

        self.card_to_dealer();
        self.card_to_player();
        self.card_to_dealer();
        self.card_to_player();
    }

    pub fn can_place_insurance_bet(&self) -> bool {
        matches!(self.dealer_hand.face_card().rank, Rank::Ace)
    }

    pub fn place_insurance_bet(&mut self) {
        self.insurance = true;
    }

    pub fn can_double_down(&self) -> bool {
        self.player_hands[self.current_hand].can_double_down()
    }

    pub fn can_split(&self) -> bool {
        !self.splitting && self.player_hands[self.current_hand].can_split()
    }

    pub fn double_down(&mut self) {
        self.card_to_player();
        self.doubling_down = true;
    }

    pub fn split(&mut self) {
        self.splitting = true;

        let mut new_hand = self.player_hands[self.current_hand].split();
        let card = self.shoe.draw_card();
        new_hand.cards.push(card);

        self.player_hands.push(new_hand);

        self.card_to_player();
    }

    pub fn stand(&mut self) {
        self.player_hands[self.current_hand].standing = true;
    }

    pub fn reveal_hole_card(&mut self) {
        self.dealer_hand.hidden_count = 0;
    }

    pub fn next_hand(&mut self) {
        self.current_hand += 1;
    }

    pub fn insurance_payout(&self) -> Money {
        self.bet + self.bet * self.config.insurance_payout_ratio
    }

    pub fn natural_blackjack_payout(&self) -> Money {
        self.bet + self.bet * self.config.blackjack_payout_ratio
    }
}

pub fn play_blackjack() -> Result<()> {
    let mut casino = Casino::from_filesystem()?;
    let mut blackjack = Blackjack::new(casino.config.blackjack.clone());

    println!("Your money: {}", casino.bankroll);

    loop {
        let bet_text = Text::new("How much will you bet?").prompt()?;

        let bet = bet_text
            .trim()
            .parse::<Money>()
            .with_context(|| "Failed to parse prompt text into an integer")?;

        if casino.bankroll >= bet {
            blackjack.set_bet(bet);
            break;
        } else {
            println!("You can't bet that amount, try again.");
        }
    }

    casino.subtract_bankroll(blackjack.bet)?;

    println!("Betting {}", blackjack.bet);

    let mut sp = Spinner::new(Spinners::Dots, "Dealing cards...".into());
    sleep(Duration::from_millis(1_500));
    sp.stop_with_message(format!("{}", "* The dealer issues your cards.".dimmed()));

    blackjack.initial_deal();

    println!("Dealer's hand: {}", blackjack.dealer_hand);
    println!("Your hand: {}", blackjack.player_hands[0]);

    if casino.bankroll >= blackjack.bet && blackjack.can_place_insurance_bet() {
        let take_insurance = Confirm::new("Insurance?").with_default(false).prompt()?;

        if take_insurance {
            casino.subtract_bankroll(blackjack.bet)?;
            blackjack.place_insurance_bet();
            println!("You make an additional {} insurance bet.", blackjack.bet,);
        } else {
            println!("You choose for forego making an insurance bet.");
        }
    }

    while !blackjack.player_hands.iter().all(|hand| hand.is_finished()) {
        let mut options = vec!["Hit", "Stand"];

        if casino.bankroll >= blackjack.bet && blackjack.can_double_down() {
            options.push("Double");
        }

        if casino.bankroll >= blackjack.bet && blackjack.can_split() {
            options.push("Split");
        }

        let prompt = format!(
            "What will you do with hand â„– {}?",
            blackjack.current_hand_index() + 1
        );

        let ans = Select::new(&prompt, options).prompt()?;

        match ans {
            "Hit" => {
                let mut sp = Spinner::new(Spinners::Dots, "Dealing another card...".into());
                sleep(Duration::from_millis(1_000));
                sp.stop_with_message(format!(
                    "{}",
                    "* The dealer hands you another card.".dimmed()
                ));

                blackjack.hit();
            }
            "Double" => {
                println!(
                    "Your bet is now {}, and you will only receive one more card.",
                    blackjack.bet * 2u32
                );

                let mut sp = Spinner::new(Spinners::Dots, "Dealing another card...".into());
                sleep(Duration::from_millis(1_000));
                sp.stop_with_message(format!(
                    "{}",
                    "* The dealer hands you another card.".dimmed()
                ));
                casino.subtract_bankroll(blackjack.bet)?;
                blackjack.double_down();
            }
            "Split" => {
                println!(
                    "You split hand â„– {} and place an additional {} bet.",
                    blackjack.current_hand_index() + 1,
                    blackjack.bet
                );

                let mut sp = Spinner::new(Spinners::Dots, "Dealing your cards...".into());
                sleep(Duration::from_millis(1_000));
                sp.stop_with_message(format!(
                    "{}",
                    "* The dealer hands you another two cards.".dimmed()
                ));

                casino.subtract_bankroll(blackjack.bet)?;
                blackjack.split();
            }
            "Stand" => {
                blackjack.stand();
            }
            _ => bail!("Unknown answer received"),
        }

        println!(
            "Your hand â„– {}: {}",
            blackjack.current_hand_index() + 1,
            blackjack.current_player_hand(),
        );

        if blackjack.current_player_hand().is_bust() {
            casino.stats.blackjack.record_loss(blackjack.bet);
            casino.stats.update_bankroll(casino.bankroll);

            println!(
                "HAND â„– {} BUST! You lose {}. You now have {}",
                blackjack.current_hand_index() + 1,
                blackjack.bet,
                casino.bankroll
            );
        }

        if blackjack.current_player_hand().is_finished() {
            blackjack.next_hand();
        }
    }

    if blackjack.player_hands.iter().any(|hand| !hand.is_bust()) {
        let mut sp = Spinner::new(Spinners::Dots, "Revealing the hole card...".into());
        sleep(Duration::from_millis(1_000));
        sp.stop_with_message(format!("{}", "* Hole card revealed!".dimmed()));

        blackjack.reveal_hole_card();
        println!("Dealer's hand: {}", blackjack.dealer_hand);

        while blackjack.dealer_hand.blackjack_sum() < 17 {
            let mut sp = Spinner::new(Spinners::Dots, "Dealing another card...".into());
            sleep(Duration::from_millis(1_000));
            sp.stop_with_message(format!(
                "{}",
                "* The dealer issues themself another card.".dimmed()
            ));

            blackjack.card_to_dealer();
            println!("Dealer's hand: {}", blackjack.dealer_hand);
        }

        let mut sp = Spinner::new(Spinners::Dots, "Determining outcome...".into());
        sleep(Duration::from_millis(1_000));
        sp.stop_with_message(format!("{}", "* The hand is finished!".dimmed()));

        for i in 0..blackjack
            .player_hands
            .iter()
            .filter(|h| !h.is_bust())
            .count()
        {
            let hand = &blackjack.player_hands[i];

            if blackjack.dealer_hand.is_bust() {
                casino.stats.blackjack.record_win(blackjack.bet);
                casino.add_bankroll(blackjack.bet * 2u32);
                println!(
                    "DEALER BUST! You receive {}. You now have {}",
                    blackjack.bet, casino.bankroll
                );
            } else if blackjack.dealer_hand.blackjack_sum() == hand.blackjack_sum() {
                casino.stats.blackjack.record_push();
                casino.add_bankroll(blackjack.bet);
                println!("PUSH! Nobody wins.");
            } else if blackjack.dealer_hand.blackjack_sum() > hand.blackjack_sum() {
                let bet = blackjack.bet;
                casino.stats.blackjack.record_loss(bet);
                casino.stats.update_bankroll(casino.bankroll);
                println!(
                    "HOUSE WINS! You lose {}. You now have {}",
                    bet, casino.bankroll
                );
            } else if hand.is_natural_blackjack() {
                let payout = blackjack.natural_blackjack_payout();
                casino.stats.blackjack.record_win(payout);
                casino.add_bankroll(payout);
                println!(
                    "BLACKJACK! You receive {}. You now have {}",
                    payout, casino.bankroll
                );
            } else {
                let bet = blackjack.bet;
                casino.stats.blackjack.record_win(bet);
                casino.add_bankroll(bet);
                println!(
                    "YOU WIN! You receive {}. You now have {}",
                    bet, casino.bankroll
                );
            }
        }

        if blackjack.dealer_hand.is_natural_blackjack() && blackjack.insurance {
            let insurance_payout = blackjack.insurance_payout();
            casino.add_bankroll(insurance_payout);
            println!(
                "DEALER BLACKJACK! Your insurance bet pays out {}. You now have {}.",
                insurance_payout, casino.bankroll
            );
        }
    }

    casino.check_for_mister_green();

    casino.save();

    Ok(())
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BlackjackConfig {
    #[serde(default = "BlackjackConfig::default_shoe_count")]
    pub shoe_count: u8,

    #[serde(default = "BlackjackConfig::default_shuffle_penetration")]
    pub shuffle_at_penetration: f32,

    #[serde(default = "BlackjackConfig::default_payout_ratio")]
    pub payout_ratio: Ratio<i64>,

    #[serde(default = "BlackjackConfig::default_blackjack_payout_ratio")]
    pub blackjack_payout_ratio: Ratio<i64>,

    #[serde(default = "BlackjackConfig::default_insurance_payout_ratio")]
    pub insurance_payout_ratio: Ratio<i64>,
}

impl Default for BlackjackConfig {
    fn default() -> Self {
        Self {
            shoe_count: Self::default_shoe_count(),
            shuffle_at_penetration: Self::default_shuffle_penetration(),
            payout_ratio: Self::default_payout_ratio(),
            blackjack_payout_ratio: Self::default_blackjack_payout_ratio(),
            insurance_payout_ratio: Self::default_insurance_payout_ratio(),
        }
    }
}

impl BlackjackConfig {
    pub fn shuffle_shoe_threshold_count(&self) -> usize {
        let threshold_fraction = 1f32 - self.shuffle_at_penetration;
        let starting_shoe_size = self.shoe_count as usize * 52;

        (starting_shoe_size as f32 * threshold_fraction) as usize
    }

    fn default_shoe_count() -> u8 {
        4
    }

    fn default_shuffle_penetration() -> f32 {
        0.75
    }

    fn default_payout_ratio() -> Ratio<i64> {
        Ratio::new(1, 1)
    }

    fn default_blackjack_payout_ratio() -> Ratio<i64> {
        Ratio::new(3, 2)
    }

    fn default_insurance_payout_ratio() -> Ratio<i64> {
        Ratio::new(2, 1)
    }
}