gametools 0.8.0

Toolkit for game-building in Rust: cards, dice, dominos, spinners, refillable pools, and ordering helpers.
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
//! Uno Card Module

use crate::{Card, cards::CardFaces};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Structure representing the information on a single standard Uno card.
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct UnoCard {
    pub color: UnoColor,
    pub kind: UnoCardKind,
}

impl CardFaces for UnoCard {
    fn display_front(&self) -> String {
        format!("{} ({})", self.color, self.kind)
    }

    fn display_back(&self) -> Option<String> {
        None
    }

    fn matches(&self, other: &Self) -> bool {
        self.color == other.color && self.kind == other.kind
    }

    fn compare(&self, other: &Self) -> std::cmp::Ordering {
        if self.kind.is_wild() {
            std::cmp::Ordering::Greater
        } else if other.kind.is_wild() {
            std::cmp::Ordering::Less
        } else {
            self.kind.cmp(&other.kind)
        }
        .then_with(|| self.color.cmp(&other.color))
    }
}

impl UnoCard {
    /// Returns `true` if this card can be legally played on the `other` card.
    #[must_use]
    pub fn plays_on(&self, other: &UnoCard, declared_color: Option<UnoColor>) -> bool {
        use UnoAction::{DrawTwo, Reverse, Skip};
        use UnoCardKind::{Action, Number, Wild, WildDrawFour};
        if let Some(declared) = declared_color
            && self.color == declared
        {
            return true;
        }

        if self.color == other.color {
            return true;
        }
        match self.kind {
            Wild | WildDrawFour => true,
            Number(x) => {
                if let Number(other) = other.kind {
                    x == other
                } else {
                    false
                }
            }
            Action(uno_action) => match uno_action {
                DrawTwo => matches!(other.kind, Action(DrawTwo)),
                Skip => matches!(other.kind, Action(Skip)),
                Reverse => matches!(other.kind, Action(Reverse)),
            },
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UnoColor {
    Red,
    Blue,
    Green,
    Yellow,
    Black,
}
impl std::fmt::Display for UnoColor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UnoColor::Red => write!(f, "Red"),
            UnoColor::Blue => write!(f, "Blue"),
            UnoColor::Green => write!(f, "Green"),
            UnoColor::Yellow => write!(f, "Yellow"),
            UnoColor::Black => write!(f, "Black"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UnoCardKind {
    Number(u8),
    Action(UnoAction),
    Wild,
    WildDrawFour,
}
impl UnoCardKind {
    /// Returns true if the card is a wild card.
    #[must_use]
    pub fn is_wild(&self) -> bool {
        matches!(self, Self::Wild | Self::WildDrawFour)
    }
}
impl std::fmt::Display for UnoCardKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UnoCardKind::Number(number) => write!(f, "#{number}"),
            UnoCardKind::Action(action) => write!(f, "{action}"),
            UnoCardKind::Wild => write!(f, "Wild"),
            UnoCardKind::WildDrawFour => write!(f, "Wild + Draw 4"),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UnoAction {
    DrawTwo,
    Skip,
    Reverse,
}
impl std::fmt::Display for UnoAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UnoAction::DrawTwo => write!(f, "Draw Two"),
            UnoAction::Skip => write!(f, "Skip"),
            UnoAction::Reverse => write!(f, "Reverse"),
        }
    }
}

/// Counts of each number card per color, 0-9.
pub const UNO_NUMBER_CARD_COUNTS: &[u8] = &[1, 2, 2, 2, 2, 2, 2, 2, 2, 2];
pub const MAIN_UNO_COLORS: &[UnoColor] = &[
    UnoColor::Red,
    UnoColor::Blue,
    UnoColor::Green,
    UnoColor::Yellow,
];

/// Create a full set of 108 Uno cards
#[must_use]
pub fn full_uno_set() -> Vec<UnoCard> {
    let mut cards = Vec::new();
    cards.extend(uno_number_cards());
    cards.extend(uno_action_cards());
    cards.extend(uno_wild_cards());
    cards
}

/// Create all of the number card faces for a standard Uno deck
#[must_use]
pub fn uno_number_cards() -> Vec<UnoCard> {
    let mut cards = Vec::new();
    for color in MAIN_UNO_COLORS {
        for (number, count) in UNO_NUMBER_CARD_COUNTS.iter().enumerate() {
            for _ in 0..*count {
                cards.push(UnoCard {
                    color: *color,
                    #[allow(clippy::cast_possible_truncation)]
                    kind: UnoCardKind::Number(number as u8),
                });
            }
        }
    }
    cards
}

/// Create all of the action cards in a standard Uno deck
#[must_use]
pub fn uno_action_cards() -> Vec<UnoCard> {
    let mut cards = Vec::new();
    for color in MAIN_UNO_COLORS {
        for _ in 0..2 {
            cards.push(UnoCard {
                color: *color,
                kind: UnoCardKind::Action(UnoAction::DrawTwo),
            });
            cards.push(UnoCard {
                color: *color,
                kind: UnoCardKind::Action(UnoAction::Skip),
            });
            cards.push(UnoCard {
                color: *color,
                kind: UnoCardKind::Action(UnoAction::Reverse),
            });
        }
    }
    cards
}

/// Create the wild cards for a standard Uno deck
#[must_use]
pub fn uno_wild_cards() -> Vec<UnoCard> {
    let mut cards = Vec::new();
    for _ in 0..4 {
        cards.push(UnoCard {
            color: UnoColor::Black,
            kind: UnoCardKind::Wild,
        });
        cards.push(UnoCard {
            color: UnoColor::Black,
            kind: UnoCardKind::WildDrawFour,
        });
    }
    cards
}

impl super::Hand<UnoCard> {
    /// Return the cards in this hand that can legally be played on `top`.
    ///
    /// `declared_color` should be provided when a wild has set the active color; when `None`
    /// the visible face drives eligibility. Each result pairs the card's index within the
    /// hand (handy for follow-up removal) with a reference to the card.
    #[must_use]
    pub fn playable_on(
        &self,
        top: &Card<UnoCard>,
        declared_color: Option<UnoColor>,
    ) -> Vec<(usize, &Card<UnoCard>)> {
        let mut playable = Vec::new();
        for (idx, card) in self.cards().iter().enumerate() {
            if card.faces.plays_on(&top.faces, declared_color) {
                playable.push((idx, card));
            }
        }
        playable
    }
    /// Determine the number of points this hand is currently worth.
    #[must_use]
    pub fn points(&self) -> usize {
        let mut pts = 0usize;
        for card in self.cards() {
            match card.faces.kind {
                UnoCardKind::Number(face_value) => pts += face_value as usize,
                UnoCardKind::Action(_) => pts += 20,
                UnoCardKind::Wild | UnoCardKind::WildDrawFour => pts += 50,
            }
        }
        pts
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{AddCard, Card, Hand};
    use std::collections::BTreeMap;

    fn face(color: UnoColor, kind: UnoCardKind) -> UnoCard {
        UnoCard { color, kind }
    }

    fn card(color: UnoColor, kind: UnoCardKind) -> Card<UnoCard> {
        Card::new_card(face(color, kind))
    }

    #[test]
    fn plays_on_honors_color_number_action_and_declared_color() {
        let red_three = face(UnoColor::Red, UnoCardKind::Number(3));
        let red_five = face(UnoColor::Red, UnoCardKind::Number(5));
        let blue_three = face(UnoColor::Blue, UnoCardKind::Number(3));
        let green_four = face(UnoColor::Green, UnoCardKind::Number(4));

        assert!(red_three.plays_on(&red_five, None), "matching colors");
        assert!(red_three.plays_on(&blue_three, None), "matching numbers");
        assert!(
            !red_three.plays_on(&green_four, None),
            "no color or number match"
        );

        let blue_skip = face(UnoColor::Blue, UnoCardKind::Action(UnoAction::Skip));
        let yellow_skip = face(UnoColor::Yellow, UnoCardKind::Action(UnoAction::Skip));
        assert!(
            blue_skip.plays_on(&yellow_skip, None),
            "same action despite color"
        );

        let wild = face(UnoColor::Black, UnoCardKind::Wild);
        let draw_four = face(UnoColor::Black, UnoCardKind::WildDrawFour);
        assert!(wild.plays_on(&red_three, None));
        assert!(draw_four.plays_on(&red_three, None));

        assert!(
            blue_skip.plays_on(&wild, Some(UnoColor::Blue)),
            "declared color allows play"
        );
        assert!(red_three.plays_on(&wild, Some(UnoColor::Red)));
        assert!(
            !green_four.plays_on(&wild, Some(UnoColor::Blue)),
            "mismatched declaration"
        );
    }

    #[test]
    fn playable_on_identifies_valid_hand_cards() {
        let mut hand = Hand::<UnoCard>::new("bot");
        for card in [
            card(UnoColor::Red, UnoCardKind::Number(3)),
            card(UnoColor::Blue, UnoCardKind::Number(3)),
            card(UnoColor::Yellow, UnoCardKind::Action(UnoAction::Skip)),
            card(UnoColor::Black, UnoCardKind::Wild),
        ] {
            hand.add_card(card);
        }

        let top = card(UnoColor::Green, UnoCardKind::Number(3));
        {
            let playable = hand.playable_on(&top, None);
            let snapshot: Vec<(usize, UnoColor, UnoCardKind)> = playable
                .iter()
                .map(|(idx, card)| (*idx, card.faces.color, card.faces.kind))
                .collect();

            assert_eq!(
                snapshot,
                vec![
                    (0, UnoColor::Red, UnoCardKind::Number(3)),
                    (1, UnoColor::Blue, UnoCardKind::Number(3)),
                    (3, UnoColor::Black, UnoCardKind::Wild),
                ],
                "cards matching by number or being wild are playable",
            );
        }

        let wild_top = card(UnoColor::Black, UnoCardKind::Wild);
        let declared = hand.playable_on(&wild_top, Some(UnoColor::Yellow));
        let declared_indices: Vec<usize> = declared.iter().map(|(idx, _)| *idx).collect();
        assert_eq!(
            declared_indices,
            vec![2, 3],
            "only declared color and wilds remain playable"
        );
    }

    #[test]
    fn number_cards_follow_expected_distribution() {
        let cards = uno_number_cards();
        let expected_per_color: usize = UNO_NUMBER_CARD_COUNTS
            .iter()
            .map(|&count| count as usize)
            .sum();

        assert_eq!(
            cards.len(),
            MAIN_UNO_COLORS.len() * expected_per_color,
            "Unexpected number of Uno number cards"
        );
        assert!(
            cards
                .iter()
                .all(|card| matches!(card.kind, UnoCardKind::Number(_)))
        );

        for color in MAIN_UNO_COLORS {
            for number in 0u8..=9 {
                let expected = UNO_NUMBER_CARD_COUNTS[number as usize] as usize;
                let actual = cards
                    .iter()
                    .filter(|card| {
                        card.color == *color
                            && matches!(card.kind, UnoCardKind::Number(value) if value == number)
                    })
                    .count();
                assert_eq!(
                    actual, expected,
                    "Expected {expected} copies of {color:?} {number}, found {actual}"
                );
            }
        }
    }

    #[test]
    fn action_cards_include_two_of_each_per_color() {
        let cards = uno_action_cards();
        assert_eq!(
            cards.len(),
            MAIN_UNO_COLORS.len() * 6,
            "Unexpected number of Uno action cards"
        );

        for color in MAIN_UNO_COLORS {
            for action in [UnoAction::DrawTwo, UnoAction::Skip, UnoAction::Reverse] {
                let actual = cards
                    .iter()
                    .filter(|card| {
                        card.color == *color
                            && matches!(card.kind, UnoCardKind::Action(kind) if kind == action)
                    })
                    .count();
                assert_eq!(
                    actual, 2,
                    "Expected two {color:?} {action:?} cards, found {actual}"
                );
            }
        }
    }

    #[test]
    fn wild_cards_include_four_of_each_type() {
        let cards = uno_wild_cards();
        assert_eq!(cards.len(), 8);
        assert!(cards.iter().all(|card| card.color == UnoColor::Black));

        let wild_count = cards
            .iter()
            .filter(|card| matches!(card.kind, UnoCardKind::Wild))
            .count();
        let wild_draw_four_count = cards
            .iter()
            .filter(|card| matches!(card.kind, UnoCardKind::WildDrawFour))
            .count();

        assert_eq!(wild_count, 4);
        assert_eq!(wild_draw_four_count, 4);
    }

    #[test]
    fn full_uno_set_contains_expected_cards() {
        let full_set = full_uno_set();
        assert_eq!(full_set.len(), 108);

        let mut seen = BTreeMap::new();
        for card in &full_set {
            *seen.entry(*card).or_insert(0usize) += 1;
        }

        for card in uno_number_cards()
            .into_iter()
            .chain(uno_action_cards())
            .chain(uno_wild_cards())
        {
            let should_remove = match seen.get_mut(&card) {
                Some(count) if *count > 0 => {
                    *count -= 1;
                    *count == 0
                }
                _ => panic!("Card {:?} was expected in full Uno set but not found", card),
            };

            if should_remove {
                seen.remove(&card);
            }
        }

        assert!(
            seen.is_empty(),
            "Found unexpected extra cards in full Uno set: {:?}",
            seen
        );
    }
}