robin4002

Untitled

Apr 7th, 2020
870
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use rand::Rng;
  2.  
  3. fn main() {
  4.    
  5.     let cash_machines = vec![CashMachine::new(1), CashMachine::new(2), CashMachine::new(3), CashMachine::new(4), CashMachine::new(5)];
  6.  
  7.     // Methode B
  8.     let pre_score_method_b = cash_machines.iter().map(|machine| {
  9.         let score = machine.play_x_time(10);
  10.         println!("Score pour la machine {} : {}", machine.number, score);
  11.         score
  12.     }).collect::<Vec<i32>>();
  13.  
  14.     let max = pre_score_method_b.iter().enumerate().map(|(x, y)| (y, x)).max().unwrap();
  15.     println!("Meilleur machine d'après méthode B: {} avec score de {}", (max.1 + 1), max.0);
  16.     let score_method_a: i32 = pre_score_method_b.iter().sum::<i32>() + cash_machines[max.1].play_x_time(500_000 - 50);
  17.     println!("Score méthode B : {}", score_method_a);
  18.  
  19.     // method C
  20.     let pre_score_method_c = cash_machines.iter().map(|machine| {
  21.         let score = machine.play_x_time(1_000);
  22.         println!("Score pour la machine {} : {}", machine.number, score);
  23.         score
  24.     }).collect::<Vec<i32>>();
  25.  
  26.     let max = pre_score_method_c.iter().enumerate().map(|(x, y)| (y, x)).max().unwrap();
  27.     println!("Meilleur machine d'après méthode C: {} avec score de {}", (max.1 + 1), max.0);
  28.     let score_method_c: i32 = pre_score_method_c.iter().sum::<i32>() + cash_machines[max.1].play_x_time(500_000 - 5_000);
  29.     println!("Score méthode C : {}", score_method_c);
  30.  
  31.     // method D
  32.     let score_method_d = cash_machines.iter().map(|machine| {
  33.         let score = machine.play_x_time(100_000);
  34.         println!("Score pour la machine {} : {}", machine.number, score);
  35.         score
  36.     }).sum::<i32>();
  37.     println!("Score méthode D : {}", score_method_d);
  38.  
  39. }
  40.  
  41. enum PlayResult {
  42.     WIN,
  43.     LOOSE
  44. }
  45.  
  46. struct CashMachine {
  47.     number: i8,
  48.     max: f32,
  49. }
  50.  
  51. impl CashMachine {
  52.     pub fn new(number: i8) -> CashMachine {
  53.         let mut rng = rand::thread_rng();
  54.         CashMachine {
  55.             number: number,
  56.             max: rng.gen::<f32>(),
  57.         }
  58.     }
  59.  
  60.     pub fn play(&self) -> PlayResult {
  61.         let mut rng = rand::thread_rng();
  62.         if rng.gen::<f32>() < self.max {
  63.             PlayResult::WIN
  64.         }
  65.         else {
  66.             PlayResult::LOOSE
  67.         }
  68.     }
  69.  
  70.     pub fn play_x_time(&self, time: i32) -> i32 {
  71.         let mut score: i32 = 0;
  72.         for _ in 1..=time {
  73.             if let PlayResult::WIN = self.play() {
  74.                 score += 1
  75.             }
  76.         }
  77.         score
  78.     }
  79. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×