chironaut 0.3.5

A poker game library for Texas Hold'em and other poker variants
Documentation
use rand::seq::SliceRandom;
use rand::thread_rng;

use crate::card::{Card, Rank, Suit};

#[derive(Debug, Clone)]
pub struct Deck {
    cards: Vec<Card>,
}

impl Deck {
    /// Create a new standard 52-card deck
    pub fn new() -> Self {
        let mut cards = Vec::with_capacity(52);
        
        for &suit in &[Suit::Clubs, Suit::Diamonds, Suit::Hearts, Suit::Spades] {
            for &rank in &[
                Rank::Two, Rank::Three, Rank::Four, Rank::Five, Rank::Six,
                Rank::Seven, Rank::Eight, Rank::Nine, Rank::Ten, Rank::Jack,
                Rank::Queen, Rank::King, Rank::Ace,
            ] {
                cards.push(Card::new(rank, suit));
            }
        }
        
        Self { cards }
    }
    
    /// Shuffle the deck
    pub fn shuffle(&mut self) {
        let mut rng = thread_rng();
        self.cards.shuffle(&mut rng);
    }
    
    /// Deal a single card from the deck
    pub fn deal(&mut self) -> Option<Card> {
        self.cards.pop()
    }
    
    /// Deal multiple cards at once
    pub fn deal_many(&mut self, count: usize) -> Vec<Card> {
        let remaining = self.cards.len();
        let count = count.min(remaining);
        
        let cards: Vec<Card> = self.cards.drain(remaining - count..).collect();
        cards
    }
    
    /// Get the number of cards remaining in the deck
    pub fn remaining(&self) -> usize {
        self.cards.len()
    }
    
    /// Create a new empty deck (for restoring from a snapshot)
    pub fn new_empty() -> Self {
        Self { cards: Vec::new() }
    }
    
    /// Add a card to the deck (used when restoring from a snapshot)
    pub fn add_card(&mut self, card: Card) {
        self.cards.push(card);
    }
    
    /// Get a copy of all remaining cards in the deck
    pub fn remaining_cards(&self) -> Vec<Card> {
        self.cards.clone()
    }
    
    /// Reset the deck to a full, unshuffled state
    pub fn reset(&mut self) {
        *self = Deck::new();
    }
}