Deck Implementation
Implemented a Deck type that has a new function that returns an unshuffled deck. Also wrote an integration test for the new function to make sure that it works. Reviewed-on: #11
This commit was merged in pull request #11.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Card {
|
||||
pub suit: u8,
|
||||
pub value: u8,
|
||||
|
||||
41
src/deck.rs
41
src/deck.rs
@@ -0,0 +1,41 @@
|
||||
use super::card::Card;
|
||||
|
||||
pub struct Deck {
|
||||
deck_size: u64,
|
||||
current_card: u64,
|
||||
cards: Vec<Card>,
|
||||
}
|
||||
|
||||
impl Deck {
|
||||
pub fn new(num_decks: u64) -> Self {
|
||||
let deck_size: u64 = num_decks * 52;
|
||||
let mut cards: Vec<Card> = Vec::new();
|
||||
|
||||
for card in 0..deck_size {
|
||||
let value = (card % 13) as u8;
|
||||
let suit = ((card / 13) % 4) as u8;
|
||||
|
||||
cards.push(Card {
|
||||
value: value,
|
||||
suit: suit,
|
||||
});
|
||||
}
|
||||
|
||||
Deck {
|
||||
deck_size: deck_size,
|
||||
current_card: 0,
|
||||
cards: cards,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_next_card(&mut self) -> Card {
|
||||
let next_card = self.cards[self.current_card as usize];
|
||||
self.current_card += 1;
|
||||
|
||||
next_card
|
||||
}
|
||||
|
||||
pub fn deck_size(&self) -> usize {
|
||||
self.deck_size as usize
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ use std::error::Error;
|
||||
|
||||
mod card;
|
||||
mod deck;
|
||||
|
||||
pub use deck::Deck;
|
||||
|
||||
mod gamestate;
|
||||
mod hand;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user