瀏覽代碼

Added scoring system

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
     players
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
     let cards_per_player = players[0].hand.len();
47
     let cards_per_player = players[0].hand.len();
47
     // Check if all players have same number of cards
48
     // Check if all players have same number of cards
48
     for i in 1..params::NUMBER_OF_PLAYERS {
49
     for i in 1..params::NUMBER_OF_PLAYERS {
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
 // Keeps the order of the game but puts starting player first
130
 // Keeps the order of the game but puts starting player first

+ 12 - 1
src/main.rs

3
 mod params;
3
 mod params;
4
 mod game;
4
 mod game;
5
 mod ai;
5
 mod ai;
6
+mod scoring;
6
 
7
 
7
 extern crate rand;
8
 extern crate rand;
8
 
9
 
26
             println!("{}", card);
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

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
+}