Denis Merigoux 9 роки тому
батько
комміт
96f0ad2449
3 змінених файлів з 27 додано та 19 видалено
  1. 6 12
      src/card.rs
  2. 20 6
      src/game.rs
  3. 1 1
      src/main.rs

+ 6 - 12
src/card.rs

@@ -1,7 +1,7 @@
1 1
 use std::fmt;
2 2
 use std::cmp::Ordering;
3 3
 
4
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
4
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
5 5
 pub enum Suit {
6 6
     Clubs,
7 7
     Diamonds,
@@ -26,7 +26,7 @@ impl Suit {
26 26
     }
27 27
 }
28 28
 
29
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
29
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
30 30
 pub enum Symbol {
31 31
     Ace,
32 32
     Two,
@@ -84,7 +84,7 @@ impl Symbol {
84 84
     }
85 85
 }
86 86
 
87
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)]
87
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
88 88
 pub struct Face {
89 89
     pub suit: Suit,
90 90
     pub symbol: Symbol,
@@ -93,20 +93,14 @@ pub struct Face {
93 93
 impl Ord for Face {
94 94
     fn cmp(&self, face: &Face) -> Ordering {
95 95
         match self.symbol.cmp(&face.symbol) {
96
-            Ordering::Equal => {
97
-                let result = self.suit.cmp(&face.suit);
98
-                if result == Ordering::Equal {
99
-                    println!("Equal cards!");
100
-                }
101
-                result
102
-            }
96
+            Ordering::Equal => self.suit.cmp(&face.suit),
103 97
             Ordering::Less => Ordering::Less,
104 98
             Ordering::Greater => Ordering::Greater,
105 99
         }
106 100
     }
107 101
 }
108 102
 
109
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
103
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
110 104
 pub enum Trump {
111 105
     One,
112 106
     Two,
@@ -185,7 +179,7 @@ impl Trump {
185 179
     }
186 180
 }
187 181
 
188
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)]
182
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
189 183
 pub enum Card {
190 184
     Face(Face),
191 185
     Trump(Trump),

+ 20 - 6
src/game.rs

@@ -27,20 +27,34 @@ pub fn start_game(dog: deck::Hand, hands: [deck::Hand; params::NUMBER_OF_PLAYERS
27 27
             new_hands[i - starting_player % params::NUMBER_OF_PLAYERS] = new_hand;
28 28
             new_heap
29 29
         });
30
-        let new_starting_player = winning_card(end_heap);
31
-        (new_starting_player, hands)
30
+        let new_starting_player = winning_card(end_heap.clone());
31
+        println!("Les joueurs ont joué : {:?}", end_heap);
32
+        (new_starting_player, new_hands)
32 33
     });
33 34
 }
34 35
 
35
-fn play_card(hand: deck::Hand, cards_played: Heap) -> (deck::Hand, Heap) {
36
-    (hand, cards_played)
36
+fn play_card((player, hand): deck::Hand, cards_played: Heap) -> (deck::Hand, Heap) {
37
+    let valid_cards = valid_cards((player, hand.clone()), cards_played.clone());
38
+    let card_to_play = select_card(valid_cards, cards_played.clone());
39
+    println!("Card to play: {:?}", card_to_play.clone());
40
+    let mut new_hand = hand.clone();
41
+    new_hand.take(&card_to_play);
42
+    let mut new_cards_played = cards_played.clone();
43
+    new_cards_played.push(card_to_play);
44
+    ((player, new_hand), new_cards_played)
37 45
 }
38 46
 
39
-fn valid_cards(hand: deck::Hand, cards_played: Heap) -> (deck::Hand) {
40
-    //TODO
47
+fn valid_cards(hand: deck::Hand, cards_played: Heap) -> deck::Hand {
41 48
     hand
42 49
 }
43 50
 
51
+fn select_card((player, valid_cards): deck::Hand, cards_played: Heap) -> card::Card {
52
+    match valid_cards.into_iter().next() {
53
+        Some(card) => card,
54
+        None => panic!("there is no valid card to play"),
55
+    }
56
+}
57
+
44 58
 fn winning_card(cards: Heap) -> usize {
45 59
     0
46 60
 }

+ 1 - 1
src/main.rs

@@ -17,7 +17,7 @@ fn main() {
17 17
         println!("{}", card);
18 18
     }
19 19
     for &(number, ref hand) in hands.into_iter() {
20
-        println!("-> Cartes du joueur {} ({}):", number + 1, hand.len());
20
+        println!("-> Cartes du joueur {}:", number + 1);
21 21
         for card in hand.iter() {
22 22
             println!("{}", card);
23 23
         }