Deck Shuffling

Implemented a deck shuffling algorithm. It will iterate over the deck of
cards 1000 times and swap the card it is on with another random card to
mix up the deck.

Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
2026-01-10 20:09:53 -05:00
parent cedef1792f
commit b5cc0a7293
4 changed files with 160 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
use super::card::Card;
use rand::random_range;
pub struct Deck {
deck_size: u64,
@@ -9,7 +10,7 @@ pub struct Deck {
impl Deck {
pub fn new(num_decks: u64) -> Self {
let deck_size: u64 = num_decks * 52;
let mut cards: Vec<Card> = Vec::new();
let mut cards: Vec<Card> = Vec::with_capacity(deck_size as usize);
for card in 0..deck_size {
let value = (card % 13) as u8;
@@ -38,4 +39,19 @@ impl Deck {
pub fn deck_size(&self) -> usize {
self.deck_size as usize
}
pub fn cur_card(&self) -> u64 {
self.current_card
}
pub fn shuffle(&mut self) {
for _ in 0..1000 {
for card in 0..self.deck_size {
let swap_card = random_range(0..self.deck_size);
let save_card = self.cards[card as usize];
self.cards[card as usize] = self.cards[swap_card as usize];
self.cards[swap_card as usize] = save_card;
}
}
}
}