2021-01-02 02:25:39 +01:00
|
|
|
use std::ops::{Deref, DerefMut};
|
2021-01-03 00:15:45 +01:00
|
|
|
use rand::prelude::*;
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-03 04:44:48 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2021-01-03 00:15:45 +01:00
|
|
|
pub enum Tile {
|
2021-01-02 01:30:02 +01:00
|
|
|
Start,
|
2021-01-01 23:07:16 +01:00
|
|
|
Blue,
|
|
|
|
Yellow,
|
|
|
|
Red,
|
|
|
|
Black,
|
|
|
|
Teal
|
|
|
|
}
|
|
|
|
|
2021-01-03 00:15:45 +01:00
|
|
|
// factory, color, pattern line
|
|
|
|
pub struct GameMove (pub usize, pub Tile, pub usize);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2021-01-02 02:25:39 +01:00
|
|
|
struct Bag (Vec<Tile>);
|
2021-01-01 23:07:16 +01:00
|
|
|
impl Default for Bag {
|
|
|
|
fn default() -> Self {
|
2021-01-02 02:25:39 +01:00
|
|
|
let mut bag = Vec::<Tile>::with_capacity(100);
|
|
|
|
for _ in 0..20 {
|
|
|
|
bag.push(Tile::Blue);
|
|
|
|
};
|
|
|
|
for _ in 0..20 {
|
|
|
|
bag.push(Tile::Yellow);
|
|
|
|
};
|
|
|
|
for _ in 0..20 {
|
|
|
|
bag.push(Tile::Red);
|
|
|
|
};
|
|
|
|
for _ in 0..20 {
|
|
|
|
bag.push(Tile::Black);
|
|
|
|
};
|
|
|
|
for _ in 0..20 {
|
|
|
|
bag.push(Tile::Teal);
|
|
|
|
};
|
|
|
|
|
|
|
|
Bag(bag)
|
2021-01-01 23:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 02:25:39 +01:00
|
|
|
impl Deref for Bag {
|
|
|
|
type Target = Vec<Tile>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Vec<Tile> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for Bag {
|
|
|
|
fn deref_mut(&mut self) -> &mut Vec<Tile> {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 00:15:45 +01:00
|
|
|
impl From<Vec<Tile>> for Bag {
|
|
|
|
fn from(vector: Vec<Tile>) -> Bag {
|
|
|
|
Bag(vector)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-03 00:15:45 +01:00
|
|
|
#[derive(Default, Debug, Clone)]
|
2021-01-03 04:44:48 +01:00
|
|
|
struct Factory (Vec<Tile>);
|
2021-01-02 02:25:39 +01:00
|
|
|
|
|
|
|
impl Deref for Factory {
|
2021-01-03 04:44:48 +01:00
|
|
|
type Target = Vec<Tile>;
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-03 04:44:48 +01:00
|
|
|
fn deref(&self) -> &Vec<Tile> {
|
2021-01-02 02:25:39 +01:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for Factory {
|
2021-01-03 04:44:48 +01:00
|
|
|
fn deref_mut(&mut self) -> &mut Vec<Tile> {
|
2021-01-02 02:25:39 +01:00
|
|
|
&mut self.0
|
|
|
|
}
|
2021-01-01 23:07:16 +01:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-03 04:44:48 +01:00
|
|
|
|
2021-01-03 00:15:45 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2021-01-02 02:25:39 +01:00
|
|
|
struct Market (Vec<Tile>);
|
|
|
|
|
2021-01-01 23:07:16 +01:00
|
|
|
impl Default for Market {
|
|
|
|
fn default() -> Self {
|
2021-01-02 02:25:39 +01:00
|
|
|
let mut market = Vec::<Tile>::with_capacity(20);
|
|
|
|
market.push(Tile::Start);
|
|
|
|
Market(market)
|
2021-01-01 23:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-02 02:25:39 +01:00
|
|
|
impl Deref for Market {
|
|
|
|
type Target = Vec<Tile>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Vec<Tile> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for Market {
|
|
|
|
fn deref_mut(&mut self) -> &mut Vec<Tile> {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 04:44:48 +01:00
|
|
|
type Patterns = [Vec<Tile>; 5];
|
2021-01-01 23:07:16 +01:00
|
|
|
|
|
|
|
type Row = [bool; 5];
|
|
|
|
type Wall = [Row; 5];
|
|
|
|
|
2021-01-03 00:15:45 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2021-01-01 23:07:16 +01:00
|
|
|
struct Board {
|
|
|
|
score: u8,
|
|
|
|
wall: Wall,
|
2021-01-03 00:15:45 +01:00
|
|
|
floor: Vec<Tile>,
|
2021-01-01 23:07:16 +01:00
|
|
|
patterns: Patterns,
|
|
|
|
}
|
|
|
|
impl Default for Board {
|
|
|
|
fn default() -> Self {
|
|
|
|
Board {
|
|
|
|
score: 0,
|
|
|
|
wall: Wall::default(),
|
2021-01-02 01:30:02 +01:00
|
|
|
floor: Vec::default(),
|
2021-01-01 23:07:16 +01:00
|
|
|
patterns: Patterns::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 00:15:45 +01:00
|
|
|
#[derive(Default, Debug, Clone)]
|
2021-01-01 23:07:16 +01:00
|
|
|
pub struct Game {
|
2021-01-02 01:30:02 +01:00
|
|
|
turn: u8,
|
2021-01-03 00:15:45 +01:00
|
|
|
player: usize,
|
2021-01-02 02:25:39 +01:00
|
|
|
box_top: Bag,
|
2021-01-01 23:07:16 +01:00
|
|
|
bag: Bag,
|
|
|
|
market: Market,
|
|
|
|
factories: Vec<Factory>,
|
|
|
|
boards: Vec<Board>
|
|
|
|
}
|
|
|
|
impl Game {
|
|
|
|
pub fn new(players: usize) -> Result<Self, &'static str> {
|
|
|
|
let n_factories = match players {
|
|
|
|
2 => 5,
|
|
|
|
3 => 7,
|
|
|
|
4 => 9,
|
|
|
|
_ => return Err("Not a valid amount of players")
|
|
|
|
};
|
|
|
|
let mut factories = Vec::<Factory>::with_capacity(n_factories);
|
|
|
|
for _ in 0..n_factories {
|
|
|
|
factories.push(Factory::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut boards = Vec::<Board>::with_capacity(players);
|
|
|
|
for _ in 0..players {
|
|
|
|
boards.push(Board::default());
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:27:27 +01:00
|
|
|
let game = Game {
|
2021-01-02 01:30:02 +01:00
|
|
|
turn: 0,
|
|
|
|
player: 0,
|
2021-01-03 00:15:45 +01:00
|
|
|
box_top: Vec::<Tile>::with_capacity(100).into(),
|
2021-01-01 23:07:16 +01:00
|
|
|
bag: Bag::default(),
|
|
|
|
market: Market::default(),
|
|
|
|
factories: factories,
|
|
|
|
boards: boards
|
|
|
|
};
|
2021-01-03 00:15:45 +01:00
|
|
|
|
2021-01-01 23:07:16 +01:00
|
|
|
Ok(game)
|
|
|
|
}
|
2021-01-03 04:44:48 +01:00
|
|
|
pub fn fill(&mut self) -> Result<(), &'static str> {
|
2021-01-02 02:25:39 +01:00
|
|
|
for factory in &self.factories {
|
|
|
|
if factory.len() != 0 {
|
|
|
|
return Err("Cannot fill, factories are not empty")
|
|
|
|
};
|
|
|
|
};
|
2021-01-03 00:15:45 +01:00
|
|
|
for factory in &mut self.factories {
|
2021-01-03 14:27:27 +01:00
|
|
|
for _ in 0..4 {
|
2021-01-03 00:15:45 +01:00
|
|
|
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);
|
2021-01-03 04:44:48 +01:00
|
|
|
factory.push(tile);
|
2021-01-03 00:15:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-01-02 02:25:39 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-03 00:15:45 +01:00
|
|
|
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();
|
|
|
|
|
2021-01-03 04:44:48 +01:00
|
|
|
let board = &mut game.boards[self.player];
|
|
|
|
|
2021-01-03 14:27:27 +01:00
|
|
|
match game_move {
|
|
|
|
GameMove(_, Tile::Start, _) => return Err("You can't take the start tile specifically"),
|
|
|
|
GameMove(0, _, 0) => {
|
|
|
|
let mut hand = self.market.deref().clone();
|
|
|
|
hand.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)
|
|
|
|
},
|
|
|
|
GameMove(_,_,_) => return Err("Not a valid move")
|
|
|
|
}
|
|
|
|
|
|
|
|
let old_factory = match game_move.0 {
|
|
|
|
0 => self.market.deref(),
|
|
|
|
1..=9 => {
|
|
|
|
if game_move.0 > self.factories.len() {
|
|
|
|
return Err("No factory with that indice")
|
|
|
|
}
|
|
|
|
self.factories[game_move.0 - 1].deref()
|
|
|
|
},
|
|
|
|
_ => return Err("Not a valid place to take tiles from")
|
|
|
|
};
|
|
|
|
let new_factory = match game_move.0 {
|
|
|
|
0 => game.market.deref_mut(),
|
|
|
|
1..=9 => game.factories[game_move.0 - 1].deref_mut(),
|
|
|
|
_ => return Err("Not a valid place to take tiles from (new)")
|
|
|
|
};
|
2021-01-03 00:15:45 +01:00
|
|
|
|
|
|
|
let sel_tile = game_move.1;
|
2021-01-03 04:44:48 +01:00
|
|
|
let mut hand = old_factory.to_vec();
|
2021-01-03 00:15:45 +01:00
|
|
|
hand.retain(|x| *x == sel_tile);
|
|
|
|
if hand.len() == 0 {
|
|
|
|
return Err("That tile is not in that factory")
|
|
|
|
}
|
|
|
|
|
2021-01-03 04:44:48 +01:00
|
|
|
new_factory.retain(|x| *x != sel_tile);
|
|
|
|
game.market.append(new_factory);
|
|
|
|
|
|
|
|
if game_move.2 == 0 {
|
|
|
|
board.floor.append(&mut hand);
|
|
|
|
return Ok(game)
|
|
|
|
};
|
|
|
|
|
2021-01-03 05:16:04 +01:00
|
|
|
let target: &mut Vec<Tile> = match game_move.2 {
|
|
|
|
1..=5 => &mut board.patterns[game_move.2 - 1],
|
2021-01-03 04:44:48 +01:00
|
|
|
_ => return Err("That's not a valid pattern line")
|
|
|
|
};
|
|
|
|
|
2021-01-03 05:16:04 +01:00
|
|
|
println!("{:#?}", target);
|
|
|
|
|
|
|
|
if target.first() != None && target.first() != Some(&sel_tile) {
|
2021-01-03 04:44:48 +01:00
|
|
|
return Err("You cannot place that tile on that pattern-line, because there are already other tiles with a different color there")
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.len() == game_move.2 {
|
|
|
|
return Err("That line is full!")
|
|
|
|
}
|
|
|
|
|
|
|
|
let empty = game_move.2 - target.len();
|
|
|
|
if hand.len() <= empty {
|
|
|
|
target.append(&mut hand);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for tile in hand.drain(..) {
|
|
|
|
let empty = game_move.2 - &target.len();
|
|
|
|
if empty >= 1 {
|
|
|
|
target.push(tile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
board.floor.push(tile)
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
game.turn += 1;
|
|
|
|
game.player = (game.player + 1) % self.boards.len();
|
|
|
|
|
|
|
|
Ok(game)
|
|
|
|
}
|
2021-01-03 04:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tests
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bag() {
|
|
|
|
let game = Game::new(2).unwrap();
|
|
|
|
let bag = game.bag;
|
|
|
|
assert_eq!(bag.len(), 100);
|
|
|
|
|
|
|
|
let mut reds = bag.clone();
|
|
|
|
reds.retain(|x| *x == Tile::Red);
|
|
|
|
assert_eq!(reds.len(), 20);
|
2021-01-01 23:07:16 +01:00
|
|
|
}
|