very slight improvements to tidepool::sim

This commit is contained in:
tali 2023-04-13 15:36:03 -04:00
parent 66bb54a48c
commit e62270cd42
2 changed files with 22 additions and 14 deletions

View File

@ -105,9 +105,10 @@ fn simulation(
eprintln!("#{}, {} left", pieces, sim.lines_left()); eprintln!("#{}, {} left", pieces, sim.lines_left());
} }
let (hold, next) = sim.queue(); let mut bot = bot::Bot::new(
let queue = mino::Queue::new(hold, &next); sim.matrix(),
let mut bot = bot::Bot::new(sim.matrix(), queue); mino::Queue::new(sim.queue().hold(), sim.queue().next()),
);
for i in 0..config.bot.iters { for i in 0..config.bot.iters {
bot.think(); bot.think();
if i % 1000 == 999 { if i % 1000 == 999 {

View File

@ -1,7 +1,8 @@
//! Implements a cheese race with automatically generated garbage and random bag system.
use mino::matrix::{Mat, MatBuf}; use mino::matrix::{Mat, MatBuf};
use mino::srs::{Piece, PieceType}; use mino::srs::{Piece, PieceType};
use rand::{Rng as _, SeedableRng as _}; use rand::{Rng as _, SeedableRng as _};
use std::collections::VecDeque;
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
pub struct Options { pub struct Options {
@ -15,6 +16,7 @@ pub struct Options {
pub type Rng = rand_xoshiro::SplitMix64; pub type Rng = rand_xoshiro::SplitMix64;
/// Cheese race simulation.
pub struct Simul { pub struct Simul {
queue: Queue, queue: Queue,
garbage: Garbage, garbage: Garbage,
@ -23,6 +25,8 @@ pub struct Simul {
} }
impl Simul { impl Simul {
/// Constructs a new simulation with PRNG seeded by the given values, and given game
/// configuration.
pub fn new(seed: u64, options: Options) -> Self { pub fn new(seed: u64, options: Options) -> Self {
let rng1 = Rng::seed_from_u64(seed); let rng1 = Rng::seed_from_u64(seed);
let rng2 = rng1.clone(); let rng2 = rng1.clone();
@ -40,6 +44,8 @@ impl Simul {
} }
} }
/// Plays the given piece and then advances the game state, generating more previews
/// and garbage lines.
pub fn play(&mut self, piece: Piece) { pub fn play(&mut self, piece: Piece) {
// play piece // play piece
self.queue.remove(piece.ty); self.queue.remove(piece.ty);
@ -54,8 +60,8 @@ impl Simul {
&self.matrix &self.matrix
} }
pub fn queue(&self) -> (Option<PieceType>, Vec<PieceType>) { pub fn queue(&self) -> &Queue {
(self.queue.hold(), self.queue.next()) &self.queue
} }
pub fn lines_left(&self) -> usize { pub fn lines_left(&self) -> usize {
@ -67,7 +73,7 @@ impl Simul {
/// whenever pieces are consumed from the front of the queue. /// whenever pieces are consumed from the front of the queue.
pub struct Queue { pub struct Queue {
hold: Option<PieceType>, hold: Option<PieceType>,
next: VecDeque<PieceType>, next: Vec<PieceType>,
bag: Bag, bag: Bag,
} }
@ -90,10 +96,10 @@ impl Queue {
/// - `count`: number of next pieces. /// - `count`: number of next pieces.
pub fn new(rng: Rng, count: usize) -> Self { pub fn new(rng: Rng, count: usize) -> Self {
assert!(count > 0, "number of pieces cannot be zero"); assert!(count > 0, "number of pieces cannot be zero");
let mut next = VecDeque::with_capacity(count); let mut next = Vec::with_capacity(count);
let mut bag = Bag::new(rng); let mut bag = Bag::new(rng);
while next.len() < count { while next.len() < count {
next.push_back(bag.pop()); next.push(bag.pop());
} }
Self { Self {
hold: None, hold: None,
@ -108,8 +114,8 @@ impl Queue {
} }
/// Returns the list of the next previews. /// Returns the list of the next previews.
pub fn next(&self) -> Vec<PieceType> { pub fn next(&self) -> &[PieceType] {
self.next.iter().copied().collect() &self.next
} }
/// Remove a piece from the front of the queue. The piece must either by the current /// Remove a piece from the front of the queue. The piece must either by the current
@ -135,8 +141,9 @@ impl Queue {
} }
fn pop_front(&mut self) -> PieceType { fn pop_front(&mut self) -> PieceType {
let ty = self.next.pop_front().expect("len > 0"); let ty = self.next[0];
self.next.push_back(self.bag.pop()); self.next.remove(0);
self.next.push(self.bag.pop());
ty ty
} }
} }
@ -448,7 +455,7 @@ mod tests {
".xxxxxxxxx"; ".xxxxxxxxx";
}, },
); );
assert_eq!(sim.queue().1, { assert_eq!(sim.queue().next(), {
use PieceType::*; use PieceType::*;
vec![J, L, T, Z, S, O, I] vec![J, L, T, Z, S, O, I]
}); });