2021-05-16 00:23:45 +02:00
|
|
|
use std::{hash::Hash, ops::{Deref, DerefMut}};
|
2021-01-03 00:15:45 +01:00
|
|
|
use rand::prelude::*;
|
2021-01-07 23:44:44 +01:00
|
|
|
use rand::distributions::WeightedIndex;
|
2021-01-07 12:52:30 +01:00
|
|
|
//use smallvec::{SmallVec, smallvec};
|
|
|
|
|
|
|
|
//use alloc_counter::{AllocCounterSystem, no_alloc};
|
|
|
|
//#[global_allocator]
|
|
|
|
//static A: AllocCounterSystem = AllocCounterSystem;
|
|
|
|
|
|
|
|
pub fn size_of_stuff() {
|
|
|
|
println!("size of azul game: {}", std::mem::size_of::<Game>());
|
|
|
|
println!("size of azul tile: {}", std::mem::size_of::<Tile>());
|
|
|
|
println!("size of azul bag: {}", std::mem::size_of::<Bag>());
|
|
|
|
println!("size of azul market: {}", std::mem::size_of::<Market>());
|
|
|
|
println!("size of azul factories: {}", std::mem::size_of::<tinyvec::ArrayVec<[Factory; 9]>>());
|
|
|
|
println!("size of azul factory array: {}", std::mem::size_of::<[Factory; 9]>());
|
|
|
|
println!("size of azul factory: {}", std::mem::size_of::<Factory>());
|
|
|
|
println!("size of azul boards: {}", std::mem::size_of::<tinyvec::ArrayVec<[Board; 4]>>());
|
|
|
|
println!("size of azul board array: {}", std::mem::size_of::<[Board; 4]>());
|
|
|
|
println!("size of azul board: {}", std::mem::size_of::<Board>());
|
|
|
|
|
|
|
|
println!("size of azul option tile: {}", std::mem::size_of::<Option<Tile>>());
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-07 12:52:30 +01:00
|
|
|
}
|
2021-01-05 23:04:38 +01:00
|
|
|
|
2021-05-16 00:23:45 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2021-01-03 00:15:45 +01:00
|
|
|
pub enum Tile {
|
2021-05-16 00:23:45 +02:00
|
|
|
// None,
|
2021-01-02 01:30:02 +01:00
|
|
|
Start,
|
2021-01-01 23:07:16 +01:00
|
|
|
Blue,
|
|
|
|
Yellow,
|
|
|
|
Red,
|
|
|
|
Black,
|
|
|
|
Teal
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:37:51 +01:00
|
|
|
impl Default for Tile {
|
|
|
|
fn default() -> Self {
|
2021-05-16 00:23:45 +02:00
|
|
|
// Tile::None
|
2021-01-06 14:37:51 +01:00
|
|
|
Tile::Blue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:38:56 +01:00
|
|
|
impl IntoIterator for Tile {
|
|
|
|
type Item = Tile;
|
|
|
|
type IntoIter = TileIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
TileIter {
|
|
|
|
current: self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 00:23:45 +02:00
|
|
|
/*impl From<Option<Tile>> for Tile {
|
|
|
|
fn from(option: Option<Tile>) -> Tile {
|
|
|
|
match option {
|
|
|
|
Option::None => Tile::None,
|
|
|
|
Some(tile) => tile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2021-01-05 07:38:56 +01:00
|
|
|
pub struct TileIter {
|
|
|
|
current: Tile
|
|
|
|
}
|
|
|
|
impl Iterator for TileIter {
|
|
|
|
type Item = Tile;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Tile>{
|
|
|
|
match self.current {
|
|
|
|
Tile::Blue => {
|
|
|
|
let next = Tile::Yellow;
|
|
|
|
self.current = next;
|
|
|
|
Some(next)
|
|
|
|
},
|
|
|
|
Tile::Yellow => {
|
|
|
|
let next = Tile::Red;
|
|
|
|
self.current = next;
|
|
|
|
Some(next)
|
|
|
|
},
|
|
|
|
Tile::Red => {
|
|
|
|
let next = Tile::Black;
|
|
|
|
self.current = next;
|
|
|
|
Some(next)
|
|
|
|
},
|
|
|
|
Tile::Black => {
|
|
|
|
let next = Tile::Teal;
|
|
|
|
self.current = next;
|
|
|
|
Some(next)
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-03 00:15:45 +01:00
|
|
|
// factory, color, pattern line
|
2021-01-05 07:38:56 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2021-01-03 00:15:45 +01:00
|
|
|
pub struct GameMove (pub usize, pub Tile, pub usize);
|
2021-01-04 10:01:06 +01:00
|
|
|
impl Default for GameMove {
|
|
|
|
fn default() -> Self {
|
2021-01-04 11:24:49 +01:00
|
|
|
GameMove(0, Tile::Blue, 1)
|
2021-01-04 10:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 07:38:56 +01:00
|
|
|
impl IntoIterator for GameMove {
|
|
|
|
type Item = GameMove;
|
|
|
|
type IntoIter = GameMoveIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
GameMoveIter {
|
2021-01-06 10:26:33 +01:00
|
|
|
players: 4,
|
2021-01-05 07:38:56 +01:00
|
|
|
current: self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 10:26:33 +01:00
|
|
|
#[derive(Debug)]
|
2021-01-05 07:38:56 +01:00
|
|
|
pub struct GameMoveIter {
|
2021-01-06 10:26:33 +01:00
|
|
|
players: u8,
|
2021-01-05 07:38:56 +01:00
|
|
|
current: GameMove
|
|
|
|
}
|
2021-01-06 10:26:33 +01:00
|
|
|
impl GameMoveIter {
|
|
|
|
pub fn new(players: u8) -> Self {
|
|
|
|
GameMoveIter {
|
|
|
|
players: players,
|
|
|
|
current: GameMove::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_n_factories(players: u8) -> Result<u8, &'static str> {
|
|
|
|
return match players {
|
|
|
|
2 => Ok(5),
|
|
|
|
3 => Ok(7),
|
|
|
|
4 => Ok(9),
|
|
|
|
_ => Err("Not a valid amount of players")
|
|
|
|
};
|
|
|
|
}
|
2021-01-05 07:38:56 +01:00
|
|
|
|
|
|
|
impl Iterator for GameMoveIter {
|
|
|
|
type Item = GameMove;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<GameMove> {
|
|
|
|
let factory = self.current.0;
|
|
|
|
let tile = self.current.1;
|
|
|
|
let pattern = self.current.2;
|
|
|
|
|
2021-01-06 10:26:33 +01:00
|
|
|
let max_factories = match get_n_factories(self.players) {
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(_) => return None
|
|
|
|
} as usize;
|
|
|
|
|
|
|
|
if factory == max_factories && tile == Tile::Teal && pattern == 0 {
|
2021-01-05 07:38:56 +01:00
|
|
|
return None
|
|
|
|
}
|
2021-01-06 10:26:33 +01:00
|
|
|
else if factory == max_factories && tile == Tile::Teal {
|
2021-01-05 07:38:56 +01:00
|
|
|
let next = GameMove(0, Tile::Blue, (pattern + 1) % 6);
|
|
|
|
self.current = next;
|
|
|
|
return Some(next)
|
|
|
|
}
|
2021-01-06 10:26:33 +01:00
|
|
|
else if factory == max_factories {
|
2021-01-05 07:38:56 +01:00
|
|
|
let next = GameMove(0, tile.into_iter().next().unwrap(), pattern);
|
|
|
|
self.current = next;
|
|
|
|
return Some(next)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let next = GameMove(factory + 1, tile, pattern);
|
|
|
|
self.current = next;
|
|
|
|
return Some(next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 00:15:45 +01:00
|
|
|
|
2021-05-14 00:14:39 +02:00
|
|
|
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
|
2021-05-16 03:40:11 +02:00
|
|
|
struct Bag {
|
|
|
|
blue: u8,
|
|
|
|
yellow: u8,
|
|
|
|
red: u8,
|
|
|
|
black: u8,
|
|
|
|
teal: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Bag {
|
|
|
|
fn default() -> Self {
|
|
|
|
Bag {
|
|
|
|
blue: 20,
|
|
|
|
yellow: 20,
|
|
|
|
red: 20,
|
|
|
|
black: 20,
|
|
|
|
teal: 20,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bag {
|
|
|
|
fn len(&self) -> u8 {
|
|
|
|
self.blue + self.yellow + self.red + self.black + self.teal
|
|
|
|
}
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
|
2021-01-06 14:37:51 +01:00
|
|
|
struct Bag (tinyvec::ArrayVec::<[Tile; 128]>);
|
|
|
|
|
2021-01-01 23:07:16 +01:00
|
|
|
impl Default for Bag {
|
|
|
|
fn default() -> Self {
|
2021-01-06 14:37:51 +01:00
|
|
|
let mut bag = tinyvec::ArrayVec::<[Tile; 128]>::new();
|
2021-01-02 02:25:39 +01:00
|
|
|
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 {
|
2021-01-06 14:37:51 +01:00
|
|
|
type Target = tinyvec::ArrayVec<[Tile; 128]>;
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-06 14:37:51 +01:00
|
|
|
fn deref(&self) -> &tinyvec::ArrayVec<[Tile; 128]> {
|
2021-01-02 02:25:39 +01:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for Bag {
|
2021-01-06 14:37:51 +01:00
|
|
|
fn deref_mut(&mut self) -> &mut tinyvec::ArrayVec<[Tile; 128]> {
|
2021-01-02 02:25:39 +01:00
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:37:51 +01:00
|
|
|
impl From<tinyvec::ArrayVec<[Tile; 128]>> for Bag {
|
|
|
|
fn from(vector: tinyvec::ArrayVec<[Tile; 128]>) -> Bag {
|
2021-01-03 00:15:45 +01:00
|
|
|
Bag(vector)
|
|
|
|
}
|
2021-05-16 03:40:11 +02:00
|
|
|
}*/
|
2021-01-03 00:15:45 +01:00
|
|
|
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-05-16 00:23:45 +02:00
|
|
|
#[derive(Default, Debug, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2021-01-06 14:37:51 +01:00
|
|
|
struct Factory (tinyvec::ArrayVec<[Tile; 4]>);
|
2021-01-05 23:04:38 +01:00
|
|
|
impl Clone for Factory {
|
2021-01-07 12:52:30 +01:00
|
|
|
//#[no_alloc]
|
2021-01-05 23:04:38 +01:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Factory(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 02:25:39 +01:00
|
|
|
impl Deref for Factory {
|
2021-01-06 14:37:51 +01:00
|
|
|
type Target = tinyvec::ArrayVec<[Tile; 4]>;
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-06 14:37:51 +01:00
|
|
|
fn deref(&self) -> &tinyvec::ArrayVec<[Tile; 4]> {
|
2021-01-02 02:25:39 +01:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for Factory {
|
2021-01-06 14:37:51 +01:00
|
|
|
fn deref_mut(&mut self) -> &mut tinyvec::ArrayVec<[Tile; 4]> {
|
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-05-14 00:14:39 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2021-01-06 15:39:27 +01:00
|
|
|
struct Market (tinyvec::ArrayVec<[Tile; 28]>);
|
2021-01-01 23:07:16 +01:00
|
|
|
impl Default for Market {
|
|
|
|
fn default() -> Self {
|
2021-01-06 15:39:27 +01:00
|
|
|
// tiles * factories + start = 3 * 9 + 1 = 28
|
|
|
|
let mut market = tinyvec::ArrayVec::<[Tile; 28]>::new();
|
2021-01-02 02:25:39 +01:00
|
|
|
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 {
|
2021-01-06 15:39:27 +01:00
|
|
|
type Target = tinyvec::ArrayVec<[Tile; 28]>;
|
2021-01-02 02:25:39 +01:00
|
|
|
|
2021-01-06 15:39:27 +01:00
|
|
|
fn deref(&self) -> &tinyvec::ArrayVec<[Tile; 28]> {
|
2021-01-02 02:25:39 +01:00
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl DerefMut for Market {
|
2021-01-06 15:39:27 +01:00
|
|
|
fn deref_mut(&mut self) -> &mut tinyvec::ArrayVec<[Tile; 28]> {
|
2021-01-02 02:25:39 +01:00
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:37:51 +01:00
|
|
|
type Patterns = [tinyvec::ArrayVec<[Tile; 5]>; 5];
|
2021-01-01 23:07:16 +01:00
|
|
|
|
|
|
|
type Row = [bool; 5];
|
|
|
|
type Wall = [Row; 5];
|
|
|
|
|
2021-05-14 00:14:39 +02:00
|
|
|
#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Hash)]
|
2021-01-01 23:07:16 +01:00
|
|
|
struct Board {
|
|
|
|
score: u8,
|
|
|
|
wall: Wall,
|
2021-01-06 15:39:27 +01:00
|
|
|
floor: tinyvec::ArrayVec<[Tile; 20]>,
|
2021-01-01 23:07:16 +01:00
|
|
|
patterns: Patterns,
|
|
|
|
}
|
2021-01-03 22:09:40 +01:00
|
|
|
impl Board {
|
|
|
|
fn wall_index(color: Tile, row: usize) -> Result<usize, &'static str> {
|
|
|
|
match row {
|
|
|
|
0 => {
|
|
|
|
match color {
|
|
|
|
Tile::Blue => Ok(0),
|
|
|
|
Tile::Yellow => Ok(1),
|
|
|
|
Tile::Red => Ok(2),
|
|
|
|
Tile::Black => Ok(3),
|
|
|
|
Tile::Teal => Ok(4),
|
|
|
|
_ => return Err("Not a valid tile on the wall")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
1 => {
|
|
|
|
match color {
|
|
|
|
Tile::Blue => Ok(1),
|
|
|
|
Tile::Yellow => Ok(2),
|
|
|
|
Tile::Red => Ok(3),
|
|
|
|
Tile::Black => Ok(4),
|
|
|
|
Tile::Teal => Ok(0),
|
|
|
|
_ => return Err("Not a valid tile on the wall")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
match color {
|
|
|
|
Tile::Blue => Ok(2),
|
|
|
|
Tile::Yellow => Ok(3),
|
|
|
|
Tile::Red => Ok(4),
|
|
|
|
Tile::Black => Ok(0),
|
|
|
|
Tile::Teal => Ok(1),
|
|
|
|
_ => return Err("Not a valid tile on the wall")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
3 => {
|
|
|
|
match color {
|
|
|
|
Tile::Blue => Ok(3),
|
|
|
|
Tile::Yellow => Ok(4),
|
|
|
|
Tile::Red => Ok(0),
|
|
|
|
Tile::Black => Ok(1),
|
|
|
|
Tile::Teal => Ok(2),
|
|
|
|
_ => return Err("Not a valid tile on the wall")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
4 => {
|
|
|
|
match color {
|
|
|
|
Tile::Blue => Ok(4),
|
|
|
|
Tile::Yellow => Ok(0),
|
|
|
|
Tile::Red => Ok(1),
|
|
|
|
Tile::Black => Ok(2),
|
|
|
|
Tile::Teal => Ok(3),
|
|
|
|
_ => return Err("Not a valid tile on the wall")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => return Err("Not a valid row on the wall")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn connected(&self, coordinate: (usize, usize)) -> u8 {
|
|
|
|
let wall = self.wall;
|
|
|
|
|
|
|
|
let mut sum = 0;
|
|
|
|
let mut active = false;
|
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
for i in 0..5 {
|
|
|
|
if active == true && wall[coordinate.0][i] == false {
|
|
|
|
break;
|
|
|
|
} else if wall[coordinate.0][i] == false {
|
|
|
|
count = 0;
|
|
|
|
} else if (coordinate.0, i) == coordinate {
|
|
|
|
active = true;
|
|
|
|
count += 1;
|
|
|
|
} else {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum += count;
|
|
|
|
|
|
|
|
let mut active = false;
|
|
|
|
let mut count = 0;
|
|
|
|
for i in 0..5 {
|
|
|
|
if active == true && wall[i][coordinate.1] == false {
|
|
|
|
break;
|
|
|
|
} else if wall[i][coordinate.1] == false {
|
|
|
|
count = 0;
|
|
|
|
} else if (i, coordinate.1) == coordinate {
|
|
|
|
active = true;
|
|
|
|
count += 1;
|
|
|
|
} else {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum += count;
|
|
|
|
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 23:07:16 +01:00
|
|
|
|
2021-01-07 12:52:30 +01:00
|
|
|
//#[repr(align(16))]
|
2021-05-14 00:14:39 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
2021-01-01 23:07:16 +01:00
|
|
|
pub struct Game {
|
2021-01-04 10:01:06 +01:00
|
|
|
turn: u32,
|
2021-01-07 23:44:44 +01:00
|
|
|
player: u8,
|
2021-01-02 02:25:39 +01:00
|
|
|
box_top: Bag,
|
2021-01-01 23:07:16 +01:00
|
|
|
bag: Bag,
|
|
|
|
market: Market,
|
2021-01-07 23:44:44 +01:00
|
|
|
factories: tinyvec::ArrayVec<[Factory; 5]>, // TODO set to 9?
|
|
|
|
boards: tinyvec::ArrayVec<[Board; 2]> // TODO set to 4?
|
2021-01-01 23:07:16 +01:00
|
|
|
}
|
|
|
|
impl Game {
|
2021-01-06 14:37:51 +01:00
|
|
|
pub fn new(players: u8) -> Result<Game, &'static str> {
|
2021-01-06 10:26:33 +01:00
|
|
|
let n_factories = get_n_factories(players)?;
|
2021-01-07 23:44:44 +01:00
|
|
|
let mut factories = tinyvec::ArrayVec::<[Factory; 5]>::new();
|
2021-01-01 23:07:16 +01:00
|
|
|
for _ in 0..n_factories {
|
|
|
|
factories.push(Factory::default())
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:44:44 +01:00
|
|
|
let mut boards = tinyvec::ArrayVec::<[Board; 2]>::new();
|
2021-01-01 23:07:16 +01:00
|
|
|
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-05-16 03:40:11 +02:00
|
|
|
box_top: Bag {
|
|
|
|
blue: 0,
|
|
|
|
yellow: 0,
|
|
|
|
red: 0,
|
|
|
|
black: 0,
|
|
|
|
teal: 0,
|
|
|
|
},
|
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-06 14:37:51 +01:00
|
|
|
pub fn fill(&mut self, mut rng: StdRng) -> 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-05-16 03:40:11 +02:00
|
|
|
if self.bag.is_empty() && !self.box_top.is_empty() {
|
|
|
|
self.bag = self.box_top;
|
|
|
|
self.box_top = Bag { blue: 0, yellow: 0, red: 0, black: 0, teal: 0};
|
2021-01-03 00:15:45 +01:00
|
|
|
}
|
2021-05-16 03:40:11 +02:00
|
|
|
else if self.bag.is_empty() {
|
2021-01-03 00:15:45 +01:00
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
else {
|
2021-05-16 03:40:11 +02:00
|
|
|
let choices = [Tile::Blue, Tile::Yellow, Tile::Red, Tile::Black, Tile::Teal];
|
|
|
|
let weights = [self.bag.blue, self.bag.yellow, self.bag.red, self.bag.black, self.bag.teal];
|
|
|
|
let dist = WeightedIndex::new(&weights).unwrap();
|
|
|
|
let tile = choices[dist.sample(&mut rng)];
|
|
|
|
|
|
|
|
match tile {
|
|
|
|
Tile::Start => {return Err("This should never happen")}
|
|
|
|
Tile::Blue => {self.bag.blue -= 1}
|
|
|
|
Tile::Yellow => {self.bag.yellow -= 1}
|
|
|
|
Tile::Red => {self.bag.red -= 1}
|
|
|
|
Tile::Black => {self.bag.black -= 1}
|
|
|
|
Tile::Teal => {self.bag.teal -= 1}
|
|
|
|
}
|
|
|
|
factory.push(tile)
|
2021-01-03 00:15:45 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-16 00:23:45 +02:00
|
|
|
factory.sort_unstable();
|
2021-01-03 00:15:45 +01:00
|
|
|
};
|
2021-05-16 00:23:45 +02:00
|
|
|
self.factories.sort_unstable();
|
2021-01-02 02:25:39 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-03 22:09:40 +01:00
|
|
|
fn score(&mut self) -> Result<(), &'static str> {
|
|
|
|
for board in &mut self.boards {
|
|
|
|
for row in 0..4 {
|
|
|
|
if board.patterns[row].len() == (row + 1) {
|
|
|
|
let color = board.patterns[row].remove(0);
|
|
|
|
let index = Board::wall_index(color, row)?;
|
|
|
|
board.wall[row][index] = true;
|
|
|
|
board.score += board.connected((row, index));
|
|
|
|
|
2021-05-16 03:40:11 +02:00
|
|
|
match color {
|
|
|
|
Tile::Start => {return Err("This should never happen")}
|
|
|
|
Tile::Blue => {self.box_top.blue += (row as u8 + 1)}
|
|
|
|
Tile::Yellow => {self.box_top.yellow += (row as u8 + 1)}
|
|
|
|
Tile::Red => {self.box_top.red += (row as u8 + 1)}
|
|
|
|
Tile::Black => {self.box_top.black += (row as u8 + 1)}
|
|
|
|
Tile::Teal => {self.box_top.teal += (row as u8 + 1)}
|
|
|
|
}
|
|
|
|
//self.box_top.extend_from_slice(board.patterns[row].as_slice());
|
2021-01-06 14:37:51 +01:00
|
|
|
board.patterns[row].clear();
|
2021-01-03 22:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let negative = match board.floor.len() {
|
|
|
|
0 => 0,
|
|
|
|
1 => 1,
|
|
|
|
2 => 2,
|
|
|
|
3 => 4,
|
|
|
|
4 => 6,
|
|
|
|
5 => 8,
|
|
|
|
6 => 11,
|
|
|
|
_ => 14
|
|
|
|
};
|
|
|
|
board.score -= negative;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-07 12:52:30 +01:00
|
|
|
// #[no_alloc(forbid)]
|
2021-01-04 15:45:07 +01:00
|
|
|
pub fn do_move(&mut self, game_move: GameMove) -> Result<(), &'static str> {
|
2021-01-07 23:44:44 +01:00
|
|
|
let board = &mut self.boards[self.player as usize];
|
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) => {
|
2021-01-03 22:09:40 +01:00
|
|
|
if self.market.contains(&game_move.1) {
|
2021-01-05 15:47:12 +01:00
|
|
|
let mut hand= self.market.clone();
|
|
|
|
hand.retain(|x| *x == Tile::Start || *x == game_move.1);
|
2021-01-04 15:45:07 +01:00
|
|
|
self.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
2021-01-03 22:09:40 +01:00
|
|
|
|
2021-01-06 15:39:27 +01:00
|
|
|
board.floor.extend_from_slice(hand.as_slice());
|
|
|
|
hand.clear();
|
2021-01-03 22:09:40 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Err("Market does not contain selected tile")
|
|
|
|
}
|
2021-01-03 14:27:27 +01:00
|
|
|
},
|
2021-01-04 10:01:06 +01:00
|
|
|
GameMove(0, _, 1..=6) => {
|
2021-01-03 22:09:40 +01:00
|
|
|
if self.market.len() == 0 {
|
|
|
|
return Err("Market is empty");
|
|
|
|
}
|
|
|
|
else if self.market.contains(&game_move.1) {
|
|
|
|
let target = &mut board.patterns[game_move.2 - 1];
|
2021-01-04 11:24:49 +01:00
|
|
|
if target.first().is_some() && target[0] != game_move.1 {
|
|
|
|
return Err("That pattern line already contains a different color")
|
|
|
|
}
|
2021-01-03 22:09:40 +01:00
|
|
|
let empty = game_move.2 - target.len();
|
|
|
|
|
|
|
|
if empty == 0 {
|
|
|
|
return Err("That pattern is full")
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut hand = self.market.deref().clone();
|
2021-01-05 15:47:12 +01:00
|
|
|
hand.retain(|x| *x == Tile::Start || *x == game_move.1);
|
2021-01-04 15:45:07 +01:00
|
|
|
self.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
2021-01-03 22:09:40 +01:00
|
|
|
|
|
|
|
for tile in hand.drain(..) {
|
2021-01-05 23:04:38 +01:00
|
|
|
let empty = game_move.2 - target.len();
|
2021-01-03 22:09:40 +01:00
|
|
|
if tile == Tile::Start {
|
|
|
|
board.floor.push(tile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if empty >= 1 {
|
|
|
|
target.push(tile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
board.floor.push(tile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Err("Market does not contain selected tile")
|
2021-01-03 14:27:27 +01:00
|
|
|
}
|
|
|
|
},
|
2021-01-03 22:09:40 +01:00
|
|
|
GameMove(1..=9, _, _) => {
|
2021-01-07 23:44:44 +01:00
|
|
|
let board = &mut self.boards[self.player as usize];
|
2021-01-06 15:14:56 +01:00
|
|
|
if game_move.0 > self.factories.len() {
|
2021-01-04 10:01:06 +01:00
|
|
|
return Err("That factory is out of bounds");
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:37:51 +01:00
|
|
|
let factory = self.factories[game_move.0 - 1].deref_mut();
|
2021-01-04 15:45:07 +01:00
|
|
|
if factory.contains(&game_move.1) {
|
2021-01-05 15:47:12 +01:00
|
|
|
let mut hand = factory.clone();
|
|
|
|
hand.retain(|x| *x == game_move.1);
|
|
|
|
factory.retain(|x| *x != game_move.1);
|
2021-01-03 22:09:40 +01:00
|
|
|
|
2021-01-06 14:37:51 +01:00
|
|
|
self.market.extend_from_slice(factory.as_slice());
|
|
|
|
factory.clear();
|
2021-01-03 22:09:40 +01:00
|
|
|
|
|
|
|
match game_move.2 {
|
|
|
|
0 => {
|
2021-01-06 14:37:51 +01:00
|
|
|
board.floor.extend_from_slice(hand.as_slice());
|
2021-01-03 22:09:40 +01:00
|
|
|
},
|
|
|
|
1..=9 => {
|
|
|
|
let target = &mut board.patterns[game_move.2 - 1];
|
2021-01-04 11:24:49 +01:00
|
|
|
if target.first().is_some() && target[0] != game_move.1 {
|
|
|
|
return Err("That pattern line already contains a different color")
|
|
|
|
}
|
2021-01-03 22:09:40 +01:00
|
|
|
let empty = game_move.2 - target.len();
|
|
|
|
if hand.len() <= empty {
|
2021-01-06 14:37:51 +01:00
|
|
|
target.extend_from_slice(hand.as_slice());
|
|
|
|
hand.clear();
|
2021-01-03 22:09:40 +01:00
|
|
|
}
|
|
|
|
else if empty != 0 {
|
|
|
|
for tile in hand.drain(..) {
|
2021-01-04 15:45:07 +01:00
|
|
|
let empty = game_move.2 - target.len();
|
2021-01-03 22:09:40 +01:00
|
|
|
if empty >= 1 {
|
|
|
|
target.push(tile);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
board.floor.push(tile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Err("That pattern line is full")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => return Err("Not a valid destination")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Err("That tile is not in that factory")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
GameMove(_,_,_) => return Err("Not a valid move")
|
2021-01-03 00:15:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:39:27 +01:00
|
|
|
/*
|
2021-01-03 22:09:40 +01:00
|
|
|
let mut empty = true;
|
2021-01-04 15:45:07 +01:00
|
|
|
for factory in &mut self.factories {
|
2021-01-03 22:09:40 +01:00
|
|
|
if factory.len() != 0 {
|
|
|
|
empty = false;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-03 04:44:48 +01:00
|
|
|
}
|
2021-01-03 22:09:40 +01:00
|
|
|
if empty == true {
|
2021-01-04 15:45:07 +01:00
|
|
|
if self.market.len() != 0 {
|
2021-01-03 22:09:40 +01:00
|
|
|
empty = false;
|
|
|
|
}
|
2021-01-03 04:44:48 +01:00
|
|
|
}
|
|
|
|
|
2021-01-03 22:09:40 +01:00
|
|
|
if empty == true {
|
2021-01-04 15:45:07 +01:00
|
|
|
self.score()?;
|
2021-01-03 04:44:48 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-01-04 15:45:07 +01:00
|
|
|
self.player = (self.player + 1) % self.boards.len();
|
2021-01-03 00:15:45 +01:00
|
|
|
}
|
2021-01-06 15:39:27 +01:00
|
|
|
*/
|
2021-05-16 00:23:45 +02:00
|
|
|
|
|
|
|
self.factories.sort_unstable();
|
|
|
|
self.market.sort();
|
|
|
|
for board in &mut self.boards {
|
|
|
|
board.floor.sort_unstable();
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:44:44 +01:00
|
|
|
self.player = (self.player + 1) % self.boards.len() as u8;
|
2021-01-04 15:45:07 +01:00
|
|
|
self.turn += 1;
|
|
|
|
Ok(())
|
2021-01-03 00:15:45 +01:00
|
|
|
}
|
2021-01-03 04:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tests
|
|
|
|
|
2021-01-05 07:38:56 +01:00
|
|
|
pub fn complicated() -> Result<Game, &'static str> {
|
2021-01-06 14:37:51 +01:00
|
|
|
let mut game = Game::new(2)?;
|
2021-01-05 07:38:56 +01:00
|
|
|
|
|
|
|
let mut tiles = Tile::Blue;
|
|
|
|
|
|
|
|
for factory in &mut game.factories {
|
|
|
|
for _ in 0..4 {
|
|
|
|
factory.push(tiles);
|
|
|
|
tiles = tiles.into_iter().next().unwrap_or(Tile::Blue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(game)
|
|
|
|
}
|
|
|
|
|
2021-05-16 12:39:57 +02:00
|
|
|
pub fn some_game() -> Result<Game, &'static str> {
|
|
|
|
let mut game = Game::new(2)?;
|
|
|
|
|
|
|
|
let factories = &mut game.factories;
|
|
|
|
|
|
|
|
factories[0].push(Tile::Blue);
|
|
|
|
factories[0].push(Tile::Blue);
|
|
|
|
factories[0].push(Tile::Yellow);
|
|
|
|
factories[0].push(Tile::Black);
|
|
|
|
|
|
|
|
factories[1].push(Tile::Blue);
|
|
|
|
factories[1].push(Tile::Red);
|
|
|
|
factories[1].push(Tile::Red);
|
|
|
|
factories[1].push(Tile::Red);
|
|
|
|
|
|
|
|
factories[2].push(Tile::Blue);
|
|
|
|
factories[2].push(Tile::Red);
|
|
|
|
factories[2].push(Tile::Black);
|
|
|
|
factories[2].push(Tile::Teal);
|
|
|
|
|
|
|
|
factories[3].push(Tile::Blue);
|
|
|
|
factories[3].push(Tile::Red);
|
|
|
|
factories[3].push(Tile::Black);
|
|
|
|
factories[3].push(Tile::Teal);
|
|
|
|
|
|
|
|
factories[4].push(Tile::Yellow);
|
|
|
|
factories[4].push(Tile::Yellow);
|
|
|
|
factories[4].push(Tile::Black);
|
|
|
|
factories[4].push(Tile::Teal);
|
|
|
|
|
|
|
|
Ok(game)
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:09:40 +01:00
|
|
|
#[test]
|
|
|
|
fn connected() -> Result<(), String> {
|
|
|
|
let mut board = Board::default();
|
|
|
|
board.wall[0] = [false, false, false, false, false];
|
|
|
|
board.wall[1] = [true, false, true, false, false];
|
|
|
|
board.wall[2] = [true, false, true, false, false];
|
|
|
|
board.wall[3] = [true, false, false, false, false];
|
|
|
|
board.wall[4] = [true, true, true, false, false];
|
|
|
|
|
|
|
|
let coordinate = (4 as usize, Board::wall_index(Tile::Yellow, 4)?);
|
|
|
|
assert_eq!(coordinate, (4,0));
|
|
|
|
|
|
|
|
let score = board.connected(coordinate);
|
|
|
|
|
|
|
|
assert_eq!(score, 7);
|
|
|
|
Ok(())
|
2021-01-05 07:38:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn game_move_iter() {
|
|
|
|
let i = GameMove::default();
|
|
|
|
println!("Original: {:?}", i);
|
|
|
|
assert_eq!(i.into_iter().next().unwrap(), GameMove(1, Tile::Blue, 1));
|
|
|
|
|
|
|
|
assert_eq!(i.into_iter().count(), 5)
|
2021-01-07 12:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sizes() {
|
|
|
|
println!("size of azul game: {}", std::mem::size_of::<Game>());
|
2021-01-07 23:44:44 +01:00
|
|
|
}
|