add more code for 84

still does not work (WIP)
This commit is contained in:
Dr. Matthias Ratajczak
2022-09-27 19:53:09 +02:00
parent a5c27e42b4
commit 1edca7b0ed
2 changed files with 53 additions and 21 deletions
+52 -20
View File
@@ -92,8 +92,9 @@ enum Chance {
DoNothing6, DoNothing6,
} }
#[allow(dead_code)]
#[derive(Debug)] #[derive(Debug)]
enum Dice { pub enum Dice {
FourSided = 4, FourSided = 4,
SixSided = 6, SixSided = 6,
} }
@@ -126,14 +127,16 @@ impl Monopoly {
} }
#[allow(clippy::new_without_default)] #[allow(clippy::new_without_default)]
pub fn new() -> Self { pub fn new(dice: Dice) -> Self {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let community_pile = Self::gen_random_community_pile(&mut rng); let community_pile = Self::gen_random_community_pile(&mut rng);
let chance_pile = Self::gen_random_chance_pile(&mut rng); let chance_pile = Self::gen_random_chance_pile(&mut rng);
let mut board = Squares::iter().cycle();
let _ = board.next(); // make sure we are on "Go"
Self { Self {
rng, rng,
board: Squares::iter().cycle(), board,
dice: Dice::SixSided, dice,
community_pile, community_pile,
chance_pile, chance_pile,
number_consecutive_doubles: 0, number_consecutive_doubles: 0,
@@ -159,7 +162,7 @@ impl Monopoly {
return self.go_to_jail(); return self.go_to_jail();
} }
let square = self.board.nth(distance).unwrap(); let square = self.advance_by(distance);
match square { match square {
Squares::G2J => self.go_to_jail(), Squares::G2J => self.go_to_jail(),
Squares::CC1 => self.handle_community_chest(Squares::CC1), Squares::CC1 => self.handle_community_chest(Squares::CC1),
@@ -193,8 +196,7 @@ impl Monopoly {
} }
fn go_to_square(&mut self, square: Squares) -> Squares { fn go_to_square(&mut self, square: Squares) -> Squares {
let _ = (&mut self.board).skip_while(|c| *c != square); self.board.find(|c| *c == square).unwrap()
square
} }
fn go_to_squares(&mut self, squares: &[Squares]) -> Squares { fn go_to_squares(&mut self, squares: &[Squares]) -> Squares {
@@ -251,9 +253,8 @@ impl Monopoly {
} }
} }
#[cfg(test)] fn advance_by(&mut self, number: usize) -> Squares {
fn advance_by_number(&mut self, number: usize) -> Squares { self.board.nth(number - 1).unwrap()
self.board.nth(number).unwrap()
} }
} }
@@ -268,7 +269,7 @@ impl Statistics {
Self { Self {
monopoly, monopoly,
iterations, iterations,
fields_count: HashMap::from_iter(Squares::iter().zip([0].into_iter().cycle())), fields_count: Squares::iter().zip([0].into_iter().cycle()).collect(),
} }
} }
@@ -293,19 +294,19 @@ impl Statistics {
} }
#[cfg(test)] #[cfg(test)]
pub mod test { mod test {
use super::{Monopoly, Squares}; use super::{Dice, Monopoly, Squares, NUMBER_SQUARES};
#[test] #[test]
fn jail() { fn jail() {
let mut monopoly = Monopoly::new(); let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.go_to_jail(), Squares::Jail); assert_eq!(monopoly.go_to_jail(), Squares::Jail);
assert_eq!(monopoly.go_to_jail(), Squares::Jail); assert_eq!(monopoly.go_to_jail(), Squares::Jail);
} }
#[test] #[test]
fn utility() { fn utility() {
let mut monopoly = Monopoly::new(); let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.go_to_utility(), Squares::U1); assert_eq!(monopoly.go_to_utility(), Squares::U1);
assert_eq!(monopoly.go_to_utility(), Squares::U2); assert_eq!(monopoly.go_to_utility(), Squares::U2);
assert_eq!(monopoly.go_to_utility(), Squares::U1); assert_eq!(monopoly.go_to_utility(), Squares::U1);
@@ -313,7 +314,7 @@ pub mod test {
#[test] #[test]
fn railway() { fn railway() {
let mut monopoly = Monopoly::new(); let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.go_to_railway(), Squares::R1); assert_eq!(monopoly.go_to_railway(), Squares::R1);
assert_eq!(monopoly.go_to_railway(), Squares::R2); assert_eq!(monopoly.go_to_railway(), Squares::R2);
assert_eq!(monopoly.go_to_railway(), Squares::R3); assert_eq!(monopoly.go_to_railway(), Squares::R3);
@@ -323,18 +324,49 @@ pub mod test {
#[test] #[test]
fn go() { fn go() {
let mut monopoly = Monopoly::new(); let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.advance_to_go(), Squares::Go); assert_eq!(monopoly.advance_to_go(), Squares::Go);
assert_eq!(monopoly.advance_to_go(), Squares::Go); assert_eq!(monopoly.advance_to_go(), Squares::Go);
} }
#[test] #[test]
fn back_3() { fn back_3() {
let mut monopoly = Monopoly::new(); let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.advance_by_number(3) as isize, 3); assert_eq!(monopoly.advance_by(3) as isize, 3);
assert_eq!(monopoly.go_back_3_squares(), Squares::Go); assert_eq!(monopoly.go_back_3_squares(), Squares::Go);
assert_eq!(monopoly.advance_by_number(27) as isize, 27); assert_eq!(monopoly.advance_by(27) as isize, 27);
assert_eq!(monopoly.go_back_3_squares() as isize, 24); assert_eq!(monopoly.go_back_3_squares() as isize, 24);
} }
#[test]
fn custom() {
let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.go_to_square(Squares::E3), Squares::E3);
}
#[test]
fn advance_by() {
let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.advance_by(NUMBER_SQUARES) as isize, 0);
assert_eq!(monopoly.advance_by(NUMBER_SQUARES) as isize, 0);
assert_eq!(monopoly.advance_by(1) as isize, 1);
assert_eq!(monopoly.advance_by(1) as isize, 2);
assert_eq!(monopoly.advance_by(1) as isize, 3);
}
#[ignore]
#[test]
fn rng() {
use rand::prelude::*;
let mut rng = rand::thread_rng();
let possible_values = Vec::from_iter(0..=5);
let mut result = vec![0; 6];
for _ in 0..100_000_000 {
result[*possible_values.choose(&mut rng).unwrap()] += 1;
}
println!("{result:?}");
}
} }
+1 -1
View File
@@ -1,7 +1,7 @@
mod lib; mod lib;
fn main() { fn main() {
let monopoly = lib::Monopoly::new(); let monopoly = lib::Monopoly::new(lib::Dice::SixSided);
let mut statistics = lib::Statistics::new(monopoly, 10_000_000); let mut statistics = lib::Statistics::new(monopoly, 10_000_000);
statistics.run(); statistics.run();
statistics.show(); statistics.show();