Selaa lähdekoodia

Creation of shuffled decks

Denis Merigoux 9 vuotta sitten
vanhempi
commit
a21a211158

+ 1 - 1
.gitignore

@@ -1 +1 @@
1
-main
1
+target/

+ 19 - 0
Cargo.lock

@@ -1,4 +1,23 @@
1 1
 [root]
2 2
 name = "tarot"
3 3
 version = "0.0.1"
4
+dependencies = [
5
+ "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
6
+]
4 7
 
8
+[[package]]
9
+name = "libc"
10
+version = "0.2.19"
11
+source = "registry+https://github.com/rust-lang/crates.io-index"
12
+
13
+[[package]]
14
+name = "rand"
15
+version = "0.3.15"
16
+source = "registry+https://github.com/rust-lang/crates.io-index"
17
+dependencies = [
18
+ "libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)",
19
+]
20
+
21
+[metadata]
22
+"checksum libc 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "9e030dc72013ed68994d1b2cbf36a94dd0e58418ba949c4b0db7eeb70a7a6352"
23
+"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"

+ 2 - 0
Cargo.toml

@@ -5,3 +5,5 @@ version = "0.0.1"
5 5
 authors = [ "Denis Merigoux <denis.merigoux@gmail.com>" ]
6 6
 
7 7
 [dependencies]
8
+
9
+rand = "0.3"

+ 57 - 1
src/card.rs

@@ -1,5 +1,6 @@
1 1
 use std::fmt;
2 2
 
3
+#[derive(Copy, Clone)]
3 4
 pub enum Suit {
4 5
     Clubs,
5 6
     Diamonds,
@@ -18,6 +19,13 @@ impl fmt::Display for Suit {
18 19
     }
19 20
 }
20 21
 
22
+impl Suit {
23
+    pub fn values() -> Vec<Suit> {
24
+        vec![Suit::Clubs, Suit::Diamonds, Suit::Hearts, Suit::Spades]
25
+    }
26
+}
27
+
28
+#[derive(Copy, Clone)]
21 29
 pub enum Symbol {
22 30
     Ace,
23 31
     Two,
@@ -56,11 +64,32 @@ impl fmt::Display for Symbol {
56 64
     }
57 65
 }
58 66
 
67
+impl Symbol {
68
+    pub fn values() -> Vec<Symbol> {
69
+        vec![Symbol::Ace,
70
+             Symbol::Two,
71
+             Symbol::Three,
72
+             Symbol::Four,
73
+             Symbol::Five,
74
+             Symbol::Six,
75
+             Symbol::Seven,
76
+             Symbol::Eight,
77
+             Symbol::Nine,
78
+             Symbol::Ten,
79
+             Symbol::Jack,
80
+             Symbol::Knight,
81
+             Symbol::Queen,
82
+             Symbol::King]
83
+    }
84
+}
85
+
86
+#[derive(Copy, Clone)]
59 87
 pub struct Face {
60 88
     pub suit: Suit,
61 89
     pub symbol: Symbol,
62 90
 }
63 91
 
92
+#[derive(Copy, Clone)]
64 93
 pub enum Trump {
65 94
     One,
66 95
     Two,
@@ -113,6 +142,33 @@ impl fmt::Display for Trump {
113 142
     }
114 143
 }
115 144
 
