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

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(())
}