summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: e8f751116a6adf54a3e7c24927acc9d471ced84a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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();
    }
}