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,
}
#[allow(dead_code)]
#[derive(Debug)]
enum Dice {
pub enum Dice {
FourSided = 4,
SixSided = 6,
}
@@ -126,14 +127,16 @@ impl Monopoly {
}
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
pub fn new(dice: Dice) -> Self {
let mut rng = rand::thread_rng();
let community_pile = Self::gen_random_community_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 {
rng,
board: Squares::iter().cycle(),
dice: Dice::SixSided,
board,
dice,
community_pile,
chance_pile,
number_consecutive_doubles: 0,
@@ -159,7 +162,7 @@ impl Monopoly {
return self.go_to_jail();
}
let square = self.board.nth(distance).unwrap();
let square = self.advance_by(distance);
match square {
Squares::G2J => self.go_to_jail(),
Squares::CC1 => self.handle_community_chest(Squares::CC1),
@@ -193,8 +196,7 @@ impl Monopoly {
}
fn go_to_square(&mut self, square: Squares) -> Squares {
let _ = (&mut self.board).skip_while(|c| *c != square);
square
self.board.find(|c| *c == square).unwrap()
}
fn go_to_squares(&mut self, squares: &[Squares]) -> Squares {
@@ -251,9 +253,8 @@ impl Monopoly {
}
}
#[cfg(test)]
fn advance_by_number(&mut self, number: usize) -> Squares {
self.board.nth(number).unwrap()
fn advance_by(&mut self, number: usize) -> Squares {
self.board.nth(number - 1).unwrap()
}
}
@@ -268,7 +269,7 @@ impl Statistics {
Self {
monopoly,
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)]
pub mod test {
use super::{Monopoly, Squares};
mod test {
use super::{Dice, Monopoly, Squares, NUMBER_SQUARES};
#[test]
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);
}
#[test]
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::U2);
assert_eq!(monopoly.go_to_utility(), Squares::U1);
@@ -313,7 +314,7 @@ pub mod test {
#[test]
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::R2);
assert_eq!(monopoly.go_to_railway(), Squares::R3);
@@ -323,18 +324,49 @@ pub mod test {
#[test]
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);
}
#[test]
fn back_3() {
let mut monopoly = Monopoly::new();
assert_eq!(monopoly.advance_by_number(3) as isize, 3);
let mut monopoly = Monopoly::new(Dice::SixSided);
assert_eq!(monopoly.advance_by(3) as isize, 3);
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);
}
#[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;
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);
statistics.run();
statistics.show();