forked from jana/hanoigame
add seeding for rng
This commit is contained in:
parent
cf5b93cb0d
commit
3e15a32a48
3 changed files with 147 additions and 8 deletions
24
src/main.rs
24
src/main.rs
|
|
@ -1,4 +1,7 @@
|
|||
use palette::{IntoColor, OklabHue, Oklch, Srgb};
|
||||
use rand::seq::SliceRandom as _;
|
||||
use rand_pcg::Pcg64;
|
||||
use rand_seeder::Seeder;
|
||||
use std::{
|
||||
collections::{BinaryHeap, HashMap, HashSet},
|
||||
fmt::Display,
|
||||
|
|
@ -643,15 +646,16 @@ pub struct GameGenerator {
|
|||
}
|
||||
|
||||
impl GameGenerator {
|
||||
pub fn generate<const TOWER_HEIGHT: usize>(self) -> Game<TOWER_HEIGHT> {
|
||||
pub fn generate<const TOWER_HEIGHT: usize>(self, rng: &mut Pcg64) -> Game<TOWER_HEIGHT> {
|
||||
let mut towers = Vec::new();
|
||||
let mut rings = HashSet::new();
|
||||
let mut rings = Vec::new();
|
||||
|
||||
for ring_type in 0..self.num_ring_types {
|
||||
for idx in 0..TOWER_HEIGHT {
|
||||
rings.insert((Ring(NonZero::new(ring_type + 1).unwrap()), idx));
|
||||
rings.push((Ring(NonZero::new(ring_type + 1).unwrap()), idx));
|
||||
}
|
||||
}
|
||||
rings.shuffle(rng);
|
||||
|
||||
let rings = rings.into_iter().map(|(i, _)| i).collect::<Vec<_>>();
|
||||
|
||||
|
|
@ -666,9 +670,13 @@ impl GameGenerator {
|
|||
Game::new(towers)
|
||||
}
|
||||
|
||||
pub fn highest_burried_score<const TOWER_HEIGHT: usize>(self) -> Game<TOWER_HEIGHT> {
|
||||
pub fn highest_burried_score<const TOWER_HEIGHT: usize>(
|
||||
self,
|
||||
seed: &str,
|
||||
) -> Game<TOWER_HEIGHT> {
|
||||
let mut rng: Pcg64 = Seeder::from(seed).into_rng();
|
||||
iter::repeat(self)
|
||||
.map(|i| i.generate())
|
||||
.map(|i| i.generate(&mut rng))
|
||||
.take(100)
|
||||
.max_by_key(|i| i.burried_ring_score())
|
||||
.unwrap()
|
||||
|
|
@ -748,11 +756,15 @@ impl<const TOWER_HEIGHT: usize> Default for UndoStack<TOWER_HEIGHT> {
|
|||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
eprint!("seed> ");
|
||||
let mut seed = String::new();
|
||||
stdin().read_line(&mut seed).unwrap();
|
||||
|
||||
let mut g = GameGenerator {
|
||||
num_extra_towers: 2,
|
||||
num_ring_types: 13,
|
||||
}
|
||||
.highest_burried_score::<4>();
|
||||
.highest_burried_score::<4>(&seed);
|
||||
|
||||
if let Some(solution) = g.solve() {
|
||||
let mut g = Game {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue