fudd 0.1.9

Fun with poker. Named after that most famous of rabbit hunters.
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
use crate::analysis::outs::Outs;
use crate::games::holdem::case_eval::CaseEval;
use crate::games::holdem::seat::Seat;
use crate::games::holdem::seat_eval::SeatEval;
use crate::types::card_slot;
use crate::types::card_slot::CardSlot;
use crate::types::playing_card::PlayingCard;
use crate::types::playing_cards::PlayingCards;
use crate::types::poker_cards::PokerCards;
use crate::types::slots::hole_cards::HoleCards;
use ckc_rs::HandError;
use itertools::Itertools;
use log::debug;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Seats(Vec<Seat>);

impl Seats {
    #[must_use]
    pub fn seat(number: usize) -> Seats {
        let mut players = Seats::default();
        for _ in 0..number {
            players.add(HoleCards::default());
        }
        players
    }

    /// This alias is needed because, for some reason, the method used in the examples
    /// of turning command line user input into static strings returns mutable static strings,
    /// which is something that I didn't know was possible. For some reason, Rust has no problem
    /// with that input here, but chokes when it comes directly from implementing the `TryFrom` trait.
    ///
    /// Takes a Card index string and returns a `Players` entity with all
    /// the cards dealt to them.
    ///
    /// # Errors
    ///
    /// Will throw a `HandError::InvalidCard` if an invalid index is passed in.
    ///
    /// Will throw a `HandError::InvalidIndex` if the number of cards passed in
    /// isn't divisible by 2. (There must be two cards for each `Player`.)
    pub fn from_index(index: &'static str) -> Result<Seats, HandError> {
        Seats::try_from(index)
    }

    /// A case is defined as a specific possible collection of cards for slots that haven't been
    /// dealt to yet, made up of the remaining cards in the deck. For instance: if the `Flop` is
    /// `T♦ 4♠ 6♠` and player 1 is holding `7♥ 8♥` and player 2 is holding `3♦ A♠`, one possible
    /// case would be `A♦ T♦ 4♠ 6♠ A♥`, made up of the three cards in the `Flop` and two of the
    /// cards that have not been dealt to a player, in this case the `A♦` and the `A♥`.
    ///
    /// The `case_eval` method returns a `CaseEval` entity with the best possible combination of
    /// cards for each player given that specific case. For instance, given the above example,
    /// the best combination for player 1 would be a pair of aces with `A♥ A♦ T♦ 8♥ 7♥`, while
    /// the best combination for player 2 would be three aces with `A♠ A♥ A♦ T♦ 6♠`.
    ///
    /// TODO: This can be tightened up to just accept a `PokerHand`.
    ///
    /// # Panics
    ///
    /// Not sure how this would be triggered :-P
    #[must_use]
    pub fn case_eval(&self, cycle: &PlayingCards) -> CaseEval {
        let mut case_eval = CaseEval::default();
        debug!("Case Eval: {}", cycle);
        for seat in &self.0 {
            let cards = cycle.clone().combine(&seat.to_playing_cards());
            let best_for_player = cards.eval_7cards();
            let eval = best_for_player.unwrap();
            debug!("   Player {} {}", seat.number, eval);
            case_eval.push(SeatEval::new_from_eval(seat.clone(), eval));
        }
        case_eval
    }

    #[must_use]
    pub fn case_eval_with_outs(&self, out: PlayingCard, cycle: &PlayingCards) -> (Outs, CaseEval) {
        let case_eval = self.case_eval(cycle);
        let outs = Seats::get_outs(out, &case_eval);

        (outs, case_eval)
    }

    #[must_use]
    pub fn get_outs(out: PlayingCard, case_eval: &CaseEval) -> Outs {
        let mut outs = Outs::default();
        for seat in case_eval.winners().iter() {
            outs.add(seat.seat.number, out);
        }
        outs
    }

    #[must_use]
    pub fn dealt(&self) -> PlayingCards {
        let mut cards = PlayingCards::default();
        for player in self.iter() {
            cards.append(&player.to_playing_cards());
        }
        cards
    }

