From f4066492ce9f60f181319d433f8374ce401b4a09 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 6 Jan 2021 10:26:33 +0100 Subject: [PATCH] iterator shouldnt iterate over moves we know are invalid --- src/azul.rs | 40 ++++++++++++++++++++++++++++++---------- src/main.rs | 2 +- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/azul.rs b/src/azul.rs index d48ec0f..2df3e62 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -75,14 +75,34 @@ impl IntoIterator for GameMove { fn into_iter(self) -> Self::IntoIter { GameMoveIter { + players: 4, current: self } } } +#[derive(Debug)] pub struct GameMoveIter { + players: u8, current: GameMove } +impl GameMoveIter { + pub fn new(players: u8) -> Self { + GameMoveIter { + players: players, + current: GameMove::default() + } + } +} + +fn get_n_factories(players: u8) -> Result { + return match players { + 2 => Ok(5), + 3 => Ok(7), + 4 => Ok(9), + _ => Err("Not a valid amount of players") + }; +} impl Iterator for GameMoveIter { type Item = GameMove; @@ -92,15 +112,20 @@ impl Iterator for GameMoveIter { let tile = self.current.1; let pattern = self.current.2; - if factory == 9 && tile == Tile::Teal && pattern == 0 { + let max_factories = match get_n_factories(self.players) { + Ok(n) => n, + Err(_) => return None + } as usize; + + if factory == max_factories && tile == Tile::Teal && pattern == 0 { return None } - else if factory == 9 && tile == Tile::Teal { + else if factory == max_factories && tile == Tile::Teal { let next = GameMove(0, Tile::Blue, (pattern + 1) % 6); self.current = next; return Some(next) } - else if factory == 9 { + else if factory == max_factories { let next = GameMove(0, tile.into_iter().next().unwrap(), pattern); self.current = next; return Some(next) @@ -324,13 +349,8 @@ pub struct Game { boards: SmallVec<[Board; 2]> // TODO set to 4? } impl Game { - pub fn new(players: usize, random: StdRng) -> Result { - let n_factories = match players { - 2 => 5, - 3 => 7, - 4 => 9, - _ => return Err("Not a valid amount of players") - }; + pub fn new(players: u8, random: StdRng) -> Result { + let n_factories = get_n_factories(players)?; let mut factories = SmallVec::<[Factory; 5]>::new(); for _ in 0..n_factories { factories.push(Factory::default()) diff --git a/src/main.rs b/src/main.rs index a816ee5..18b86b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,7 +74,7 @@ fn calculate_options() -> Result<(), &'static str> { fn count_options(game: Game, depth: u8, treshold: u8) -> u128 { coz::scope!("count option"); let mut sum = 0; - let i = GameMove::default(); + let i = GameMoveIter::new(2); let mut all_failed = true; for game_move in i { //println!("{:?}", game_move);