diff options
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e8f7511 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,72 @@ +#![feature(random)] +use std::random::random; + +#[derive(Debug, Clone)] +pub enum Suit { + Spades, + Hearts, + Clubs, + Diamonds, +} + +#[derive(Debug, Clone)] +pub enum Value { + Ace, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, + Jack, + Queen, + King, +} + +#[derive(Debug, Clone)] +pub struct Card { + pub suit: Suit, + pub value: Value, +} + +impl Card { + pub fn new(suit: Suit, value: Value) -> Self { + Card { + suit: suit, + value: value, + } + } +} + +#[derive(Debug)] +pub struct Stack(Vec<Card>); + +impl Stack { + pub fn new(cards: Vec<Card>) -> Self { + Stack(cards) + } + pub fn shuffle(&mut self) { + let n: usize = self.0.len(); + if n == 0 { + return; + } + for i in 0..(n - 1) { + let j = random::<usize>(..) % (n - i) + i; + self.0.swap(i, j); + } + } + pub fn shuffle_in(&mut self, stack: &mut Stack) { + stack.take_cards(&mut self.0); + self.shuffle(); + } + pub fn draw(&mut self) -> Option<Card> { + self.0.iter().next().cloned(); + } + fn take_cards(&mut self, cards: &mut Vec<Card>) { + cards.append(&mut self.0.clone()); + self.0.clear(); + } +} |
