Przeglądaj źródła

Setup skeleton for game playing

Denis Merigoux 9 lat temu
rodzic
commit
46e3d08710
5 zmienionych plików z 86 dodań i 20 usunięć
  1. 11 1
      src/card.rs
  2. 14 15
      src/deck.rs
  3. 46 0
      src/game.rs
  4. 11 4
      src/main.rs
  5. 4 0
      src/params.rs

+ 11 - 1
src/card.rs

@@ -92,7 +92,17 @@ pub struct Face {
92 92
 
93 93
 impl Ord for Face {
94 94
     fn cmp(&self, face: &Face) -> Ordering {
95
-        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
+            }
103
+            Ordering::Less => Ordering::Less,
104
+            Ordering::Greater => Ordering::Greater,
105
+        }
96 106
     }
97 107
 }
98 108
 

+ 14 - 15
src/deck.rs

@@ -1,17 +1,14 @@
1 1
 use card;
2
+use params;
2 3
 use rand::{thread_rng, Rng};
3 4
 use std::collections::BTreeSet;
4 5
 
5
-pub const DECK_SIZE: usize = 78;
6
-pub const NUMBER_OF_PLAYERS: usize = 4;
7
-pub const CARDS_PER_DISTRIBUTION: usize = 3;
8
-pub type Deck = [card::Card; DECK_SIZE];
9
-pub type Hand = BTreeSet<card::Card>;
10
-
6
+pub type Deck = [card::Card; params::DECK_SIZE];
7
+pub type Hand = (i32, BTreeSet<card::Card>);
11 8
 
12 9
 // Creates a deck_size cards tarot deck, shuffled
13 10
 pub fn new_deck() -> Deck {
14
-    let mut deck: [card::Card; DECK_SIZE] = [card::Card::Fool; DECK_SIZE];
11
+    let mut deck: [card::Card; params::DECK_SIZE] = [card::Card::Fool; params::DECK_SIZE];
15 12
     let mut counter = 0;
16 13
     for suit in card::Suit::values() {
17 14
         for symbol in card::Symbol::values() {
@@ -33,16 +30,18 @@ pub fn new_deck() -> Deck {
33 30
 }
34 31
 
35 32
 // Distributes the deck between the four players and the dog
36
-pub fn distribute_cards(deck: Deck) -> (Hand, [Hand; 4]) {
37
-    let mut dog: Hand = BTreeSet::new();
38
-    let mut hands: [Hand; NUMBER_OF_PLAYERS] =
39
-        [BTreeSet::new(), BTreeSet::new(), BTreeSet::new(), BTreeSet::new()];
40
-    for i in 0..(DECK_SIZE - 1) {
41
-        if i <= 5 {
33
+pub fn distribute_cards(deck: Deck) -> (Hand, [Hand; params::NUMBER_OF_PLAYERS]) {
34
+    let (_, mut dog): Hand = (-1, BTreeSet::new());
35
+    let mut hands: [Hand; params::NUMBER_OF_PLAYERS] =
36
+        [(0, BTreeSet::new()), (1, BTreeSet::new()), (2, BTreeSet::new()), (3, BTreeSet::new())];
37
+    for i in 0..params::DECK_SIZE {
38
+        if i < params::DOG_SIZE {
42 39
             dog.insert(deck[i]);
43 40
         } else {
44
-            hands[(i / CARDS_PER_DISTRIBUTION) % NUMBER_OF_PLAYERS].insert(deck[i]);
41
+            hands[(i / params::CARDS_PER_DISTRIBUTION) % params::NUMBER_OF_PLAYERS]
42
+                .1
43
+                .insert(deck[i]);
45 44
         }
46 45
     }
47
-    (dog, hands)
46
+    ((-1, dog), hands)
48 47
 }

+ 46 - 0
src/game.rs

@@ -0,0 +1,46 @@
1
+use params;
2
+use deck;
3
+use card;
4
+use std::collections::BTreeSet;
5
+
6
+type Heap = Vec<card::Card>;
7
+
8
+pub fn start_game(dog: deck::Hand, hands: [deck::Hand; params::NUMBER_OF_PLAYERS]) {
9
+    let cards_per_player = hands[0].1.len();
10
+    //Check if all players have same number of cards
11
+    for i in 1..params::NUMBER_OF_PLAYERS {
12
+        if cards_per_player != hands[i].1.len() {
13
+            panic!("the players don't have the same number of cards")
14
+        }
15
+    }
16
+    //If yes start the game
17
+    let starting_player = 0;
18
+    (1..cards_per_player).fold((starting_player, hands), |(starting_player, hands), _| {
19
+        //For each turn
20
+        let mut new_hands: [deck::Hand; params::NUMBER_OF_PLAYERS] = [(0, BTreeSet::new()),
21
+                                                                      (1, BTreeSet::new()),
22
+                                                                      (2, BTreeSet::new()),
23
+                                                                      (3, BTreeSet::new())];
24
+        let end_heap = hands.into_iter().enumerate().fold(Vec::new(), |heap, (i, hand)| {
25
+            // For each player
26
+            let (new_hand, new_heap) = play_card(hand.clone(), heap);
27
+            new_hands[i - starting_player % params::NUMBER_OF_PLAYERS] = new_hand;
28
+            new_heap
29
+        });
30
+        let new_starting_player = winning_card(end_heap);
31
+        (new_starting_player, hands)
32
+    });
33
+}
34
+
35
+fn play_card(hand: deck::Hand, cards_played: Heap) -> (deck::Hand, Heap) {
36
+    (hand, cards_played)
37
+}
38
+
39
+fn valid_cards(hand: deck::Hand, cards_played: Heap) -> (deck::Hand) {
40
+    //TODO
41
+    hand
42
+}
43
+
44
+fn winning_card(cards: Heap) -> usize {
45
+    0
46
+}

+ 11 - 4
src/main.rs

@@ -1,19 +1,26 @@
1 1
 mod card;
2 2
 mod deck;
3
+mod params;
4
+mod game;
3 5
 
4 6
 extern crate rand;
5 7
 
8
+pub const DECK_SIZE: usize = 78;
9
+pub const NUMBER_OF_PLAYERS: usize = 4;
10
+pub const CARDS_PER_DISTRIBUTION: usize = 3;
11
+
6 12
 fn main() {
7 13
     let deck = deck::new_deck();
8
-    let (dog, hands) = deck::distribute_cards(deck);
14
+    let ((_, dog), hands) = deck::distribute_cards(deck);
9 15
     println!("-> Cartes du chien :");
10 16
     for card in dog.iter() {
11 17
         println!("{}", card);
12 18
     }
13
-    for i in 0..(deck::NUMBER_OF_PLAYERS) {
14
-        println!("-> Cartes du joueur {} :", i + 1);
15
-        for card in hands[i].iter() {
19
+    for &(number, ref hand) in hands.into_iter() {
20
+        println!("-> Cartes du joueur {} ({}):", number + 1, hand.len());
21
+        for card in hand.iter() {
16 22
             println!("{}", card);
17 23
         }
18 24
     }
25
+    game::start_game((-1, dog), hands);
19 26
 }

+ 4 - 0
src/params.rs

@@ -0,0 +1,4 @@
1
+pub const DECK_SIZE: usize = 78;
2
+pub const DOG_SIZE: usize = 6;
3
+pub const NUMBER_OF_PLAYERS: usize = 4;
4
+pub const CARDS_PER_DISTRIBUTION: usize = 3;