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());
}
let (hold, next) = sim.queue();
let queue = mino::Queue::new(hold, &next);
let mut bot = bot::Bot::new(sim.matrix(), queue);
let mut bot = bot::Bot::new(
sim.matrix(),
mino::Queue::new(sim.queue().hold(), sim.queue().next()),
);
for i in 0..config.bot.iters {
bot.think();
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::srs::{Piece, PieceType};
use rand::{Rng as _, SeedableRng as _};
use std::collections::VecDeque;
use std::ops::RangeInclusive;
pub struct Options {
@ -15,6 +16,7 @@ pub struct Options {
pub type Rng = rand_xoshiro::SplitMix64;
/// Cheese race simulation.
pub struct Simul {
queue: Queue,
garbage: Garbage,
@ -23,6 +25,8 @@ pub struct 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 {
let rng1 = Rng::seed_from_u64(seed);
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) {
// play piece
self.queue.remove(piece.ty);
@ -54,8 +60,8 @@ impl Simul {
&self.matrix
}
pub fn queue(&self) -> (Option<PieceType>, Vec<PieceType>) {
(self.queue.hold(), self.queue.next())
pub fn queue(&self) -> &Queue {
&self.queue
}
pub fn lines_left(&self) -> usize {
@ -67,7 +73,7 @@ impl Simul {
/// whenever pieces are consumed from the front of the queue.
pub struct Queue {
hold: Option<PieceType>,
next: VecDeque<PieceType>,
next: Vec<PieceType>,
bag: Bag,
}
@ -90,10 +96,10 @@ impl Queue {
/// - `count`: number of next pieces.
pub fn new(rng: Rng, count: usize) -> Self {
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);
while next.len() < count {
next.push_back(bag.pop());
next.push(bag.pop());
}
Self {
hold: None,
@ -108,8 +114,8 @@ impl Queue {
}
/// Returns the list of the next previews.
pub fn next(&self) -> Vec<PieceType> {
self.next.iter().copied().collect()
pub fn next(&self) -> &[PieceType] {
&self.next
}
/// 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 {
let ty = self.next.pop_front().expect("len > 0");
self.next.push_back(self.bag.pop());
let ty = self.next[0];
self.next.remove(0);
self.next.push(self.bag.pop());
ty
}
}
@ -448,7 +455,7 @@ mod tests {
".xxxxxxxxx";
},
);
assert_eq!(sim.queue().1, {
assert_eq!(sim.queue().next(), {
use PieceType::*;
vec![J, L, T, Z, S, O, I]
});