Game is Copy

This commit is contained in:
Daniel Olsen 2021-01-06 15:39:27 +01:00
parent 410e2f3bea
commit e6d7ca5001
2 changed files with 21 additions and 18 deletions

2
.vscode/launch.json vendored
View File

@ -19,7 +19,7 @@
"kind": "bin" "kind": "bin"
} }
}, },
"args": [], "args": ["1"],
"cwd": "${workspaceFolder}" "cwd": "${workspaceFolder}"
}, },
{ {

View File

@ -213,24 +213,25 @@ impl DerefMut for Factory {
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
struct Market (SmallVec<[Tile; 28]>); struct Market (tinyvec::ArrayVec<[Tile; 28]>);
impl Default for Market { impl Default for Market {
fn default() -> Self { fn default() -> Self {
let mut market = SmallVec::<[Tile; 28]>::new(); // tiles * factories + start = 3 * 9 + 1 = 28
let mut market = tinyvec::ArrayVec::<[Tile; 28]>::new();
market.push(Tile::Start); market.push(Tile::Start);
Market(market) Market(market)
} }
} }
impl Deref for Market { impl Deref for Market {
type Target = SmallVec<[Tile; 28]>; type Target = tinyvec::ArrayVec<[Tile; 28]>;
fn deref(&self) -> &SmallVec<[Tile; 28]> { fn deref(&self) -> &tinyvec::ArrayVec<[Tile; 28]> {
&self.0 &self.0
} }
} }
impl DerefMut for Market { impl DerefMut for Market {
fn deref_mut(&mut self) -> &mut SmallVec<[Tile; 28]> { fn deref_mut(&mut self) -> &mut tinyvec::ArrayVec<[Tile; 28]> {
&mut self.0 &mut self.0
} }
} }
@ -240,11 +241,11 @@ type Patterns = [tinyvec::ArrayVec<[Tile; 5]>; 5];
type Row = [bool; 5]; type Row = [bool; 5];
type Wall = [Row; 5]; type Wall = [Row; 5];
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default, Copy)]
struct Board { struct Board {
score: u8, score: u8,
wall: Wall, wall: Wall,
floor: SmallVec<[Tile; 7]>, floor: tinyvec::ArrayVec<[Tile; 20]>,
patterns: Patterns, patterns: Patterns,
} }
impl Board { impl Board {
@ -344,25 +345,25 @@ impl Board {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub struct Game { pub struct Game {
turn: u32, turn: u32,
player: usize, player: usize,
box_top: Bag, box_top: Bag,
bag: Bag, bag: Bag,
market: Market, market: Market,
factories: SmallVec<[Factory; 5]>, // TODO set to 9? factories: tinyvec::ArrayVec<[Factory; 9]>, // TODO set to 9?
boards: SmallVec<[Board; 2]> // TODO set to 4? boards: tinyvec::ArrayVec<[Board; 4]> // TODO set to 4?
} }
impl Game { impl Game {
pub fn new(players: u8) -> Result<Game, &'static str> { pub fn new(players: u8) -> Result<Game, &'static str> {
let n_factories = get_n_factories(players)?; let n_factories = get_n_factories(players)?;
let mut factories = SmallVec::<[Factory; 5]>::new(); let mut factories = tinyvec::ArrayVec::<[Factory; 9]>::new();
for _ in 0..n_factories { for _ in 0..n_factories {
factories.push(Factory::default()) factories.push(Factory::default())
} }
let mut boards = SmallVec::<[Board; 2]>::new(); let mut boards = tinyvec::ArrayVec::<[Board; 4]>::new();
for _ in 0..players { for _ in 0..players {
boards.push(Board::default()); boards.push(Board::default());
} }
@ -429,7 +430,7 @@ impl Game {
} }
Ok(()) Ok(())
} }
// #[no_alloc(forbid)] #[no_alloc(forbid)]
pub fn do_move(&mut self, game_move: GameMove) -> Result<(), &'static str> { pub fn do_move(&mut self, game_move: GameMove) -> Result<(), &'static str> {
let board = &mut self.boards[self.player]; let board = &mut self.boards[self.player];
match game_move { match game_move {
@ -440,7 +441,8 @@ impl Game {
hand.retain(|x| *x == Tile::Start || *x == game_move.1); hand.retain(|x| *x == Tile::Start || *x == game_move.1);
self.market.retain(|x| *x != Tile::Start && *x != game_move.1); self.market.retain(|x| *x != Tile::Start && *x != game_move.1);
board.floor.append(&mut hand); board.floor.extend_from_slice(hand.as_slice());
hand.clear();
} }
else { else {
return Err("Market does not contain selected tile") return Err("Market does not contain selected tile")
@ -538,6 +540,7 @@ impl Game {
GameMove(_,_,_) => return Err("Not a valid move") GameMove(_,_,_) => return Err("Not a valid move")
} }
/*
let mut empty = true; let mut empty = true;
for factory in &mut self.factories { for factory in &mut self.factories {
if factory.len() != 0 { if factory.len() != 0 {
@ -557,9 +560,9 @@ impl Game {
else { else {
self.player = (self.player + 1) % self.boards.len(); self.player = (self.player + 1) % self.boards.len();
} }
*/
self.player = (self.player + 1) % self.boards.len();
self.turn += 1; self.turn += 1;
Ok(()) Ok(())
} }
} }