I think this is working
This commit is contained in:
parent
bee99fc0f9
commit
875e232054
114
src/azul.rs
114
src/azul.rs
|
@ -11,14 +11,102 @@ pub enum Tile {
|
||||||
Teal
|
Teal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IntoIterator for Tile {
|
||||||
|
type Item = Tile;
|
||||||
|
type IntoIter = TileIter;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
TileIter {
|
||||||
|
current: self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TileIter {
|
||||||
|
current: Tile
|
||||||
|
}
|
||||||
|
impl Iterator for TileIter {
|
||||||
|
type Item = Tile;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Tile>{
|
||||||
|
match self.current {
|
||||||
|
Tile::Blue => {
|
||||||
|
let next = Tile::Yellow;
|
||||||
|
self.current = next;
|
||||||
|
Some(next)
|
||||||
|
},
|
||||||
|
Tile::Yellow => {
|
||||||
|
let next = Tile::Red;
|
||||||
|
self.current = next;
|
||||||
|
Some(next)
|
||||||
|
},
|
||||||
|
Tile::Red => {
|
||||||
|
let next = Tile::Black;
|
||||||
|
self.current = next;
|
||||||
|
Some(next)
|
||||||
|
},
|
||||||
|
Tile::Black => {
|
||||||
|
let next = Tile::Teal;
|
||||||
|
self.current = next;
|
||||||
|
Some(next)
|
||||||
|
},
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// factory, color, pattern line
|
// factory, color, pattern line
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct GameMove (pub usize, pub Tile, pub usize);
|
pub struct GameMove (pub usize, pub Tile, pub usize);
|
||||||
impl Default for GameMove {
|
impl Default for GameMove {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
GameMove(0, Tile::Blue, 1)
|
GameMove(0, Tile::Blue, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl IntoIterator for GameMove {
|
||||||
|
type Item = GameMove;
|
||||||
|
type IntoIter = GameMoveIter;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
GameMoveIter {
|
||||||
|
current: self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GameMoveIter {
|
||||||
|
current: GameMove
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for GameMoveIter {
|
||||||
|
type Item = GameMove;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<GameMove> {
|
||||||
|
let factory = self.current.0;
|
||||||
|
let tile = self.current.1;
|
||||||
|
let pattern = self.current.2;
|
||||||
|
|
||||||
|
if factory == 9 && tile == Tile::Teal && pattern == 0 {
|
||||||
|
return None
|
||||||
|
}
|
||||||
|
else if factory == 9 && tile == Tile::Teal {
|
||||||
|
let next = GameMove(0, Tile::Blue, (pattern + 1) % 6);
|
||||||
|
self.current = next;
|
||||||
|
return Some(next)
|
||||||
|
}
|
||||||
|
else if factory == 9 {
|
||||||
|
let next = GameMove(0, tile.into_iter().next().unwrap(), pattern);
|
||||||
|
self.current = next;
|
||||||
|
return Some(next)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let next = GameMove(factory + 1, tile, pattern);
|
||||||
|
self.current = next;
|
||||||
|
return Some(next)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Bag (Vec<Tile>);
|
struct Bag (Vec<Tile>);
|
||||||
|
@ -458,6 +546,21 @@ impl Game {
|
||||||
|
|
||||||
// Tests
|
// Tests
|
||||||
|
|
||||||
|
pub fn complicated() -> Result<Game, &'static str> {
|
||||||
|
let mut game = Game::new(2, StdRng::seed_from_u64(1))?;
|
||||||
|
|
||||||
|
let mut tiles = Tile::Blue;
|
||||||
|
|
||||||
|
for factory in &mut game.factories {
|
||||||
|
for _ in 0..4 {
|
||||||
|
factory.push(tiles);
|
||||||
|
tiles = tiles.into_iter().next().unwrap_or(Tile::Blue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(game)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bag() {
|
fn bag() {
|
||||||
let game = Game::new(2, StdRng::seed_from_u64(123)).unwrap();
|
let game = Game::new(2, StdRng::seed_from_u64(123)).unwrap();
|
||||||
|
@ -486,3 +589,12 @@ fn connected() -> Result<(), String> {
|
||||||
assert_eq!(score, 7);
|
assert_eq!(score, 7);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn game_move_iter() {
|
||||||
|
let i = GameMove::default();
|
||||||
|
println!("Original: {:?}", i);
|
||||||
|
assert_eq!(i.into_iter().next().unwrap(), GameMove(1, Tile::Blue, 1));
|
||||||
|
|
||||||
|
assert_eq!(i.into_iter().count(), 5)
|
||||||
|
}
|
74
src/main.rs
74
src/main.rs
|
@ -4,7 +4,7 @@ use rand::prelude::*;
|
||||||
|
|
||||||
fn main() -> Result<(), &'static str> {
|
fn main() -> Result<(), &'static str> {
|
||||||
let mut g_rng = StdRng::seed_from_u64(42);
|
let mut g_rng = StdRng::seed_from_u64(42);
|
||||||
for _ in 0..500000 {
|
for _ in 0..10000 {
|
||||||
let rng = match StdRng::from_rng(&mut g_rng) {
|
let rng = match StdRng::from_rng(&mut g_rng) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -27,11 +27,11 @@ fn run(rng: StdRng) -> Result<(), &'static str> {
|
||||||
//println!("{:#?}", game);
|
//println!("{:#?}", game);
|
||||||
all_err = true;
|
all_err = true;
|
||||||
let mut game_move:Option<GameMove> = Some(GameMove::default());
|
let mut game_move:Option<GameMove> = Some(GameMove::default());
|
||||||
while let Some(mut i) = game_move {
|
while let Some(i) = game_move {
|
||||||
match game.do_move(i) {
|
match game.do_move(i) {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
//println!("{:?}: {}", i, e);
|
//println!("{:?}: {}", i, e);
|
||||||
game_move = i.next();
|
game_move = i.into_iter().next();
|
||||||
},
|
},
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
all_err = false;
|
all_err = false;
|
||||||
|
@ -43,44 +43,46 @@ fn run(rng: StdRng) -> Result<(), &'static str> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for Tile {
|
#[test]
|
||||||
type Item = Tile;
|
fn calculate_options() -> Result<(), String> {
|
||||||
|
let game = complicated()?;
|
||||||
|
println!("{:#?}", game);
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Tile>{
|
println!("{}", count_options(game, 0));
|
||||||
match *self {
|
|
||||||
Tile::Start => None,
|
Ok(())
|
||||||
Tile::Blue => Some(Tile::Yellow),
|
|
||||||
Tile::Yellow => Some(Tile::Red),
|
|
||||||
Tile::Red => Some(Tile::Black),
|
|
||||||
Tile::Black => Some(Tile::Teal),
|
|
||||||
Tile::Teal => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for GameMove {
|
fn count_options(game: Game, depth: u8) -> u128 {
|
||||||
type Item = GameMove;
|
let mut sum = 0;
|
||||||
|
let i = GameMove::default();
|
||||||
fn next(&mut self) -> Option<GameMove> {
|
let mut all_failed = true;
|
||||||
let mut factory = self.0 + 1;
|
for game_move in i {
|
||||||
let mut _tile = Some(self.1);
|
//println!("{:?}", game_move);
|
||||||
let mut pattern = self.2;
|
let mut new_game = game.clone();
|
||||||
if factory > 9 {
|
let r = new_game.do_move(game_move);
|
||||||
factory = 0;
|
match r {
|
||||||
_tile = _tile.unwrap().next();
|
Ok(_) => sum += {/*println!("{}", depth);*/ all_failed = false; count_options(new_game, depth + 1)},
|
||||||
|
Err(_) => continue
|
||||||
};
|
};
|
||||||
let tile = match _tile {
|
|
||||||
None => {
|
|
||||||
match pattern {
|
|
||||||
6 => pattern = 0,
|
|
||||||
0 => return None,
|
|
||||||
_ => pattern = pattern + 1
|
|
||||||
};
|
|
||||||
Tile::Blue
|
|
||||||
},
|
|
||||||
Some(x) => x
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(GameMove(factory, tile, pattern))
|
if all_failed {
|
||||||
|
//println!("{}: ALL FAILED!", depth);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
|
||||||
|
/*game_move.iter_mut().map(|x| {
|
||||||
|
let mut new_game = game.clone();
|
||||||
|
let r = new_game.do_move(*x);
|
||||||
|
println!("{:#?}", r);
|
||||||
|
match r {
|
||||||
|
Ok(_) => count_options(new_game),
|
||||||
|
Err(_) => 0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.sum::<u128>()*/
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue