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
 use std::fmt;
1
 use std::fmt;
2
 use std::cmp::Ordering;
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
 pub enum Suit {
5
 pub enum Suit {
6
     Clubs,
6
     Clubs,
7
     Diamonds,
7
     Diamonds,
26
     }
26
     }
27
 }
27
 }
28
 
28
 
29
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
29
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
30
 pub enum Symbol {
30
 pub enum Symbol {
31
     Ace,
31
     Ace,
32
     Two,
32
     Two,
84
     }
84
     }
85
 }
85
 }
86
 
86
 
87
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)]
87
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
88
 pub struct Face {
88
 pub struct Face {
89
     pub suit: Suit,
89
     pub suit: Suit,
90
     pub symbol: Symbol,
90
     pub symbol: Symbol,
93
 impl Ord for Face {
93
 impl Ord for Face {
94
     fn cmp(&self, face: &Face) -> Ordering {
94
     fn cmp(&self, face: &Face) -> Ordering {
95
         match self.symbol.cmp(&face.symbol) {
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
             Ordering::Less => Ordering::Less,
97
             Ordering::Less => Ordering::Less,
104
             Ordering::Greater => Ordering::Greater,
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
 pub enum Trump {
104
 pub enum Trump {
111
     One,
105
     One,
112
     Two,
106
     Two,
185
     }
179
     }
186
 }
180
 }
187
 
181
 
188
-#[derive(Copy, Clone, Eq, PartialEq, PartialOrd)]
182
+#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd)]
189
 pub enum Card {
183
 pub enum Card {
190
     Face(Face),
184
     Face(Face),
191
     Trump(Trump),
185
     Trump(Trump),

+ 20 - 6
src/game.rs

27
             new_hands[i - starting_player % params::NUMBER_OF_PLAYERS] = new_hand;
27
             new_hands[i - starting_player % params::NUMBER_OF_PLAYERS] = new_hand;
28
             new_heap
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
     hand
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
 fn winning_card(cards: Heap) -> usize {
58
 fn winning_card(cards: Heap) -> usize {
45
     0
59
     0
46
 }
60
 }

+ 1 - 1
src/main.rs

17
         println!("{}", card);
17
         println!("{}", card);
18
     }
18
     }
19
     for &(number, ref hand) in hands.into_iter() {
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
         for card in hand.iter() {
21
         for card in hand.iter() {
22
             println!("{}", card);
22
             println!("{}", card);
23
         }
23
         }