This commit is contained in:
Daniel Olsen 2021-01-03 00:15:45 +01:00
parent 21ef5f7c5b
commit 30598ddd5a
4 changed files with 164 additions and 25 deletions

80
Cargo.lock generated
View File

@ -1,5 +1,83 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]] [[package]]
name = "Mercury" name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "getrandom"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
[[package]]
name = "mercury"
version = "0.1.0" version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8b34ba8cfb21243bd8df91854c830ff0d785fff2e82ebd4434c2644cb9ada18"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"

View File

@ -1,5 +1,5 @@
[package] [package]
name = "Mercury" name = "mercury"
version = "0.1.0" version = "0.1.0"
authors = ["Daniel Olsen <daniel.olsen99@gmail.com>"] authors = ["Daniel Olsen <daniel.olsen99@gmail.com>"]
edition = "2018" edition = "2018"
@ -7,3 +7,4 @@ 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"

View File

@ -1,7 +1,8 @@
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use rand::prelude::*;
#[derive(Debug)] #[derive(Debug, Clone, PartialEq)]
enum Tile { pub enum Tile {
Start, Start,
Blue, Blue,
Yellow, Yellow,
@ -10,7 +11,10 @@ enum Tile {
Teal Teal
} }
#[derive(Debug)] // factory, color, pattern line
pub struct GameMove (pub usize, pub Tile, pub usize);
#[derive(Debug, Clone)]
struct Bag (Vec<Tile>); struct Bag (Vec<Tile>);
impl Default for Bag { impl Default for Bag {
fn default() -> Self { fn default() -> Self {
@ -48,25 +52,31 @@ impl DerefMut for Bag {
} }
} }
impl From<Vec<Tile>> for Bag {
fn from(vector: Vec<Tile>) -> Bag {
Bag(vector)
}
}
#[derive(Default, Debug)]
struct Factory (Vec<Tile>); #[derive(Default, Debug, Clone)]
struct Factory ([Option<Tile>; 4]);
impl Deref for Factory { impl Deref for Factory {
type Target = Vec<Tile>; type Target = [Option<Tile>; 4];
fn deref(&self) -> &Vec<Tile> { fn deref(&self) -> &[Option<Tile>; 4] {
&self.0 &self.0
} }
} }
impl DerefMut for Factory { impl DerefMut for Factory {
fn deref_mut(&mut self) -> &mut Vec<Tile> { fn deref_mut(&mut self) -> &mut [Option<Tile>; 4] {
&mut self.0 &mut self.0
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
struct Market (Vec<Tile>); struct Market (Vec<Tile>);
impl Default for Market { impl Default for Market {
@ -89,19 +99,22 @@ impl DerefMut for Market {
} }
} }
type Patterns = (
#[derive(Debug, Default)] [Option<Tile>; 1],
struct PatternLine (Option<Tile>, u8); [Option<Tile>; 2],
type Patterns = [PatternLine; 5]; [Option<Tile>; 3],
[Option<Tile>; 4],
[Option<Tile>; 5]
);
type Row = [bool; 5]; type Row = [bool; 5];
type Wall = [Row; 5]; type Wall = [Row; 5];
#[derive(Debug)] #[derive(Debug, Clone)]
struct Board { struct Board {
score: u8, score: u8,
wall: Wall, wall: Wall,
floor: Vec<PatternLine>, floor: Vec<Tile>,
patterns: Patterns, patterns: Patterns,
} }
impl Default for Board { impl Default for Board {
@ -115,10 +128,10 @@ impl Default for Board {
} }
} }
#[derive(Default, Debug)] #[derive(Default, Debug, Clone)]
pub struct Game { pub struct Game {
turn: u8, turn: u8,
player: u8, player: usize,
box_top: Bag, box_top: Bag,
bag: Bag, bag: Bag,
market: Market, market: Market,
@ -143,24 +156,70 @@ impl Game {
boards.push(Board::default()); boards.push(Board::default());
} }
let game = Game { let mut game = Game {
turn: 0, turn: 0,
player: 0, player: 0,
box_top: Bag::default(), box_top: Vec::<Tile>::with_capacity(100).into(),
bag: Bag::default(), bag: Bag::default(),
market: Market::default(), market: Market::default(),
factories: factories, factories: factories,
boards: boards boards: boards
}; };
game.fill()?;
Ok(game) Ok(game)
} }
pub fn fill(&mut self) -> Result<(), &'static str> { fn fill(&mut self) -> Result<(), &'static str> {
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")
}; };
}; };
for factory in &mut self.factories {
for i in 0..4 {
if self.bag.len() == 0 && self.box_top.len() > 0 {
self.bag.append(&mut self.box_top);
}
else if self.bag.len() == 0 {
return Ok(())
}
else {
let tile_i = (random::<f32>() * self.bag.len() as f32).floor() as usize;
let tile = self.bag.remove(tile_i);
factory[i] = Some(tile);
}
}
};
Ok(()) Ok(())
} }
// pub fn shop(&mut self, ) pub fn do_move(&self, game_move: GameMove) -> Result<Game, &'static str> {
if game_move.1 == Tile::Start {
return Err("You can't take the start tile alone")
}
let mut game = self.clone();
let old_factory = &self.factories[game_move.0];
let new_factory = &game.factories[game_move.0];
let sel_tile = game_move.1;
let mut hand = old_factory.clone();
hand.to_vec();
hand.retain(|x| *x == sel_tile);
if hand.len() == 0 {
return Err("That tile is not in that factory")
}
let target = &mut game.boards[self.player].patterns[game_move.2];
if target.first().is_some() || *target.first().unwrap() != sel_tile {
return Err("You cannot place that tile on that pattern-line")
}
game.turn += 1;
game.player = (game.player + 1) % self.boards.len();
Ok(game)
}
} }

View File

@ -1,10 +1,11 @@
mod azul; mod azul;
use azul::Game; use azul::*;
fn main() -> Result<(), &'static str>{ fn main() -> Result<(), &'static str>{
let mut game = Game::new(2)?; let mut game = Game::new(2)?;
println!("{:#?}", game); println!("{:#?}", game);
game.fill()?; let game2 = game.do_move(GameMove(0, Tile::Red, 0))?;
println!("{:#?}", game2);
Ok(()) Ok(())
} }