RandomWithoutDirectRepetition & BagX2 randomizers

This commit is contained in:
dan63047 2023-11-15 18:31:25 +03:00
parent 3ec1b672bd
commit fbd10833b2
3 changed files with 49 additions and 8 deletions

View File

@ -4,15 +4,20 @@ use rand::thread_rng;
use super::{rotation_systems::PiecesData, resources::Piece};
pub trait Randomizer{
fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece>;
fn create() -> Self where Self: Sized;
fn populate_next(&mut self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece>;
}
pub struct Bag {}
pub struct RandomWithoutDirectRepetition {}
pub struct BagX2 {}
pub struct RandomWithoutDirectRepetition {
memory: usize
}
impl Randomizer for Bag {
fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece> {
fn populate_next(&mut self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece> {
let mut bag = vec![];
let mut id: usize = 0;
for _ in &pieces_data.pieces{
@ -23,11 +28,46 @@ impl Randomizer for Bag {
bag.shuffle(&mut rng);
bag
}
fn create() -> Self where Self: Sized {
Bag { }
}
}
impl Randomizer for BagX2 {
fn populate_next(&mut self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece> {
let mut bag = vec![];
let mut id: usize = 0;
for _ in &pieces_data.pieces{
bag.insert(id, Piece::create(pieces_data, id, board_width, board_height));
bag.insert(id, Piece::create(pieces_data, id, board_width, board_height));
id += 1;
}
let mut rng = thread_rng();
bag.shuffle(&mut rng);
bag
}
fn create() -> Self where Self: Sized {
BagX2 { }
}
}
impl Randomizer for RandomWithoutDirectRepetition {
fn populate_next(&self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece> {
fn populate_next(&mut self, pieces_data: &PiecesData, board_width: isize, board_height: isize) -> Vec<Piece> {
let random_number = random::<usize>() % pieces_data.pieces.len();
if random_number != self.memory {
self.memory = random_number;
vec![Piece::create(pieces_data, random_number, board_width, board_height)]
}else{
let random_number = random::<usize>() % pieces_data.pieces.len();
self.memory = random_number;
vec![Piece::create(pieces_data, random_number, board_width, board_height)]
}
}
fn create() -> Self where Self: Sized {
RandomWithoutDirectRepetition { memory: 65535 }
}
}

View File

@ -180,8 +180,9 @@ impl Engine {
self.next_queue.remove(0);
}
pub fn init(&mut self, rotation_system: &str){
pub fn init(&mut self, rotation_system: &str, randomizer: Box<dyn Randomizer + Sync + Send>){
self.rotation_system = ROTATION_SYSTEMS[rotation_system].clone();
self.randomizer = randomizer;
while self.next_queue.len() <= self.board.show_next as usize {
self.next_queue.append(&mut self.randomizer.populate_next(&self.rotation_system, self.board.width as isize, self.board.height as isize));
}

View File

@ -1,4 +1,4 @@
use super::{resources::Engine, rotation_systems::LockDelayMode, GameStates, GameloopStates};
use super::{resources::Engine, rotation_systems::LockDelayMode, GameStates, GameloopStates, randomizers::*};
use crate::engine::components::*;
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
@ -29,7 +29,7 @@ pub fn init_engine(
},
BoardVisual {},
));
engine.init("NRS");
engine.init("SRS", Box::new(BagX2::create()));
next_state.set(GameloopStates::Falling);
}