    #[must_use]
    pub fn get(&self, player: usize) -> Option<&HoleCards> {
        match self.0.get(player) {
            Some(seat) => Some(&seat.hole_cards),
            None => None,
        }
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = &Seat> {
        self.0.iter()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    #[must_use]
    pub fn len_for_player(&self, player: usize) -> Option<usize> {
        let player = self.0.get(player);
        player.map(card_slot::CardSlot::len)
    }

    #[must_use]
    pub fn take_for_player(&self, player: usize, card: PlayingCard) -> bool {
        let player = self.0.get(player);
        match player {
            Some(p) => p.take(card),
            None => false,
        }
    }

    pub fn add(&mut self, hole_cards: HoleCards) {
        self.0
            .push(Seat::new_with_hole_cards(self.len(), hole_cards));
    }

    pub fn add_from_index(&mut self, index: &'static str) {
        self.0.push(Seat::from_index(self.len(), index));
    }

    #[must_use]
    pub fn is_active(&self, seat: usize) -> bool {
        match self.0.get(seat) {
            Some(hand) => hand.is_dealt(),
            None => false,
        }
    }

    /// Returns true if all seated players have had their cards dealt to them.
    #[must_use]
    pub fn is_dealt(&self) -> bool {
        for i in 0..self.0.len() {
            if !self.is_active(i) {
                return false;
            }
        }
        true
    }

    #[must_use]
    pub fn fold_player(&self, player: usize) -> Option<PlayingCards> {
        self.0.get(player).map(card_slot::CardSlot::fold)
    }
}

impl CardSlot for Seats {
    /// Attempts to take a `PokerCard` and place it in the first open slot for the players,
    /// iterating over each Player like a poker dealer would. So, if the first slot of the
    /// first player is filled, and the first slot of the second player is empty, the
    /// second player's slot will take the card first before the second slot of the first player
    /// is taken.
    fn take(&self, card: PlayingCard) -> bool {
        for i in 0..self.len() {
            if let Some(0) = self.len_for_player(i) {
                return self.take_for_player(i, card);
            }
        }
        for i in 0..self.len() {
            if let Some(1) = self.len_for_player(i) {
                return self.take_for_player(i, card);
            }
        }
        false
    }

    /// Folds all the `Players` and returns the `PokerCards` folded.
    fn fold(&self) -> PlayingCards {
        let mut poker_cards = PlayingCards::default();
        for i in 0..self.len() {
            poker_cards.append(&self.fold_player(i).unwrap());
        }
        poker_cards
    }

    /// Returns true of all the `Players` have been dealt their cards.
    fn is_dealt(&self) -> bool {
        for player in self.0.clone() {
            if !player.is_dealt() {
                return false;
            }
        }
        true
    }

    /// Returns the `PokerCards` for all the `Players`.
    fn to_playing_cards(&self) -> PlayingCards {
        let mut cards = PlayingCards::default();
        for player in self.0.clone() {
            cards.append(&player.to_playing_cards());
        }
        cards
    }
}

impl Default for Seats {
    fn default() -> Seats {
        Seats::from(Vec::default())
    }
}

impl fmt::Display for Seats {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let joined = Itertools::join(&mut self.0.iter(), ", ");
        write!(f, "[{joined}]")
    }
}

impl From<Vec<Seat>> for Seats {
    fn from(value: Vec<Seat>) -> Self {
        Seats(value)
    }
}

/// Takes a Card index string and returns a `Players` entity with all
/// the cards dealt to them.
///
/// # Errors
///
/// Will throw a `HandError::InvalidCard` if an invalid index is passed in.
///
/// Will throw a `HandError::InvalidIndex` if the number of cards passed in
/// isn't divisible by 2. (There must be two cards for each `Player`.)
impl TryFrom<&'static str> for Seats {
    type Error = HandError;

    fn try_from(value: &'static str) -> Result<Self, Self::Error> {
        let poker_cards = PokerCards::try_from(value);
        match poker_cards {
            Ok(cards) => Seats::try_from(cards),
            Err(e) => Err(e),
        }
    }
}

/// # Errors
///
/// Will throw a `HandError::InvalidIndex` if the number of cards passed in
/// isn't divisible by 2. (There must be two cards for each `Player`.)
impl TryFrom<PokerCards> for Seats {
    type Error = HandError;

    fn try_from(value: PokerCards) -> Result<Self, Self::Error> {
        let mut cards = value;
        if cards.len() % 2 == 0 {
            let num_of_players = cards.len() / 2;
            let players = Seats::seat(num_of_players);
            for i in 0..num_of_players {
                let _ = players.take_for_player(i, PlayingCard::from(cards.pull()));
                let _ = players.take_for_player(i, PlayingCard::from(cards.pull()));
            }
            Ok(players)
        } else {
            Err(HandError::InvalidIndex)
        }
    }
}

impl TryFrom<PlayingCards> for Seats {
    type Error = HandError;

    fn try_from(value: PlayingCards) -> Result<Self, Self::Error> {
        let mut cards = value;
        if cards.len() % 2 == 0 {
            let num_of_players = cards.len() / 2;
            let players = Seats::seat(num_of_players);
            for i in 0..num_of_players {
                let _ = players.take_for_player(i, cards.draw_one());
                let _ = players.take_for_player(i, cards.draw_one());
            }
            Ok(players)
        } else {
            Err(HandError::InvalidIndex)
        }
    }
}

