Denis Merigoux преди 9 години
родител
ревизия
9214bc550d
променени са 3 файла, в които са добавени 62 реда и са изтрити 21 реда
  1. 3 20
      src/game.rs
  2. 12 1
      src/main.rs
  3. 47 0
      src/scoring.rs

+ 3 - 20
src/game.rs

@@ -42,7 +42,8 @@ pub fn initialize_players(hands: &mut Vec<deck::Hand>, names: &mut Vec<String>)
42 42
     players
43 43
 }
44 44
 
45
-pub fn start_game(mut dog: &mut deck::Hand, mut players: &mut Vec<Player>) {
45
+//Plays the game and returns the tuple (attack_heap,defense_heap)
46
+pub fn play_game(mut dog: &mut deck::Hand, mut players: &mut Vec<Player>) -> Game {
46 47
     let cards_per_player = players[0].hand.len();
47 48
     // Check if all players have same number of cards
48 49
     for i in 1..params::NUMBER_OF_PLAYERS {
@@ -123,25 +124,7 @@ pub fn start_game(mut dog: &mut deck::Hand, mut players: &mut Vec<Player>) {
123 124
             }
124 125
         }
125 126
     }
126
-
127
-    println!("-> Cartes gagnées par l'attaque");
128
-    for &(card, id) in game.attack_heap.iter() {
129
-        println!("{} ({})",
130
-                 card,
131
-                 match players.iter().find(|player| player.id == id) {
132
-                     None => panic!("cannot find the player who won the round"),
133
-                     Some(player) => &player.name,
134
-                 });
135
-    }
136
-    println!("-> Cartes gagnées par la défense");
137
-    for &(card, id) in game.defense_heap.iter() {
138
-        println!("{} ({})",
139
-                 card,
140
-                 match players.iter().find(|player| player.id == id) {
141
-                     None => panic!("cannot find the player who won the round"),
142
-                     Some(player) => &player.name,
143
-                 });
144
-    }
127
+    game
145 128
 }
146 129
 
147 130
 // Keeps the order of the game but puts starting player first

+ 12 - 1
src/main.rs

@@ -3,6 +3,7 @@ mod deck;
3 3
 mod params;
4 4
 mod game;
5 5
 mod ai;
6
+mod scoring;
6 7
 
7 8
 extern crate rand;
8 9
 
@@ -26,5 +27,15 @@ fn main() {
26 27
             println!("{}", card);
27 28
         }
28 29
     }
29
-    game::start_game(&mut dog, &mut players);
30
+    let game = game::play_game(&mut dog, &mut players);
31
+    println!("===== Fin de la partie ! =====");
32
+    let game_scores = scoring::compute_game_scores(&game);
33
+    println!("Score de l'attaque ({}) : {} ({} bouts), score de la défense : {}",
34
+             match players.iter().find(|player| player.id == game.taker_id) {
35
+                 None => panic!("cannot find the player who won the round"),
36
+                 Some(player) => &player.name,
37
+             },
38
+             game_scores.attack_score,
39
+             game_scores.attack_bouts_number,
40
+             game_scores.defense_score);
30 41
 }

+ 47 - 0
src/scoring.rs

@@ -0,0 +1,47 @@
1
+use game;
2
+use card;
3
+
4
+pub struct GameScores {
5
+    pub attack_score: f32,
6
+    pub defense_score: f32,
7
+    pub attack_bouts_number: i32,
8
+}
9
+
10
+pub fn compute_game_scores(game: &game::Game) -> GameScores {
11
+    let mut game_scores: GameScores = GameScores {
12
+        attack_score: 0 as f32,
13
+        defense_score: 0 as f32,
14
+        attack_bouts_number: 0,
15
+    };
16
+    for &(card, _) in game.attack_heap.iter() {
17
+        game_scores.attack_score += card_score(&card);
18
+        match card {
19
+            card::Card::Fool |
20
+            card::Card::Trump(card::Trump::One) |
21
+            card::Card::Trump(card::Trump::TwentyOne) => game_scores.attack_bouts_number += 1,
22
+            _ => (),
23
+        }
24
+    }
25
+    for &(card, _) in game.defense_heap.iter() {
26
+        game_scores.defense_score += card_score(&card);
27
+    }
28
+    game_scores
29
+}
30
+
31
+fn card_score(&card: &card::Card) -> f32 {
32
+    match card {
33
+        card::Card::Face(card::Face { suit: _, symbol }) => {
34
+            match symbol {
35
+                card::Symbol::Jack => 1.5,
36
+                card::Symbol::Knight => 2.5,
37
+                card::Symbol::Queen => 3.5,
38
+                card::Symbol::King => 4.5,
39
+                _ => 0.5,
40
+            }
41
+        }
42
+        card::Card::Fool |
43
+        card::Card::Trump(card::Trump::One) |
44
+        card::Card::Trump(card::Trump::TwentyOne) => 4.5,
45
+        _ => 0.5,
46
+    }
47
+}