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.
# It is not intended for manual editing.
[[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"
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]
name = "Mercury"
name = "mercury"
version = "0.1.0"
authors = ["Daniel Olsen <daniel.olsen99@gmail.com>"]
edition = "2018"
@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.0"

View File

@ -1,7 +1,8 @@
use std::ops::{Deref, DerefMut};
use rand::prelude::*;
#[derive(Debug)]
enum Tile {
#[derive(Debug, Clone, PartialEq)]
pub enum Tile {
Start,
Blue,
Yellow,
@ -10,7 +11,10 @@ enum Tile {
Teal
}
#[derive(Debug)]
// factory, color, pattern line
pub struct GameMove (pub usize, pub Tile, pub usize);
#[derive(Debug, Clone)]
struct Bag (Vec<Tile>);
impl Default for Bag {
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 {
type Target = Vec<Tile>;
type Target = [Option<Tile>; 4];
fn deref(&self) -> &Vec<Tile> {
fn deref(&self) -> &[Option<Tile>; 4] {
&self.0
}
}
impl DerefMut for Factory {
fn deref_mut(&mut self) -> &mut Vec<Tile> {
fn deref_mut(&mut self) -> &mut [Option<Tile>; 4] {
&mut self.0
}
}
#[derive(Debug)]
#[derive(Debug, Clone)]
struct Market (Vec<Tile>);
impl Default for Market {
@ -89,19 +99,22 @@ impl DerefMut for Market {
}
}
#[derive(Debug, Default)]
struct PatternLine (Option<Tile>, u8);
type Patterns = [PatternLine; 5];
type Patterns = (
[Option<Tile>; 1],
[Option<Tile>; 2],
[Option<Tile>; 3],
[Option<Tile>; 4],
[Option<Tile>; 5]
);
type Row = [bool; 5];
type Wall = [Row; 5];
#[derive(Debug)]
#[derive(Debug, Clone)]
struct Board {
score: u8,
wall: Wall,
floor: Vec<PatternLine>,
floor: Vec<Tile>,
patterns: Patterns,
}
impl Default for Board {
@ -115,10 +128,10 @@ impl Default for Board {
}
}
#[derive(Default, Debug)]
#[derive(Default, Debug, Clone)]
pub struct Game {
turn: u8,
player: u8,
player: usize,
box_top: Bag,
bag: Bag,
market: Market,
@ -143,24 +156,70 @@ impl Game {
boards.push(Board::default());
}
let game = Game {
let mut game = Game {
turn: 0,
player: 0,
box_top: Bag::default(),
box_top: Vec::<Tile>::with_capacity(100).into(),
bag: Bag::default(),
market: Market::default(),
factories: factories,
boards: boards
};
game.fill()?;
Ok(game)
}
pub fn fill(&mut self) -> Result<(), &'static str> {
fn fill(&mut self) -> Result<(), &'static str> {
for factory in &self.factories {
if factory.len() != 0 {
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(())
}
// 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;
use azul::Game;
use azul::*;
fn main() -> Result<(), &'static str>{
let mut game = Game::new(2)?;
println!("{:#?}", game);
game.fill()?;
let game2 = game.do_move(GameMove(0, Tile::Red, 0))?;
println!("{:#?}", game2);
Ok(())
}