From 30598ddd5ac6fc243d2d1e17e31af07d89f97056 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sun, 3 Jan 2021 00:15:45 +0100 Subject: [PATCH] test --- Cargo.lock | 80 ++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +- src/azul.rs | 101 +++++++++++++++++++++++++++++++++++++++++----------- src/main.rs | 5 +-- 4 files changed, 164 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 67a6654..accd7ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index bae83bd..d1d14c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "Mercury" +name = "mercury" version = "0.1.0" authors = ["Daniel Olsen "] 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" \ No newline at end of file diff --git a/src/azul.rs b/src/azul.rs index 58d0435..837df68 100644 --- a/src/azul.rs +++ b/src/azul.rs @@ -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); impl Default for Bag { fn default() -> Self { @@ -48,25 +52,31 @@ impl DerefMut for Bag { } } +impl From> for Bag { + fn from(vector: Vec) -> Bag { + Bag(vector) + } +} -#[derive(Default, Debug)] -struct Factory (Vec); + +#[derive(Default, Debug, Clone)] +struct Factory ([Option; 4]); impl Deref for Factory { - type Target = Vec; + type Target = [Option; 4]; - fn deref(&self) -> &Vec { + fn deref(&self) -> &[Option; 4] { &self.0 } } impl DerefMut for Factory { - fn deref_mut(&mut self) -> &mut Vec { + fn deref_mut(&mut self) -> &mut [Option; 4] { &mut self.0 } } -#[derive(Debug)] +#[derive(Debug, Clone)] struct Market (Vec); impl Default for Market { @@ -89,19 +99,22 @@ impl DerefMut for Market { } } - -#[derive(Debug, Default)] -struct PatternLine (Option, u8); -type Patterns = [PatternLine; 5]; +type Patterns = ( + [Option; 1], + [Option; 2], + [Option; 3], + [Option; 4], + [Option; 5] +); type Row = [bool; 5]; type Wall = [Row; 5]; -#[derive(Debug)] +#[derive(Debug, Clone)] struct Board { score: u8, wall: Wall, - floor: Vec, + floor: Vec, 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::::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::() * 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 { + + 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) + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 287d771..ad2e642 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) }