472 lines
12 KiB
Rust
472 lines
12 KiB
Rust
//! 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::ops::RangeInclusive;
|
|
|
|
pub struct Options {
|
|
/// Total number of garbage lines required to clear.
|
|
pub goal: usize,
|
|
/// Min/max number of garbage lines on the matrix at a given time.
|
|
pub garbage: RangeInclusive<usize>,
|
|
/// Number of preview pieces.
|
|
pub previews: usize,
|
|
}
|
|
|
|
pub type Rng = rand_xoshiro::SplitMix64;
|
|
|
|
/// Cheese race simulation.
|
|
pub struct Simul {
|
|
queue: Queue,
|
|
garbage: Garbage,
|
|
matrix: MatBuf,
|
|
pieces: usize,
|
|
lines_left: usize,
|
|
}
|
|
|
|
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();
|
|
|
|
let queue = Queue::new(rng1, options.previews);
|
|
let mut garbage = Garbage::new(rng2, options.goal, options.garbage);
|
|
let mut matrix = MatBuf::new();
|
|
garbage.insert(&mut matrix, false);
|
|
|
|
Self {
|
|
queue,
|
|
garbage,
|
|
matrix,
|
|
pieces: 0,
|
|
lines_left: options.goal,
|
|
}
|
|
}
|
|
|
|
/// 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.pieces += 1;
|
|
self.queue.remove(piece.ty);
|
|
piece.cells().fill(&mut self.matrix);
|
|
|
|
// process line clears
|
|
let c = self.matrix.clear_lines();
|
|
self.lines_left -= self.garbage.clear(c.start);
|
|
self.garbage.insert(&mut self.matrix, !c.is_empty());
|
|
}
|
|
|
|
pub fn matrix(&self) -> &Mat {
|
|
&self.matrix
|
|
}
|
|
|
|
pub fn queue(&self) -> &Queue {
|
|
&self.queue
|
|
}
|
|
|
|
pub fn pieces(&self) -> usize {
|
|
self.pieces
|
|
}
|
|
|
|
pub fn lines_left(&self) -> usize {
|
|
self.lines_left
|
|
}
|
|
}
|
|
|
|
/// Manages the preview pieces and hold slot, and automatically refills the previews
|
|
/// whenever pieces are consumed from the front of the queue.
|
|
pub struct Queue {
|
|
hold: Option<PieceType>,
|
|
next: Vec<PieceType>,
|
|
bag: Bag,
|
|
}
|
|
|
|
static PIECES: [PieceType; 7] = [
|
|
// this order is arbitrary (alphabetical), but must be set in stone in order for bags
|
|
// to be reproducible by identical PRNG's.
|
|
PieceType::I,
|
|
PieceType::J,
|
|
PieceType::L,
|
|
PieceType::O,
|
|
PieceType::S,
|
|
PieceType::T,
|
|
PieceType::Z,
|
|
];
|
|
|
|
impl Queue {
|
|
/// Constructs a new queue.
|
|
///
|
|
/// - `rng`: random number source
|
|
/// - `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 = Vec::with_capacity(count);
|
|
let mut bag = Bag::new(rng);
|
|
while next.len() < count {
|
|
next.push(bag.pop());
|
|
}
|
|
Self {
|
|
hold: None,
|
|
next,
|
|
bag,
|
|
}
|
|
}
|
|
|
|
/// Returns the current piece in the hold slot.
|
|
pub fn hold(&self) -> Option<PieceType> {
|
|
self.hold
|
|
}
|
|
|
|
/// Returns the list of the next previews.
|
|
pub fn next(&self) -> &[PieceType] {
|
|
&self.next
|
|
}
|
|
|
|
/// Remove a piece from the front of the queue. The piece must either by the current
|
|
/// piece at the front of the next-previews, or it must be reachable by swapping with
|
|
/// the piece in hold; if the hold is empty then the second piece in the queue is
|
|
/// reachable by moving the first piece into the hold slot.
|
|
///
|
|
/// Panics if the given piece is not reachable from this queue.
|
|
pub fn remove(&mut self, ty: PieceType) {
|
|
// remove current piece from front of queue
|
|
let top = self.pop_front();
|
|
if top != ty {
|
|
// if not placing current piece, then must be placing either the hold piece or
|
|
// the piece reachable by hold (if hold was previously empty).
|
|
if let Some(held) = self.hold.replace(top) {
|
|
// hold piece
|
|
assert_eq!(ty, held);
|
|
} else {
|
|
// hold empty, so get next reachable piece
|
|
assert_eq!(ty, self.pop_front());
|
|
}
|
|
}
|
|
}
|
|
|
|
fn pop_front(&mut self) -> PieceType {
|
|
let ty = self.next[0];
|
|
self.next.remove(0);
|
|
self.next.push(self.bag.pop());
|
|
ty
|
|
}
|
|
}
|
|
|
|
struct Bag {
|
|
rng: Rng,
|
|
bag: Vec<PieceType>,
|
|
pos: u32,
|
|
}
|
|
|
|
impl Bag {
|
|
fn new(rng: Rng) -> Self {
|
|
let bag = PIECES.to_vec();
|
|
Self { rng, bag, pos: 0 }
|
|
}
|
|
|
|
fn pop(&mut self) -> PieceType {
|
|
static N: u32 = PIECES.len() as u32;
|
|
let i = self.pos;
|
|
let j = self.rng.gen_range(i..N); // gen u32 instead of usize so its deterministic
|
|
|
|
self.bag.swap(i as usize, j as usize);
|
|
self.pos += 1;
|
|
self.pos %= N;
|
|
|
|
self.bag[i as usize]
|
|
}
|
|
}
|
|
|
|
/// Manages the current level of garbage and the remaining garbage lines.
|
|
pub struct Garbage {
|
|
cheese: std::iter::Take<Cheese>,
|
|
// current level of garbage on the matrix
|
|
level: i16,
|
|
// min/max garbage to insert on the matrix
|
|
range: RangeInclusive<usize>,
|
|
}
|
|
|
|
impl Garbage {
|
|
/// Constructs a new garbage generator.
|
|
///
|
|
/// - `rng`: random number source
|
|
/// - `count`: total number of garbage rows to insert
|
|
/// - `range`: min/max amount of garbage on the matrix at a given time
|
|
pub fn new(rng: Rng, count: usize, range: RangeInclusive<usize>) -> Self {
|
|
Self {
|
|
cheese: Cheese::new(rng).take(count),
|
|
level: 0,
|
|
range,
|
|
}
|
|
}
|
|
|
|
/// Signals that a line clear happened at row `min_y`. This is necessary to determine
|
|
/// how much garbage needs to be inserted. Returns the number of garbage lines that
|
|
/// were removed from this line clear.
|
|
pub fn clear(&mut self, min_y: i16) -> usize {
|
|
if min_y < self.level {
|
|
let removed = (self.level - min_y) as usize;
|
|
self.level = min_y;
|
|
removed
|
|
} else {
|
|
0
|
|
}
|
|
}
|
|
|
|
/// Inserts new garbage lines to the bottom of `mat`. If `comboing` is `true` then
|
|
/// less garbage lines are inserted (like jstris's cheese race mechanics). Returns the
|
|
/// number of new garbage lines inserted.
|
|
pub fn insert(&mut self, mat: &mut MatBuf, comboing: bool) -> usize {
|
|
let target = if comboing {
|
|
*self.range.start()
|
|
} else {
|
|
*self.range.end()
|
|
};
|
|
let difference = target.saturating_sub(self.level as usize);
|
|
|
|
(&mut self.cheese)
|
|
.take(difference)
|
|
.map(|col| {
|
|
mat.shift_up();
|
|
mat.fill_row(0, garbage_row(col));
|
|
self.level += 1;
|
|
})
|
|
.count()
|
|
}
|
|
}
|
|
|
|
fn garbage_row(col: u32) -> u16 {
|
|
!(1 << col)
|
|
}
|
|
|
|
struct Cheese {
|
|
rng: Rng,
|
|
col: u32,
|
|
}
|
|
|
|
impl Cheese {
|
|
fn new(mut rng: Rng) -> Self {
|
|
let col = rng.gen_range(0..10);
|
|
Self { rng, col }
|
|
}
|
|
}
|
|
|
|
impl Iterator for Cheese {
|
|
type Item = u32;
|
|
#[inline]
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
self.col += self.rng.gen_range(1..10);
|
|
self.col %= 10;
|
|
Some(self.col)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use mino::mat;
|
|
|
|
const TEST_SEED: u64 = 0x1234567800ABCDEF;
|
|
|
|
fn make_rng() -> Rng {
|
|
Rng::seed_from_u64(TEST_SEED)
|
|
}
|
|
|
|
#[test]
|
|
fn test_bag_order() {
|
|
let mut tys = std::collections::HashSet::new();
|
|
let mut bag = Bag::new(make_rng());
|
|
for _ in 0..50 {
|
|
tys.extend(PIECES);
|
|
for _ in 0..7 {
|
|
let ch = bag.pop();
|
|
assert!(tys.remove(&ch), "{ch:?}");
|
|
}
|
|
assert!(tys.is_empty());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_pop() {
|
|
let mut que = Queue::new(make_rng(), 5);
|
|
assert_eq!(que.hold(), None);
|
|
|
|
let next = que.next();
|
|
assert_eq!(next.len(), 5);
|
|
|
|
// [](a)bcde
|
|
let a = next[0];
|
|
let b = next[1];
|
|
let c = next[2];
|
|
let d = next[3];
|
|
let e = next[4];
|
|
assert_ne!(a, b);
|
|
assert_ne!(c, d);
|
|
assert_ne!(a, e);
|
|
|
|
// [](b)cdef -> a
|
|
que.remove(a);
|
|
let next = que.next();
|
|
assert_eq!(que.hold, None);
|
|
assert_eq!(next.len(), 5);
|
|
assert_eq!(next[0], b);
|
|
assert_eq!(next[1], c);
|
|
assert_eq!(next[2], d);
|
|
assert_eq!(next[3], e);
|
|
let f = next[4];
|
|
|
|
// [b](d)efgh -> c
|
|
que.remove(c);
|
|
assert_eq!(que.hold(), Some(b));
|
|
let next = que.next();
|
|
assert_eq!(next.len(), 5);
|
|
assert_eq!(next[0], d);
|
|
assert_eq!(next[1], e);
|
|
assert_eq!(next[2], f);
|
|
let g = next[3];
|
|
let h = next[4];
|
|
|
|
// [b](e)fghi -> d
|
|
que.remove(d);
|
|
assert_eq!(que.hold(), Some(b));
|
|
let next = que.next();
|
|
assert_eq!(next.len(), 5);
|
|
assert_eq!(next[0], e);
|
|
assert_eq!(next[1], f);
|
|
assert_eq!(next[2], g);
|
|
assert_eq!(next[3], h);
|
|
let i = next[4];
|
|
|
|
// [e](f)ghij -> b
|
|
que.remove(b);
|
|
assert_eq!(que.hold(), Some(e));
|
|
let next = que.next();
|
|
assert_eq!(next.len(), 5);
|
|
assert_eq!(next[0], f);
|
|
assert_eq!(next[1], g);
|
|
assert_eq!(next[2], h);
|
|
assert_eq!(next[3], i);
|
|
//let j = next[4];
|
|
}
|
|
|
|
#[test]
|
|
fn test_garbage_row() {
|
|
let mat = mat! {
|
|
"xxxxxxxxx.";
|
|
"xxxxx.xxxx";
|
|
"xx.xxxxxxx";
|
|
".xxxxxxxxx";
|
|
};
|
|
assert_eq!(mat[0], garbage_row(0));
|
|
assert_eq!(mat[1], garbage_row(2));
|
|
assert_eq!(mat[2], garbage_row(5));
|
|
assert_eq!(mat[3], garbage_row(9));
|
|
}
|
|
|
|
#[test]
|
|
fn test_cheese_no_duplicates() {
|
|
let cheese = Cheese::new(make_rng());
|
|
let mut prev = None;
|
|
let mut history = std::collections::HashSet::new();
|
|
for x1 in cheese.take(500) {
|
|
if let Some(x0) = prev {
|
|
assert_ne!(x0, x1);
|
|
}
|
|
prev = Some(x1);
|
|
history.insert(x1);
|
|
}
|
|
assert_eq!(history.len(), 10);
|
|
for x in 0..10 {
|
|
assert!(history.contains(&x));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_garbage_insert() {
|
|
let mut mat = MatBuf::new();
|
|
let mut garbage_left = 15;
|
|
let mut downstacked = 0;
|
|
let mut garbage = Garbage::new(make_rng(), garbage_left, 3..=9);
|
|
|
|
garbage_left -= garbage.insert(&mut mat, true);
|
|
assert_eq!(garbage_left, 12);
|
|
assert_eq!(mat.rows(), 3);
|
|
let m0 = mat[0];
|
|
let m1 = mat[1];
|
|
let m2 = mat[2];
|
|
|
|
garbage_left -= garbage.insert(&mut mat, false);
|
|
assert_eq!(garbage_left, 6);
|
|
assert_eq!(mat.rows(), 9);
|
|
assert_ne!(mat[5], m0);
|
|
assert_eq!(mat[6], m0);
|
|
assert_eq!(mat[7], m1);
|
|
assert_eq!(mat[8], m2);
|
|
|
|
mat.fill_row(9, mino::matrix::EMPTY_ROW | 0b1);
|
|
assert_eq!(garbage.insert(&mut mat, false), 0);
|
|
assert_eq!(mat.rows(), 10);
|
|
|
|
mat.fill_row(7, mino::matrix::FULL_ROW);
|
|
mat.fill_row(8, mino::matrix::FULL_ROW);
|
|
assert_eq!(mat.clear_lines(), 7..9);
|
|
assert_eq!(mat.rows(), 8);
|
|
|
|
downstacked += garbage.clear(7);
|
|
assert_eq!(downstacked, 2);
|
|
|
|
garbage_left -= garbage.insert(&mut mat, false);
|
|
assert_eq!(garbage_left, 4);
|
|
assert_eq!(mat.rows(), 10);
|
|
|
|
mat.clear();
|
|
downstacked += garbage.clear(0);
|
|
assert_eq!(downstacked, 11);
|
|
|
|
garbage_left -= garbage.insert(&mut mat, false);
|
|
assert_eq!(garbage_left, 0);
|
|
assert_eq!(mat.rows(), 4);
|
|
|
|
mat.clear();
|
|
downstacked += garbage.clear(0);
|
|
assert_eq!(downstacked, 15);
|
|
|
|
assert_eq!(garbage.insert(&mut mat, false), 0);
|
|
assert_eq!(mat.rows(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn test_deterministic() {
|
|
let sim = Simul::new(
|
|
TEST_SEED,
|
|
Options {
|
|
goal: 100,
|
|
garbage: 0..=9,
|
|
previews: 7,
|
|
},
|
|
);
|
|
assert_eq!(
|
|
sim.matrix(),
|
|
mat! {
|
|
"xxx.xxxxxx";
|
|
"xxxxx.xxxx";
|
|
".xxxxxxxxx";
|
|
"xxx.xxxxxx";
|
|
"x.xxxxxxxx";
|
|
".xxxxxxxxx";
|
|
"xxxxxxxxx.";
|
|
"xxxx.xxxxx";
|
|
".xxxxxxxxx";
|
|
},
|
|
);
|
|
assert_eq!(sim.queue().next(), {
|
|
use PieceType::*;
|
|
vec![J, L, T, Z, S, O, I]
|
|
});
|
|
}
|
|
}
|