rusty_freecell/cards.rs
1//! Utilities for creating cards and decks
2
3/// A struct representing a playing card with a rank and a suit.
4#[derive(Default, Copy, Clone)]
5pub struct Card {
6 /// The rank of the card, from 1 (Ace) to 13 (King).
7 pub rank: u8,
8
9 /// The suit of the card, represented as an integer where:
10 /// - 1: Hearts
11 /// - 2: Clubs
12 /// - 3: Diamonds
13 /// - 4: Spades
14 pub suit: u8
15}
16
17impl PartialEq for Card {
18 fn eq(&self, other: &Self) -> bool {
19 self.rank == other.rank && self.suit == other.suit
20 }
21}
22
23/// Creates a standard deck of playing cards.
24///
25/// # Arguments
26///
27/// * `ranks` - The number of ranks per suit in the deck.
28/// * `suits` - The number of suits in the deck.
29///
30/// # Returns
31///
32/// A vector containing a standard deck of cards with ranks ranging from 1 to `ranks` and suits ranging from 1 to `suits`.
33pub fn new_standard_deck(ranks: u8, suits: u8) -> Vec<Card> {
34 (0..ranks*suits).map(
35 |i|
36 Card {
37 rank: (i % ranks + 1),
38 suit: (i / ranks + 1)
39 }
40 ).collect()
41}