From 706e33a12f13358efe70da68e4c6cd3f198309c2 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Mon, 4 Jan 2021 11:24:49 +0100 Subject: [PATCH] bug fixes --- profile.coz | 4 ---- src/azul.rs | 8 +++++++- src/main.rs | 27 ++++++++++++++++----------- 3 files changed, 23 insertions(+), 16 deletions(-) delete mode 100644 profile.coz diff --git a/profile.coz b/profile.coz deleted file mode 100644 index 9fdc54d..0000000 --- a/profile.coz +++ /dev/null @@ -1,4 +0,0 @@ -startup time=1609744772805409109 -runtime time=10083706 -startup time=1609744984945886640 -runtime time=10097224 diff --git a/src/azul.rs b/src/azul.rs index a9486dc..ae29020 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -16,7 +16,7 @@ pub enum Tile { pub struct GameMove (pub usize, pub Tile, pub usize); impl Default for GameMove { fn default() -> Self { - GameMove(0, Tile::Blue, 0) + GameMove(0, Tile::Blue, 1) } } @@ -348,6 +348,9 @@ impl Game { } else if self.market.contains(&game_move.1) { let target = &mut board.patterns[game_move.2 - 1]; + if target.first().is_some() && target[0] != game_move.1 { + return Err("That pattern line already contains a different color") + } let empty = game_move.2 - target.len(); if empty == 0 { @@ -400,6 +403,9 @@ impl Game { }, 1..=9 => { let target = &mut board.patterns[game_move.2 - 1]; + if target.first().is_some() && target[0] != game_move.1 { + return Err("That pattern line already contains a different color") + } let empty = game_move.2 - target.len(); if hand.len() <= empty { target.append(&mut hand); diff --git a/src/main.rs b/src/main.rs index a4aad9b..f38e559 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,31 @@ mod azul; use azul::*; -fn main() -> Result<(), &'static str>{ +fn main() -> Result<(), &'static str> { + for _ in 0..10000 { + run()?; + } + Ok(()) +} + +fn run() -> Result<(), &'static str> { let mut game = Game::new(2)?; game.fill()?; let mut all_err = false; while !all_err { - println!("{:#?}", game); + //println!("{:#?}", game); all_err = true; let mut game_move:Option = Some(GameMove::default()); while let Some(mut i) = game_move { match game.do_move(i) { Err(e) => { - println!("{:?}: {}", i, e); + //println!("{:?}: {}", i, e); game_move = i.next(); }, Ok(g) => { - println!("{:?}", i); + //println!("{:?}", i); game = g; all_err = false; break; @@ -26,9 +33,6 @@ fn main() -> Result<(), &'static str>{ } } } - - let game2 = game.do_move(GameMove(1, Tile::Red, 1))?; - println!("{:#?}", game2); Ok(()) } @@ -60,14 +64,15 @@ impl Iterator for GameMove { }; let tile = match _tile { None => { - pattern += 1; + match pattern { + 6 => pattern = 0, + 0 => return None, + _ => pattern = pattern + 1 + }; Tile::Blue }, Some(x) => x }; - if pattern > 6 { - return None - } Some(GameMove(factory, tile, pattern)) }