Convert do_task to changing the referenced game
This commit is contained in:
parent
eb9998d639
commit
f2758fcb14
File diff suppressed because it is too large
Load Diff
46
src/azul.rs
46
src/azul.rs
|
@ -238,7 +238,7 @@ pub struct Game {
|
||||||
boards: Vec<Board>
|
boards: Vec<Board>
|
||||||
}
|
}
|
||||||
impl Game {
|
impl Game {
|
||||||
pub fn new(players: usize, random: StdRng) -> Result<Self, &'static str> {
|
pub fn new(players: usize, random: StdRng) -> Result<Game, &'static str> {
|
||||||
coz::scope!("create game");
|
coz::scope!("create game");
|
||||||
let n_factories = match players {
|
let n_factories = match players {
|
||||||
2 => 5,
|
2 => 5,
|
||||||
|
@ -320,23 +320,17 @@ impl Game {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn do_move(&self, game_move: GameMove) -> Result<Game, &'static str> {
|
pub fn do_move(&mut self, game_move: GameMove) -> Result<(), &'static str> {
|
||||||
coz::scope!("do a move");
|
coz::scope!("do a move");
|
||||||
if game_move.1 == Tile::Start {
|
|
||||||
return Err("You can't take the start tile alone")
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut game = self.clone();
|
|
||||||
|
|
||||||
let board = &mut game.boards[self.player];
|
|
||||||
|
|
||||||
|
let board = &mut self.boards[self.player];
|
||||||
match game_move {
|
match game_move {
|
||||||
GameMove(_, Tile::Start, _) => return Err("You can't take the start tile specifically"),
|
GameMove(_, Tile::Start, _) => return Err("You can't take the start tile specifically"),
|
||||||
GameMove(0, _, 0) => {
|
GameMove(0, _, 0) => {
|
||||||
if self.market.contains(&game_move.1) {
|
if self.market.contains(&game_move.1) {
|
||||||
let mut hand = self.market.deref().clone();
|
let mut hand = self.market.deref().clone();
|
||||||
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
||||||
game.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.append(&mut hand)
|
||||||
}
|
}
|
||||||
|
@ -361,7 +355,7 @@ impl Game {
|
||||||
|
|
||||||
let mut hand = self.market.deref().clone();
|
let mut hand = self.market.deref().clone();
|
||||||
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
hand.retain(|&x| x == Tile::Start || x == game_move.1);
|
||||||
game.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
self.market.retain(|x| *x != Tile::Start && *x != game_move.1);
|
||||||
|
|
||||||
for tile in hand.drain(..) {
|
for tile in hand.drain(..) {
|
||||||
let empty = game_move.2 - &target.len();
|
let empty = game_move.2 - &target.len();
|
||||||
|
@ -383,21 +377,19 @@ impl Game {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
GameMove(1..=9, _, _) => {
|
GameMove(1..=9, _, _) => {
|
||||||
|
let board = &mut self.boards[self.player];
|
||||||
if game_move.0 > self.factories.len() - 1 {
|
if game_move.0 > self.factories.len() - 1 {
|
||||||
return Err("That factory is out of bounds");
|
return Err("That factory is out of bounds");
|
||||||
}
|
}
|
||||||
|
|
||||||
let old_factory = &self.factories[game_move.0 - 1];
|
let factory = &mut self.factories[game_move.0 - 1];
|
||||||
if old_factory.contains(&game_move.1) {
|
if factory.contains(&game_move.1) {
|
||||||
|
let mut hand = factory.deref().clone();
|
||||||
let mut new_factory = &mut game.factories[game_move.0 - 1];
|
|
||||||
|
|
||||||
let mut hand = old_factory.deref().clone();
|
|
||||||
|
|
||||||
hand.retain(|&x| x == game_move.1);
|
hand.retain(|&x| x == game_move.1);
|
||||||
new_factory.retain(|&x| x != game_move.1);
|
factory.retain(|&x| x != game_move.1);
|
||||||
|
|
||||||
game.market.append(&mut new_factory);
|
self.market.append(factory);
|
||||||
|
|
||||||
match game_move.2 {
|
match game_move.2 {
|
||||||
0 => {
|
0 => {
|
||||||
|
@ -414,7 +406,7 @@ impl Game {
|
||||||
}
|
}
|
||||||
else if empty != 0 {
|
else if empty != 0 {
|
||||||
for tile in hand.drain(..) {
|
for tile in hand.drain(..) {
|
||||||
let empty = game_move.2 - &target.len();
|
let empty = game_move.2 - target.len();
|
||||||
if empty >= 1 {
|
if empty >= 1 {
|
||||||
target.push(tile);
|
target.push(tile);
|
||||||
}
|
}
|
||||||
|
@ -438,28 +430,28 @@ impl Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut empty = true;
|
let mut empty = true;
|
||||||
for factory in &game.factories {
|
for factory in &mut self.factories {
|
||||||
if factory.len() != 0 {
|
if factory.len() != 0 {
|
||||||
empty = false;
|
empty = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if empty == true {
|
if empty == true {
|
||||||
if game.market.len() != 0 {
|
if self.market.len() != 0 {
|
||||||
empty = false;
|
empty = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if empty == true {
|
if empty == true {
|
||||||
game.score()?;
|
self.score()?;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
game.player = (game.player + 1) % self.boards.len();
|
self.player = (self.player + 1) % self.boards.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
game.turn += 1;
|
self.turn += 1;
|
||||||
|
|
||||||
Ok(game)
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -468,7 +460,7 @@ impl Game {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bag() {
|
fn bag() {
|
||||||
let game = Game::new(2).unwrap();
|
let game = Game::new(2, StdRng::seed_from_u64(123)).unwrap();
|
||||||
let bag = game.bag;
|
let bag = game.bag;
|
||||||
assert_eq!(bag.len(), 100);
|
assert_eq!(bag.len(), 100);
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use rand::prelude::*;
|
||||||
|
|
||||||
fn main() -> Result<(), &'static str> {
|
fn main() -> Result<(), &'static str> {
|
||||||
let mut g_rng = StdRng::seed_from_u64(42);
|
let mut g_rng = StdRng::seed_from_u64(42);
|
||||||
for _ in 0..10000 {
|
for _ in 0..500000 {
|
||||||
let rng = match StdRng::from_rng(&mut g_rng) {
|
let rng = match StdRng::from_rng(&mut g_rng) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -33,9 +33,7 @@ fn run(rng: StdRng) -> Result<(), &'static str> {
|
||||||
//println!("{:?}: {}", i, e);
|
//println!("{:?}: {}", i, e);
|
||||||
game_move = i.next();
|
game_move = i.next();
|
||||||
},
|
},
|
||||||
Ok(g) => {
|
Ok(_) => {
|
||||||
//println!("{:?}", i);
|
|
||||||
game = g;
|
|
||||||
all_err = false;
|
all_err = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue