From 1c70d82a1ea4897631b113c0ee564fce17b89de0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sat, 2 Jul 2022 10:51:13 +0200 Subject: [PATCH] Create Prisoner structure to save results. --- src/main.rs | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index aa90c2a..2d93e5d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ extern crate rand; use structopt::StructOpt; +// define the commandline parameters #[derive(StructOpt)] #[structopt(about = "An implementation of the Skyum's Protocol.")] struct Opt @@ -34,7 +35,17 @@ struct Opt size:u32, } +// define the prisoner data +struct Prisoner +{ + // if the prisoner found his number + found:bool, + // number of attempt he found it in + attempts:u32, +} + fn main() { + // read the commandline parameters let opts = Opt::from_args(); // set number of prisoners in experiment @@ -58,23 +69,30 @@ fn main() { } } - // assume that all the prisoners have found their number - let mut all_found = true; + // create the vector of prisoners + let mut prisoners:Vec = Vec::with_capacity(size as usize); + // for every prisoner in the jail for i in 0..size { + // create the prisoner for index `i` + let mut prisoner_i = Prisoner { + // assume the prisoner does not find his box + found: false, + // set number of attempts to 0 + attempts: 0, + }; // the next (first) box the prisoner is to open is the one with his number (`i`) let mut next_box:usize = i as usize; - // assume the prisoner does not find his box - let mut found = false; // for every attempt the prisoner has (i.e. `size/2`) for _j in 0..(size/2) { + prisoner_i.attempts += 1; // if the number inside the box is his number if boxes[next_box] == i { // assign `found` to true - found = true; + prisoner_i.found = true; // stop looking through more boxes. break; } @@ -84,20 +102,18 @@ fn main() { next_box = boxes[next_box] as usize; } } - // if a prisoner has not found his number in `size/2` attempts - if !found - { - all_found = false; - } + + // add prisoner to list of prisoners in experiment + prisoners.push(prisoner_i); } // print result - if all_found - { - println!("success"); - } - else + if prisoners.iter().any(|i| !i.found) { println!("failure"); } + else + { + println!("success"); + } }