5
|
1 |
/// This is the implementation of a Deck of cards. |
|
2 |
/// There are two basic kinds: |
|
3 |
/// 1. The Game Deck, automatically generated 104 cards. |
|
4 |
/// 2. Any Deck-type pile, like the hand of a Player. |
|
5 |
/// Descks can be shuffled, cards pushed to bottom and |
|
6 |
/// pulled from top. Maybe others, too. |
|
7 |
/// |
|
8 |
|
4
|
9 |
use std::collections::VecDeque; |
|
10 |
use rand::Rng; |
|
11 |
|
5
|
12 |
/// We reference [`Card`]s |
4
|
13 |
use crate::card::Card; |
|
14 |
|
|
15 |
#[derive(Debug)] |
5
|
16 |
/// A Deck is an ordered two-ended vector (`VecDeque`) of [`Card`]s. |
4
|
17 |
pub(crate) struct Deck { |
|
18 |
content: VecDeque<Card>, |
|
19 |
} |
|
20 |
|
|
21 |
impl Deck { |
|
22 |
/// Creates an empty [`Deck`] of undefined size |
|
23 |
pub(crate) fn new_empty() -> Self { |
|
24 |
debug!("Empty deck generated"); |
|
25 |
Deck { |
|
26 |
content: VecDeque::new(), |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
/// Creates a new full [`Deck`] with 104 cards. |
|
31 |
pub(crate) fn new() -> Self { |
|
32 |
debug!("Full deck generated"); |
|
33 |
let content = (1..=104).into_iter().map( |n| Card::new(n) ).collect(); |
|
34 |
Deck { |
|
35 |
content, |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
/// Shuffles this [`Deck`] (using its default method) |
|
40 |
pub(crate) fn shuffle( &mut self ) { |
|
41 |
let mut rng = rand::thread_rng(); |
|
42 |
|
|
43 |
trace!("Deck before shuffle: len {}, {:?}", self.content.len(), self); |
|
44 |
debug!("Deck shuffled"); |
|
45 |
// shufflers: |
|
46 |
// * naive: swap cards n times |
|
47 |
// * kgb: half the deck, take 1..4 cards sequentially from each |
|
48 |
// * grin: take 1..6 from front and put at bottom |
|
49 |
|
5
|
50 |
// naive shuffle: exchange <n> random cards |
|
51 |
for _ in 1..=500 { |
|
52 |
// Generate two random positions of cards in Deck |
4
|
53 |
let c1 = rng.gen_range(0 .. self.content.len()); |
|
54 |
let c2 = rng.gen_range(0 .. self.content.len()); |
5
|
55 |
// If it's not the same card we swap them in the `content` vector. |
|
56 |
// The `swap()` funtion handles ownership and does not angry |
|
57 |
// the borrow checker. |
4
|
58 |
if c1 != c2 { |
|
59 |
self.content.swap(c1, c2); |
|
60 |
} |
|
61 |
} |
|
62 |
trace!("Deck after shuffle: len {}, {:?}", self.content.len(), self); |
|
63 |
} |
|
64 |
|
|
65 |
/// Returns the top card of this [`Deck`]. |
|
66 |
pub(crate) fn pop( &mut self ) -> Option<Card> { |
|
67 |
self.content.pop_front() |
|
68 |
} |
|
69 |
|
|
70 |
/// Put a card into the bottom of the [`Deck`] |
|
71 |
pub(crate) fn push( &mut self, c: Card ) { |
|
72 |
self.content.push_back(c); |
|
73 |
} |
|
74 |
|
|
75 |
/// Push multiple cards to the bottom of the [`Deck`] |
|
76 |
pub(crate) fn push_cards( &mut self, cards: VecDeque<Card> ) { |
|
77 |
trace!("Collecting back, deck len is {}, cards {}", self.content.len(), cards.len()); |
|
78 |
cards.into_iter().for_each( |card| self.push(card) ); |
|
79 |
trace!("Deck len is {}", self.content.len()); |
|
80 |
} |
|
81 |
|
|
82 |
/// Returns the length of this [`Deck`]. |
|
83 |
pub(crate) fn len( &self ) -> usize { |
|
84 |
self.content.len() |
|
85 |
} |
|
86 |
|
5
|
87 |
/// Take a card from inside of the Deck. |
|
88 |
/// Using an illegal index will `panic!()`, so don't. |
4
|
89 |
fn _get_nth( &mut self, n: usize ) -> Card { |
|
90 |
if let Some(c) = self.content.remove(n) { |
|
91 |
c |
|
92 |
} else { |
|
93 |
panic!("get_nth: index {} out of bounds ({})!", n, self.content.len()); |
|
94 |
} |
|
95 |
} |
|
96 |
} |