145
+impl Trump {
146
+    pub fn values() -> Vec<Trump> {
147
+        vec![Trump::One,
148
+             Trump::Two,
149
+             Trump::Three,
150
+             Trump::Four,
151
+             Trump::Five,
152
+             Trump::Six,
153
+             Trump::Seven,
154
+             Trump::Eight,
155
+             Trump::Nine,
156
+             Trump::Ten,
157
+             Trump::Eleven,
158
+             Trump::Twelve,
159
+             Trump::Thirteen,
160
+             Trump::Fourteen,
161
+             Trump::Fifteen,
162
+             Trump::Sixteen,
163
+             Trump::Seventeen,
164
+             Trump::Eighteen,
165
+             Trump::Nineteen,
166
+             Trump::Twenty,
167
+             Trump::TwentyOne]
168
+    }
169
+}
170
+
171
+#[derive(Copy, Clone)]
116 172
 pub enum Card {
117 173
     Face(Face),
118 174
     Trump(Trump),
@@ -122,7 +178,7 @@ pub enum Card {
122 178
 impl fmt::Display for Card {
123 179
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
124 180
         match self {
125
-            &Card::Fool => write!(f, "Excuse"),
181
+            &Card::Fool => write!(f, "excuse"),
126 182
             &Card::Face(Face { ref suit, ref symbol }) => write!(f, "{} de {}", symbol, suit),
127 183
             &Card::Trump(Trump::One) => write!(f, "petit"),
128 184
             &Card::Trump(ref trump) => write!(f, "{} d'atout", trump),

+ 21 - 0
src/deck.rs

@@ -0,0 +1,21 @@
1
+use card;
2
+use rand::{thread_rng, Rng};
3
+
4
+pub fn new_deck() -> Vec<card::Card> {
5
+    let mut deck: Vec<card::Card> = Vec::new();
6
+    for suit in card::Suit::values() {
7
+        for symbol in card::Symbol::values() {
8
+            deck.push(card::Card::Face(card::Face {
9
+                suit: suit,
10
+                symbol: symbol,
11
+            }))
12
+        }
13
+    }
14
+    for trump in card::Trump::values() {
15
+        deck.push(card::Card::Trump(trump))
16
+    }
17
+    deck.push(card::Card::Fool);
18
+    let mut rng = thread_rng();
19
+    rng.shuffle(&mut deck);
20
+    deck
21
+}

+ 7 - 6
src/main.rs

@@ -1,10 +1,11 @@
1 1
 mod card;
2
+mod deck;
3
+
4
+extern crate rand;
2 5
 
3 6
 fn main() {
4
-    let card1: card::Card = card::Card::Face(card::Face {
5
-        suit: card::Suit::Hearts,
6
-        symbol: card::Symbol::Seven,
7
-    });
8
-    let card2: card::Card = card::Card::Trump(card::Trump::Two);
9
-    println!("{} et {}", card1, card2);
7
+    let mut deck = deck::new_deck();
8
+    for card in deck {
9
+        println!("{}", card)
10
+    }
10 11
 }

+ 0 - 0
target/debug/.cargo-lock


+ 0 - 1
target/debug/.fingerprint/tarot-a702c14d4f6a8863/bin-tarot

@@ -1 +0,0 @@
1
-1c78290e0bdba768

+ 0 - 1
target/debug/.fingerprint/tarot-a702c14d4f6a8863/bin-tarot.json

@@ -1 +0,0 @@
1
-{"rustc":16218068117412374134,"target":5225145117374096408,"profile":11154289914177168617,"local":{"variant":"MtimeBased","fields":[[1484630663,662815526],[47,104,111,109,101,47,100,101,110,105,115,47,116,97,114,111,116,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,116,97,114,111,116,45,97,55,48,50,99,49,52,100,52,102,54,97,56,56,54,51,47,100,101,112,45,98,105,110,45,116,97,114,111,116]]},"features":"None","deps":[],"rustflags":[]}

BIN
target/debug/.fingerprint/tarot-a702c14d4f6a8863/dep-bin-tarot


BIN
target/debug/tarot


+ 0 - 0
target/release/.cargo-lock


+ 0 - 1
target/release/.fingerprint/tarot-a702c14d4f6a8863/bin-tarot

@@ -1 +0,0 @@
1
-df0a7db364e1d3ab

+ 0 - 1
target/release/.fingerprint/tarot-a702c14d4f6a8863/bin-tarot.json

@@ -1 +0,0 @@
1
-{"rustc":16218068117412374134,"target":5225145117374096408,"profile":2761435531286440798,"local":{"variant":"MtimeBased","fields":[[1484626449,466378924],[47,104,111,109,101,47,100,101,110,105,115,47,116,97,114,111,116,47,116,97,114,103,101,116,47,114,101,108,101,97,115,101,47,46,102,105,110,103,101,114,112,114,105,110,116,47,116,97,114,111,116,45,97,55,48,50,99,49,52,100,52,102,54,97,56,56,54,51,47,100,101,112,45,98,105,110,45,116,97,114,111,116]]},"features":"None","deps":[],"rustflags":[]}

BIN
target/release/.fingerprint/tarot-a702c14d4f6a8863/dep-bin-tarot


BIN
target/release/tarot