finish 84

This commit is contained in:
Dr. Matthias Ratajczak
2022-09-27 20:21:52 +02:00
parent b3354a22c0
commit 5141f9e06e
2 changed files with 20 additions and 7 deletions
+18 -5
View File
@@ -1,4 +1,5 @@
use std::collections::{HashMap, VecDeque};
use std::fmt::Display;
use std::iter::Cycle;
use rand::prelude::*;
@@ -92,6 +93,17 @@ enum Chance {
DoNothing6,
}
impl Display for Squares {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let value = *self as isize;
if value < 10 {
f.write_fmt(format_args!("0{value}"))
} else {
f.write_fmt(format_args!("{value}"))
}
}
}
#[allow(dead_code)]
#[derive(Debug)]
pub enum Dice {
@@ -176,8 +188,7 @@ impl Monopoly {
}
fn go_to_jail(&mut self) -> Squares {
let _ = (&mut self.board).skip_while(|c| *c != Squares::Jail);
Squares::Jail
self.go_to_square(Squares::Jail)
}
fn handle_chance(&mut self, square: Squares) -> Squares {
@@ -216,8 +227,7 @@ impl Monopoly {
}
fn advance_to_go(&mut self) -> Squares {
let _ = (&mut self.board).skip_while(|c| *c != Squares::Go);
Squares::Go
self.go_to_square(Squares::Go)
}
fn draw_from_chance_pile_and_put_at_bottom(&mut self) -> &Chance {
@@ -288,9 +298,11 @@ impl Statistics {
.map(|(k, v)| (k, *v as f64 / self.iterations as f64 * 100.))
.collect();
result.sort_unstable_by(|(_, v1), (_, v2)| v2.partial_cmp(v1).unwrap());
for (s, v) in result {
for (s, v) in &result {
println!("{s:?}: {v:.2}");
}
let output: String = result.iter().take(3).map(|(s, _)| s.to_string()).collect();
println!("\n{output}");
}
}
@@ -344,6 +356,7 @@ mod test {
fn custom() {
let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.go_to_square(Squares::E3), Squares::E3);
assert_eq!(monopoly.advance_by(1), Squares::R3);
}
#[test]
+2 -2
View File
@@ -1,8 +1,8 @@
use euler84::{Dice, Monopoly, Statistics};
fn main() {
let monopoly = Monopoly::new(Dice::SixSided);
let mut statistics = Statistics::new(monopoly, 50_000_000);
let monopoly = Monopoly::new(Dice::FourSided);
let mut statistics = Statistics::new(monopoly, 500_000_000);
statistics.run();
statistics.show();
}