#[cfg(test)]
#[allow(non_snake_case)]
mod holdem_players_tests {
    use super::*;

    #[test]
    fn seat() {
        let players = Seats::seat(3);

        assert_eq!(
            "[Seat 0: __ __, Seat 1: __ __, Seat 2: __ __]",
            players.to_string()
        );
    }

    #[test]
    fn from_index() {
        let index = "4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦";

        let players = Seats::try_from(index);

        assert!(players.is_ok());

        assert_eq!(
            "[Seat 0: 4♥ K♣, Seat 1: 5♥ 3♠, Seat 2: 8♦ 9♠, Seat 3: 8♠ J♦]",
            players.unwrap().to_string()
        )
    }

    #[test]
    fn from_index__invalid_card() {
        // Who put a joker in the deck?
        let index = "4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦ 9♥ 9♦ 2♦ 5♠ JK";

        let players = Seats::from_index(index);

        assert!(players.is_err());
        assert_eq!(players.unwrap_err(), HandError::InvalidCard)
    }

    #[test]
    fn from_index__wrong_number_of_cards() {
        let index = "4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦ 9♥ 9♦ 2♦ 5♠ 7♦";

        let players = Seats::from_index(index);

        assert!(players.is_err());
        assert_eq!(players.unwrap_err(), HandError::InvalidIndex)
    }

    #[test]
    fn is_active() {
        let players = Seats::seat(2);
        assert!(!players.is_active(0));
        assert!(!players.is_active(1));
        assert!(!players.is_dealt());

        // deal first player
        let taken = players.take_for_player(0, PlayingCard::from(PlayingCard::ACE_DIAMONDS));
        assert!(taken);
        let taken = players.take_for_player(0, PlayingCard::from(PlayingCard::ACE_CLUBS));
        assert!(taken);
        assert!(players.is_active(0));
        assert!(!players.is_dealt());

        // deal second player
        let taken = players.take_for_player(1, PlayingCard::from(PlayingCard::KING_SPADES));
        assert!(taken);
        let taken = players.take_for_player(1, PlayingCard::from(PlayingCard::KING_CLUBS));
        assert!(taken);
        assert!(players.is_active(1));
        assert!(players.is_dealt());

        assert_eq!("[Seat 0: A♦ A♣, Seat 1: K♠ K♣]", players.to_string());
    }

    #[test]
    fn take() {
        let players = Seats::seat(3);
        let mut cards = PlayingCards::try_from("4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦").unwrap();

        assert!(players.take(cards.draw_one()));
        assert!(players.take(cards.draw_one()));
        assert_eq!(
            "[Seat 0: 4♥ __, Seat 1: K♣ __, Seat 2: __ __]",
            players.to_string()
        );

        assert!(players.take(cards.draw_one()));
        assert!(players.take(cards.draw_one()));
        assert_eq!(
            "[Seat 0: 4♥ 3♠, Seat 1: K♣ __, Seat 2: 5♥ __]",
            players.to_string()
        );

        assert!(players.take(cards.draw_one()));
        assert!(players.take(cards.draw_one()));
        assert_eq!(
            "[Seat 0: 4♥ 3♠, Seat 1: K♣ 8♦, Seat 2: 5♥ 9♠]",
            players.to_string()
        );

        assert!(!players.take(cards.draw_one()));
        assert_eq!(
            "[Seat 0: 4♥ 3♠, Seat 1: K♣ 8♦, Seat 2: 5♥ 9♠]",
            players.to_string()
        );
    }

    #[test]
    fn fold() {
        let index = "4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦";
        let players = Seats::from_index(index).unwrap();

        let folded = players.fold();

        assert_eq!("4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦", folded.to_string());
        assert_eq!(
            "[Seat 0: __ __, Seat 1: __ __, Seat 2: __ __, Seat 3: __ __]",
            players.to_string()
        );
        assert!(!players.is_dealt());
    }

    #[test]
    fn to_poker_cards() {
        let index = "4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦";
        let players = Seats::from_index(index).unwrap();

        assert_eq!(
            "4♥ K♣ 5♥ 3♠ 8♦ 9♠ 8♠ J♦",
            players.to_playing_cards().to_string()
        );
        assert!(players.is_dealt());
    }

    #[test]
    fn display() {
        let players = Seats::from_index("AS KS AD KD").unwrap();

        assert_eq!("[Seat 0: A♠ K♠, Seat 1: A♦ K♦]", players.to_string());
    }

    #[test]
    fn display__blank() {
        assert_eq!(
            "[Seat 0: __ __, Seat 1: __ __, Seat 2: __ __]",
            format!("{}", Seats::seat(3))
        )
    }
}