]>
Commit | Line | Data |
---|---|---|
57f490a0 SW |
1 | use rand::Rng; |
2 | ||
95b540e1 SW |
3 | pub const NUM_RANKS: usize = 13; |
4 | pub const NUM_SUITS: usize = 4; | |
5 | pub const NUM_JOKERS: usize = 2; | |
6 | pub const NUM_CARDS: usize = NUM_RANKS * NUM_SUITS + NUM_JOKERS; | |
57f490a0 SW |
7 | |
8 | #[derive(Clone, Copy, Eq, PartialEq)] | |
9 | pub struct Rank(u8); | |
b8e315ac SW |
10 | impl Rank { |
11 | #[must_use] | |
12 | pub fn value(&self) -> u8 { | |
13 | self.0 + 1 | |
14 | } | |
15 | } | |
57f490a0 | 16 | |
09822a98 SW |
17 | #[derive(Clone, Copy, Eq, PartialEq)] |
18 | pub struct Suit(u8); | |
19 | ||
644e6c7a | 20 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
09822a98 SW |
21 | pub struct Card(u8); |
22 | impl Card { | |
23 | #[must_use] | |
10e7da7b | 24 | pub fn is_joker(&self) -> bool { |
95b540e1 | 25 | usize::from(self.0) >= NUM_RANKS * NUM_SUITS |
09822a98 SW |
26 | } |
27 | #[must_use] | |
10e7da7b SW |
28 | pub fn rank(&self) -> Option<Rank> { |
29 | (!self.is_joker()).then_some(Rank(self.0 >> 2)) | |
09822a98 | 30 | } |
10e7da7b SW |
31 | #[must_use] |
32 | pub fn suit(&self) -> Option<Suit> { | |
33 | (!self.is_joker()).then_some(Suit(self.0 & 3)) | |
34 | } | |
35 | } | |
36 | ||
37 | #[derive(Clone, Copy)] | |
38 | pub enum WithOrWithoutJokers { | |
39 | WithJokers, | |
40 | WithoutJokers, | |
09822a98 SW |
41 | } |
42 | ||
43 | #[must_use] | |
10e7da7b | 44 | pub fn deck(j: WithOrWithoutJokers) -> Vec<Card> { |
95b540e1 | 45 | let limit = u8::try_from(match j { |
10e7da7b SW |
46 | WithOrWithoutJokers::WithJokers => NUM_CARDS, |
47 | WithOrWithoutJokers::WithoutJokers => NUM_SUITS * NUM_RANKS, | |
95b540e1 SW |
48 | }) |
49 | .expect("Too many cards?"); | |
10e7da7b | 50 | (0..limit).map(Card).collect() |
09822a98 SW |
51 | } |
52 | ||
57f490a0 SW |
53 | #[derive(Clone, Copy)] |
54 | pub struct PathLength(Rank); | |
55 | ||
56 | #[derive(Clone, Copy, Default)] | |
57 | pub struct PathLengthInfo(u16); | |
58 | impl PathLengthInfo { | |
59 | #[must_use] | |
60 | pub fn is_showing(&self, i: Rank) -> bool { | |
61 | (self.0 >> i.0) & 1 == 1 | |
62 | } | |
63 | fn reveal(&mut self, i: Rank) { | |
64 | self.0 |= 1 << i.0; | |
65 | } | |
66 | pub fn reveal_random(&mut self, true_length: PathLength) -> Option<Rank> { | |
95b540e1 | 67 | let showing = usize::try_from(self.0.count_ones()).expect("There aren't that many bits"); |
57f490a0 SW |
68 | let not_showing = NUM_RANKS - showing; |
69 | if not_showing <= 1 { | |
70 | return None; | |
71 | } | |
72 | ||
73 | let mut show = rand::thread_rng().gen_range(0..not_showing - 1); | |
74 | for i in 0..NUM_RANKS { | |
95b540e1 | 75 | let r = Rank(u8::try_from(i).expect("Too many cards?")); |
57f490a0 SW |
76 | if !self.is_showing(r) && r != true_length.0 { |
77 | if show == 0 { | |
78 | self.reveal(r); | |
79 | return Some(r); | |
80 | } | |
81 | show -= 1; | |
82 | } | |
83 | } | |
84 | unreachable!() | |
85 | } | |
754e9730 SW |
86 | } |
87 | ||
644e6c7a SW |
88 | #[derive(Default)] |
89 | pub struct Discard { | |
90 | cards: Vec<Card>, | |
91 | } | |
92 | impl Discard { | |
93 | pub fn discard(&mut self, card: Card) { | |
94 | self.cards.push(card); | |
95 | } | |
96 | } | |
97 | ||
98 | pub struct Library { | |
99 | cards: Vec<Card>, | |
100 | } | |
101 | impl Library { | |
102 | #[must_use] | |
103 | pub fn new(cards: Vec<Card>) -> Self { | |
104 | Self { cards } | |
105 | } | |
106 | pub fn draw(&mut self, discard: &mut Discard) -> Option<Card> { | |
107 | if self.cards.is_empty() { | |
108 | if let Some(top_discard) = discard.cards.pop() { | |
109 | std::mem::swap(&mut self.cards, &mut discard.cards); | |
110 | discard.discard(top_discard); | |
111 | // TODO: Shuffle | |
112 | } | |
113 | } | |
114 | self.cards.pop() | |
115 | } | |
116 | } | |
117 | ||
754e9730 SW |
118 | #[cfg(test)] |
119 | mod tests { | |
120 | use super::*; | |
121 | ||
122 | #[test] | |
57f490a0 SW |
123 | fn path_length_info_random_reveal() { |
124 | let length = PathLength(Rank(7)); | |
125 | let mut pli = PathLengthInfo::default(); | |
126 | for _ in 0..12 { | |
127 | let old_pli = PathLengthInfo::clone(&pli); | |
128 | match pli.reveal_random(length) { | |
129 | None => panic!("Nothing revealed?"), | |
130 | Some(r) => { | |
131 | assert!(!old_pli.is_showing(r)); | |
132 | assert!(pli.is_showing(r)); | |
133 | } | |
134 | } | |
135 | assert_eq!(pli.0.count_ones(), 1 + old_pli.0.count_ones()); | |
136 | } | |
137 | assert!(pli.reveal_random(length).is_none()); | |
754e9730 | 138 | } |
09822a98 SW |
139 | |
140 | #[test] | |
141 | fn test_deck() { | |
10e7da7b | 142 | use WithOrWithoutJokers::*; |
b8e315ac SW |
143 | let d = deck(WithoutJokers); |
144 | let rank_sum: u32 = d | |
145 | .iter() | |
146 | .map(Card::rank) | |
147 | .flatten() | |
148 | .map(|r| u32::from(r.value())) | |
149 | .sum(); | |
150 | assert_eq!(rank_sum, 364); | |
10e7da7b | 151 | let _dj = deck(WithJokers); |
09822a98 | 152 | } |
644e6c7a SW |
153 | |
154 | #[test] | |
155 | fn test_library() { | |
156 | let mut lib = Library::new(vec![Card(7)]); | |
157 | let mut dis = Discard::default(); | |
158 | dis.discard(Card(8)); | |
159 | dis.discard(Card(9)); | |
160 | assert_eq!(lib.draw(&mut dis), Some(Card(7))); | |
161 | assert_eq!(lib.draw(&mut dis), Some(Card(8))); | |
162 | assert_eq!(lib.draw(&mut dis), None); | |
163 | } | |
754e9730 | 164 | } |