asdadaa
This commit is contained in:
parent
7f98ea6afe
commit
22af164a95
|
@ -6,6 +6,16 @@ version = "0.1.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "coz"
|
||||||
|
version = "0.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cef55b3fe2f5477d59e12bc792e8b3c95a25bd099eadcfae006ecea136de76e2"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"once_cell",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
@ -27,9 +37,16 @@ checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
|
||||||
name = "mercury"
|
name = "mercury"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"coz",
|
||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ppv-lite86"
|
name = "ppv-lite86"
|
||||||
version = "0.2.10"
|
version = "0.2.10"
|
||||||
|
|
|
@ -7,4 +7,8 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = "0.8.0"
|
rand = "0.8.0"
|
||||||
|
coz = "0.1"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = 1
|
|
@ -0,0 +1,4 @@
|
||||||
|
startup time=1609744772805409109
|
||||||
|
runtime time=10083706
|
||||||
|
startup time=1609744984945886640
|
||||||
|
runtime time=10097224
|
|
@ -30,5 +30,6 @@ with nixpkgs;
|
||||||
matklad.rust-analyzer
|
matklad.rust-analyzer
|
||||||
];
|
];
|
||||||
})
|
})
|
||||||
|
coz
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
23
src/azul.rs
23
src/azul.rs
|
@ -14,6 +14,11 @@ pub enum Tile {
|
||||||
// factory, color, pattern line
|
// factory, color, pattern line
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct GameMove (pub usize, pub Tile, pub usize);
|
pub struct GameMove (pub usize, pub Tile, pub usize);
|
||||||
|
impl Default for GameMove {
|
||||||
|
fn default() -> Self {
|
||||||
|
GameMove(0, Tile::Blue, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Bag (Vec<Tile>);
|
struct Bag (Vec<Tile>);
|
||||||
|
@ -170,6 +175,7 @@ impl Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn connected(&self, coordinate: (usize, usize)) -> u8 {
|
fn connected(&self, coordinate: (usize, usize)) -> u8 {
|
||||||
|
coz::scope!("connected tiles");
|
||||||
let wall = self.wall;
|
let wall = self.wall;
|
||||||
|
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
|
@ -222,7 +228,7 @@ impl Default for Board {
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
turn: u8,
|
turn: u32,
|
||||||
player: usize,
|
player: usize,
|
||||||
box_top: Bag,
|
box_top: Bag,
|
||||||
bag: Bag,
|
bag: Bag,
|
||||||
|
@ -232,6 +238,7 @@ pub struct Game {
|
||||||
}
|
}
|
||||||
impl Game {
|
impl Game {
|
||||||
pub fn new(players: usize) -> Result<Self, &'static str> {
|
pub fn new(players: usize) -> Result<Self, &'static str> {
|
||||||
|
coz::scope!("create game");
|
||||||
let n_factories = match players {
|
let n_factories = match players {
|
||||||
2 => 5,
|
2 => 5,
|
||||||
3 => 7,
|
3 => 7,
|
||||||
|
@ -261,6 +268,7 @@ impl Game {
|
||||||
Ok(game)
|
Ok(game)
|
||||||
}
|
}
|
||||||
pub fn fill(&mut self) -> Result<(), &'static str> {
|
pub fn fill(&mut self) -> Result<(), &'static str> {
|
||||||
|
coz::scope!("fill factories from bag");
|
||||||
for factory in &self.factories {
|
for factory in &self.factories {
|
||||||
if factory.len() != 0 {
|
if factory.len() != 0 {
|
||||||
return Err("Cannot fill, factories are not empty")
|
return Err("Cannot fill, factories are not empty")
|
||||||
|
@ -284,6 +292,7 @@ impl Game {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn score(&mut self) -> Result<(), &'static str> {
|
fn score(&mut self) -> Result<(), &'static str> {
|
||||||
|
coz::scope!("score boards");
|
||||||
for board in &mut self.boards {
|
for board in &mut self.boards {
|
||||||
for row in 0..4 {
|
for row in 0..4 {
|
||||||
if board.patterns[row].len() == (row + 1) {
|
if board.patterns[row].len() == (row + 1) {
|
||||||
|
@ -310,7 +319,7 @@ impl Game {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn do_move(&self, game_move: GameMove) -> Result<Game, &'static str> {
|
pub fn do_move(&self, game_move: GameMove) -> Result<Game, &'static str> {
|
||||||
|
coz::scope!("do a move");
|
||||||
if game_move.1 == Tile::Start {
|
if game_move.1 == Tile::Start {
|
||||||
return Err("You can't take the start tile alone")
|
return Err("You can't take the start tile alone")
|
||||||
}
|
}
|
||||||
|
@ -325,7 +334,7 @@ impl Game {
|
||||||
if self.market.contains(&game_move.1) {
|
if self.market.contains(&game_move.1) {
|
||||||
let mut hand = self.market.deref().clone();
|
let mut hand = self.market.deref().clone();
|
||||||
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
||||||
game.market.retain(|&x| x != Tile::Start || x != game_move.1);
|
game.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
||||||
|
|
||||||
board.floor.append(&mut hand)
|
board.floor.append(&mut hand)
|
||||||
}
|
}
|
||||||
|
@ -333,7 +342,7 @@ impl Game {
|
||||||
return Err("Market does not contain selected tile")
|
return Err("Market does not contain selected tile")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
GameMove(0, _, 1..=9) => {
|
GameMove(0, _, 1..=6) => {
|
||||||
if self.market.len() == 0 {
|
if self.market.len() == 0 {
|
||||||
return Err("Market is empty");
|
return Err("Market is empty");
|
||||||
}
|
}
|
||||||
|
@ -347,7 +356,7 @@ impl Game {
|
||||||
|
|
||||||
let mut hand = self.market.deref().clone();
|
let mut hand = self.market.deref().clone();
|
||||||
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
||||||
game.market.retain(|&x| x != Tile::Start || x != game_move.1);
|
game.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
||||||
|
|
||||||
for tile in hand.drain(..) {
|
for tile in hand.drain(..) {
|
||||||
let empty = game_move.2 - &target.len();
|
let empty = game_move.2 - &target.len();
|
||||||
|
@ -369,6 +378,10 @@ impl Game {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
GameMove(1..=9, _, _) => {
|
GameMove(1..=9, _, _) => {
|
||||||
|
if game_move.0 > self.factories.len() - 1 {
|
||||||
|
return Err("That factory is out of bounds");
|
||||||
|
}
|
||||||
|
|
||||||
let old_factory = &self.factories[game_move.0 - 1];
|
let old_factory = &self.factories[game_move.0 - 1];
|
||||||
if old_factory.contains(&game_move.1) {
|
if old_factory.contains(&game_move.1) {
|
||||||
|
|
||||||
|
|
65
src/main.rs
65
src/main.rs
|
@ -5,9 +5,70 @@ fn main() -> Result<(), &'static str>{
|
||||||
|
|
||||||
let mut game = Game::new(2)?;
|
let mut game = Game::new(2)?;
|
||||||
game.fill()?;
|
game.fill()?;
|
||||||
println!("{:#?}", game);
|
|
||||||
|
let mut all_err = false;
|
||||||
|
while !all_err {
|
||||||
|
println!("{:#?}", game);
|
||||||
|
all_err = true;
|
||||||
|
let mut game_move:Option<GameMove> = Some(GameMove::default());
|
||||||
|
while let Some(mut i) = game_move {
|
||||||
|
match game.do_move(i) {
|
||||||
|
Err(e) => {
|
||||||
|
println!("{:?}: {}", i, e);
|
||||||
|
game_move = i.next();
|
||||||
|
},
|
||||||
|
Ok(g) => {
|
||||||
|
println!("{:?}", i);
|
||||||
|
game = g;
|
||||||
|
all_err = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let game2 = game.do_move(GameMove(1, Tile::Red, 1))?;
|
let game2 = game.do_move(GameMove(1, Tile::Red, 1))?;
|
||||||
let game2 = game2.do_move(GameMove(0, Tile::Blue, 1))?;
|
|
||||||
println!("{:#?}", game2);
|
println!("{:#?}", game2);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Iterator for Tile {
|
||||||
|
type Item = Tile;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Tile>{
|
||||||
|
match *self {
|
||||||
|
Tile::Start => None,
|
||||||
|
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 {
|
||||||
|
type Item = GameMove;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<GameMove> {
|
||||||
|
let mut factory = self.0 + 1;
|
||||||
|
let mut _tile = Some(self.1);
|
||||||
|
let mut pattern = self.2;
|
||||||
|
if factory > 9 {
|
||||||
|
factory = 0;
|
||||||
|
_tile = _tile.unwrap().next();
|
||||||
|
};
|
||||||
|
let tile = match _tile {
|
||||||
|
None => {
|
||||||
|
pattern += 1;
|
||||||
|
Tile::Blue
|
||||||
|
},
|
||||||
|
Some(x) => x
|
||||||
|
};
|
||||||
|
if pattern > 6 {
|
||||||
|
return None
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(GameMove(factory, tile, pattern))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue