forked from jana/hanoigame
Compare commits
12 commits
push-lrpsr
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fc85b3d82 | |||
| 37b01f2102 | |||
| 82fbdec1c7 | |||
| fc5872a5af | |||
| 727a738fd4 | |||
| 50f7dd20f6 | |||
| 3e15a32a48 | |||
| cf5b93cb0d | |||
| 1a41781aea | |||
| 48e963e723 | |||
| db22d1912e | |||
| ea3c02262f |
3 changed files with 214 additions and 13 deletions
128
Cargo.lock
generated
128
Cargo.lock
generated
|
|
@ -23,19 +23,46 @@ version = "1.2.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "64fa3c856b712db6612c019f14756e64e4bcea13337a6b33b696333a9eaa2d06"
|
checksum = "64fa3c856b712db6612c019f14756e64e4bcea13337a6b33b696333a9eaa2d06"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fast-srgb8"
|
name = "fast-srgb8"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dd2e7510819d6fbf51a5545c8f922716ecfb14df168a3242f7d33e0239efe6a1"
|
checksum = "dd2e7510819d6fbf51a5545c8f922716ecfb14df168a3242f7d33e0239efe6a1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.3.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"r-efi",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hanoigame"
|
name = "hanoigame"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"palette",
|
"palette",
|
||||||
|
"rand 0.9.2",
|
||||||
|
"rand_pcg",
|
||||||
|
"rand_seeder",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.175"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-traits"
|
name = "num-traits"
|
||||||
version = "0.2.19"
|
version = "0.2.19"
|
||||||
|
|
@ -86,7 +113,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
|
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"phf_shared",
|
"phf_shared",
|
||||||
"rand",
|
"rand 0.8.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -111,6 +138,15 @@ dependencies = [
|
||||||
"siphasher",
|
"siphasher",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.101"
|
version = "1.0.101"
|
||||||
|
|
@ -129,13 +165,39 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "r-efi"
|
||||||
|
version = "5.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rand"
|
name = "rand"
|
||||||
version = "0.8.5"
|
version = "0.8.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rand_core",
|
"rand_core 0.6.4",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.9.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
|
||||||
|
dependencies = [
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core 0.9.3",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core 0.9.3",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -144,6 +206,33 @@ version = "0.6.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.9.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_pcg"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b48ac3f7ffaab7fac4d2376632268aa5f89abdb55f7ebf8f4d11fffccb2320f7"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core 0.9.3",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_seeder"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "502927fdfc3c9645d53e0c95bb2d53783b5a15bfeaeeb96f7703c21fbb76841e"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core 0.9.3",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "siphasher"
|
name = "siphasher"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
|
|
@ -166,3 +255,38 @@ name = "unicode-ident"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.14.3+wasi-0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6a51ae83037bdd272a9e28ce236db8c07016dd0d50c27038b3f407533c030c95"
|
||||||
|
dependencies = [
|
||||||
|
"wit-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wit-bindgen"
|
||||||
|
version = "0.45.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "052283831dbae3d879dc7f51f3d92703a316ca49f91540417d38591826127814"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.8.26"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.8.26"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,8 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
palette = "0.7"
|
palette = "0.7"
|
||||||
|
rand = "0.9.2"
|
||||||
|
rand_pcg = "0.9.0"
|
||||||
|
rand_seeder = "0.4.0"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|
|
||||||
96
src/main.rs
96
src/main.rs
|
|
@ -1,4 +1,7 @@
|
||||||
use palette::{IntoColor, OklabHue, Oklch, Srgb};
|
use palette::{IntoColor, OklabHue, Oklch, Srgb};
|
||||||
|
use rand::seq::SliceRandom as _;
|
||||||
|
use rand_pcg::Pcg64;
|
||||||
|
use rand_seeder::Seeder;
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BinaryHeap, HashMap, HashSet},
|
collections::{BinaryHeap, HashMap, HashSet},
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
|
|
@ -27,7 +30,7 @@ impl Display for Ring {
|
||||||
|
|
||||||
let num = self.0.get() - 1;
|
let num = self.0.get() - 1;
|
||||||
for _ in 0..num {
|
for _ in 0..num {
|
||||||
initial_hue += 80.0;
|
initial_hue += 65.0;
|
||||||
initial_chroma += 0.8;
|
initial_chroma += 0.8;
|
||||||
if initial_chroma > 1.0 {
|
if initial_chroma > 1.0 {
|
||||||
initial_chroma -= 1.0;
|
initial_chroma -= 1.0;
|
||||||
|
|
@ -75,6 +78,12 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
|
||||||
tower
|
tower
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn map(self, f: impl FnMut(Option<Ring>) -> Option<Ring>) -> Self {
|
||||||
|
Self {
|
||||||
|
rings: self.rings.map(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn burried_score(&self) -> usize {
|
fn burried_score(&self) -> usize {
|
||||||
let mut ring_type = None;
|
let mut ring_type = None;
|
||||||
let mut same_ring_type_score = 0;
|
let mut same_ring_type_score = 0;
|
||||||
|
|
@ -277,6 +286,13 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let rings = self.towers[from_tower].rings;
|
||||||
|
if rings[1..].iter().all(|i| i == &rings[0])
|
||||||
|
&& self.towers[to_tower].rings[0].is_none()
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let m = Move::new(from_tower, to_tower);
|
let m = Move::new(from_tower, to_tower);
|
||||||
if self.try_make_move(m, true).is_ok() {
|
if self.try_make_move(m, true).is_ok() {
|
||||||
res.push(m);
|
res.push(m);
|
||||||
|
|
@ -309,10 +325,59 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let hash_state = |s: &[Tower<TOWER_HEIGHT>]| {
|
fn tower_solved<const TOWER_HEIGHT: usize>(tower: &Tower<TOWER_HEIGHT>) -> bool {
|
||||||
|
tower
|
||||||
|
.rings
|
||||||
|
.iter()
|
||||||
|
.all(|x| x.is_some() && x == &tower.rings[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
let hash_state = |towers: &[Tower<TOWER_HEIGHT>]| {
|
||||||
let mut hasher = DefaultHasher::default();
|
let mut hasher = DefaultHasher::default();
|
||||||
|
|
||||||
for x in s.iter().filter(|i| i.height() != 0).enumerate() {
|
let num_finished_towers = towers.iter().filter(|i| tower_solved(*i)).count();
|
||||||
|
|
||||||
|
num_finished_towers.hash(&mut hasher);
|
||||||
|
let mut filtered_towers = towers
|
||||||
|
.iter()
|
||||||
|
.filter(|i| !tower_solved(i) && i.height() != 0)
|
||||||
|
.enumerate()
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
filtered_towers.sort_by_cached_key(|(index, tower)| {
|
||||||
|
let mut rings = [0; TOWER_HEIGHT];
|
||||||
|
|
||||||
|
let mut count = 0;
|
||||||
|
for (idx, ring) in tower.rings.iter().copied().enumerate() {
|
||||||
|
let Some(ring) = ring else { break };
|
||||||
|
|
||||||
|
if rings[idx] == 0 {
|
||||||
|
count += 1;
|
||||||
|
for (j, ring2) in tower.rings[idx..].iter().copied().enumerate() {
|
||||||
|
if ring2 == Some(ring) {
|
||||||
|
rings[j] = count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(rings, *index)
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut color_assignment = [None::<Ring>; 100];
|
||||||
|
let mut num_seen = 0;
|
||||||
|
let relabelled_towers = filtered_towers.iter().map(|(_, x)| x).map(|tower| {
|
||||||
|
tower.map(|ring| {
|
||||||
|
ring.map(|ring| {
|
||||||
|
*color_assignment[ring.0.get()].get_or_insert_with(|| {
|
||||||
|
num_seen += 1;
|
||||||
|
let ring = Ring(NonZero::new(num_seen).unwrap());
|
||||||
|
ring
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
for x in relabelled_towers.into_iter().enumerate() {
|
||||||
x.hash(&mut hasher);
|
x.hash(&mut hasher);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -465,8 +530,8 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ask_tower(&self, msg: &str) -> io::Result<Input> {
|
fn ask_tower(&self, msg: &str) -> io::Result<Input> {
|
||||||
let mut res = String::new();
|
|
||||||
loop {
|
loop {
|
||||||
|
let mut res = String::new();
|
||||||
print!("{msg} (enter a tower number, exit, clear or undo):");
|
print!("{msg} (enter a tower number, exit, clear or undo):");
|
||||||
stdout().flush()?;
|
stdout().flush()?;
|
||||||
stdin().read_line(&mut res)?;
|
stdin().read_line(&mut res)?;
|
||||||
|
|
@ -643,15 +708,16 @@ pub struct GameGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 towers = Vec::new();
|
||||||
let mut rings = HashSet::new();
|
let mut rings = Vec::new();
|
||||||
|
|
||||||
for ring_type in 0..self.num_ring_types {
|
for ring_type in 0..self.num_ring_types {
|
||||||
for idx in 0..TOWER_HEIGHT {
|
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<_>>();
|
let rings = rings.into_iter().map(|(i, _)| i).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
|
@ -666,9 +732,13 @@ impl GameGenerator {
|
||||||
Game::new(towers)
|
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)
|
iter::repeat(self)
|
||||||
.map(|i| i.generate())
|
.map(|i| i.generate(&mut rng))
|
||||||
.take(100)
|
.take(100)
|
||||||
.max_by_key(|i| i.burried_ring_score())
|
.max_by_key(|i| i.burried_ring_score())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
@ -748,11 +818,15 @@ impl<const TOWER_HEIGHT: usize> Default for UndoStack<TOWER_HEIGHT> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
|
eprint!("seed> ");
|
||||||
|
let mut seed = String::new();
|
||||||
|
stdin().read_line(&mut seed).unwrap();
|
||||||
|
|
||||||
let mut g = GameGenerator {
|
let mut g = GameGenerator {
|
||||||
num_extra_towers: 2,
|
num_extra_towers: 2,
|
||||||
num_ring_types: 13,
|
num_ring_types: 16,
|
||||||
}
|
}
|
||||||
.highest_burried_score::<4>();
|
.highest_burried_score::<4>(&seed);
|
||||||
|
|
||||||
if let Some(solution) = g.solve() {
|
if let Some(solution) = g.solve() {
|
||||||
let mut g = Game {
|
let mut g = Game {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue