src/main.rs
changeset 3 3d9dba5b16e0
parent 2 950660fddec0
child 4 a2f0cb2b5c13
--- a/src/main.rs	Tue Jan 31 00:15:54 2023 +0100
+++ b/src/main.rs	Tue Jan 31 22:16:26 2023 +0100
@@ -3,7 +3,7 @@
 //
 
 use core::{fmt, panic};
-use std::{collections::VecDeque, time::Instant, i64::MAX};
+use std::{collections::VecDeque, time::Instant, cmp::Reverse};
 use rand::Rng;
 use std::mem;
 
@@ -152,7 +152,7 @@
         self.content.len()
     }
 
-    fn get_nth( &mut self, n: usize ) -> Card {
+    fn _get_nth( &mut self, n: usize ) -> Card {
         if let Some(c) = self.content.remove(n) {
             c
         } else {
@@ -214,7 +214,7 @@
     }
 
     // ask the player their score
-    fn tell_points( self ) -> i32 {
+    fn _tell_points( self ) -> i32 {
         self.game_point
     }
 
@@ -266,7 +266,7 @@
         trace!("Player {} picking a row for small card, card {:?}, rows {:?}", self.name, playercard, rows);
 
         // contains the summary point for each row
-        let mut row_points = Vec::new();
+        let mut row_points = Vec::with_capacity(5);
         // the smallest row score
         let mut smallest = 999;
         // how many rows have the same smallest score
@@ -315,7 +315,7 @@
 
     fn new() -> Self {
         Row {
-            cards: VecDeque::new(),
+            cards: VecDeque::with_capacity(5),
         }
     }
 
@@ -366,7 +366,7 @@
 }
 
 impl PlayerCard {
-    fn get_player(&self) -> i32 {
+    fn _get_player(&self) -> i32 {
         self.player_id
     }
 }
@@ -395,11 +395,11 @@
 
 impl Table {
     fn new(row_cards: VecDeque<Card>) -> Self {
-        let mut rows = Vec::new();
+        let mut rows = Vec::with_capacity(5);
         for card in row_cards {
             // create a new row then put a card into it
             let mut row = Row::new();
-            if let Some(c) = row.push_or_collect(card) {
+            if let Some(_c) = row.push_or_collect(card) {
                 panic!("Freshly created row overflowed");
             }
             rows.push( row );
@@ -501,7 +501,7 @@
 
         // dealing
         debug!("Dealing.");
-        for i in 1..=10 {
+        for _i in 1..=10 {
             for player in 0 .. player_count {
                 players[player].get_card( deck.pop().expect("Deck is empty while dealing to players") );
             }
@@ -510,9 +510,9 @@
         // we need 5 crds from deck
         debug!("Building the rows.");
         let mut cards = VecDeque::new();
-        for i in 1..=5 {
+        (1..=5).for_each(|_| {
             cards.push_back( deck.pop().expect("deck empty before starting the game") );
-        }
+        });
         // println!("We push 5 cards to rows: {:?}\n", cards);
         let mut table = Table::new(cards);
 
@@ -574,7 +574,7 @@
                         players[ player_id ].give_pile( cards );
                         // put new card in the row
                         let overflow = table.put_card_into_row(smallest, rowid);
-                        if let Some(c) = overflow {
+                        if let Some(_) = overflow {
                             panic!("Player took whole row and it's already full");
                         }
                     }
@@ -635,14 +635,22 @@
  */
     }
 
-    println!("Totals (game time {} µs, or {} s), {} games played ({} shuffles):", 
+    let elapsed_micro: f64 = stats.start_time.elapsed().as_micros() as f64;
+    let game_rounds: f64 = GAME_ROUNDS.into();
+
+    let _res: f64 = stats.start_time.elapsed().as_micros() as f64 / <i32 as Into<f64>>::into(GAME_ROUNDS);
+
+    println!("Totals (game time {} µs, or {} s; {} µs/game), {} games played ({} shuffles):", 
         stats.start_time.elapsed().as_micros(), 
         stats.start_time.elapsed().as_secs(),
+        elapsed_micro / game_rounds, 
         stats.game_count,
         stats.shuffle_count,
     );
 
-    players.sort_by( |a, b| a.total_point.partial_cmp(&b.total_point).unwrap() );
+    // players.sort_by( |a, b| a.total_point.partial_cmp(&b.total_point).unwrap() );    // ASC points
+    // players.sort_by( |a, b| b.wins.partial_cmp(&a.wins).unwrap() );                  // DESC wins
+    players.sort_by_cached_key( |x| Reverse(x.wins) );                                  // DESC wins (caching is just for the show)
 
     for i in 0..players.len() {
         let p = &players[i];
@@ -652,7 +660,7 @@
 
 #[cfg(test)]
 mod tests {
-    use core::panic;
+    // use core::panic;
     use std::collections::VecDeque;
 
     use rand::Rng;
@@ -736,7 +744,7 @@
     fn sort_cards() {
         let mut cards: VecDeque<Card> = VecDeque::new();
         let mut rng = rand::thread_rng();
-        for i in 1..50 {
+        for _ in 1..50 {
             let n = rng.gen_range(1..104);
             cards.push_back( Card::new(n) );
         }