iterator shouldnt iterate over moves we know are invalid

This commit is contained in:
Daniel Olsen 2021-01-06 10:26:33 +01:00
parent 7eab0722cf
commit f4066492ce
2 changed files with 31 additions and 11 deletions

View File

@ -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<u8, &'static str> {
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<Game, &'static str> {
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<Game, &'static str> {
let n_factories = get_n_factories(players)?;
let mut factories = SmallVec::<[Factory; 5]>::new();
for _ in 0..n_factories {
factories.push(Factory::default())

View File

@ -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);