Merge pull request 'clippy lints' (#2) from computerdruid/hanoigame:push-lrpsrlkrtkor into main

Reviewed-on: jana/hanoigame#2
This commit is contained in:
jana 2025-09-03 19:40:42 +02:00
commit 1a41781aea

View file

@ -1,11 +1,11 @@
use palette::{IntoColor, OklabHue, Oklch, Srgb}; use palette::{IntoColor, OklabHue, Oklch, Srgb};
use std::{ use std::{
collections::{BinaryHeap, HashMap, HashSet, VecDeque}, collections::{BinaryHeap, HashMap, HashSet},
fmt::Display, fmt::Display,
hash::{DefaultHasher, Hash, Hasher}, hash::{DefaultHasher, Hash, Hasher},
io::{self, Write, stdin, stdout}, io::{self, Write, stdin, stdout},
iter, iter,
num::{self, NonZero}, num::NonZero,
ops::ControlFlow, ops::ControlFlow,
}; };
@ -60,12 +60,6 @@ pub struct Tower<const TOWER_HEIGHT: usize = 4> {
pub rings: [Option<Ring>; TOWER_HEIGHT], pub rings: [Option<Ring>; TOWER_HEIGHT],
} }
macro_rules! tower {
($($rings: literal),*) => {
Tower::new(&[$(Ring(std::num::NonZero::new($rings).unwrap())),*])
};
}
impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> { impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
const EMPTY: Self = Self { const EMPTY: Self = Self {
rings: [None; TOWER_HEIGHT], rings: [None; TOWER_HEIGHT],
@ -74,7 +68,7 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
pub fn new(rings: &[Ring]) -> Self { pub fn new(rings: &[Ring]) -> Self {
let mut tower = Self::EMPTY; let mut tower = Self::EMPTY;
for (src, dst) in rings.into_iter().zip(&mut tower.rings) { for (src, dst) in rings.iter().zip(&mut tower.rings) {
*dst = Some(*src); *dst = Some(*src);
} }
@ -87,14 +81,12 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
let mut max_same_ring_type_score = 0; let mut max_same_ring_type_score = 0;
let mut burried_score = 0; let mut burried_score = 0;
for i in self.rings.iter().rev() { for ring in self.rings.iter().rev().flatten() {
if let Some(ring) = i {
if let Some(existing_ring_type) = ring_type { if let Some(existing_ring_type) = ring_type {
if existing_ring_type == *ring { if existing_ring_type == *ring {
same_ring_type_score += 2; same_ring_type_score += 2;
} else { } else {
max_same_ring_type_score = max_same_ring_type_score = max_same_ring_type_score.max(same_ring_type_score);
max_same_ring_type_score.max(same_ring_type_score);
burried_score += 2usize.pow(max_same_ring_type_score); burried_score += 2usize.pow(max_same_ring_type_score);
same_ring_type_score = 0; same_ring_type_score = 0;
ring_type = Some(*ring); ring_type = Some(*ring);
@ -104,7 +96,6 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
ring_type = Some(*ring); ring_type = Some(*ring);
} }
} }
}
burried_score burried_score
} }
@ -206,8 +197,7 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
let mut ring_type = None; let mut ring_type = None;
let mut count = 0; let mut count = 0;
for i in &self.rings { for ring in self.rings.iter().flatten() {
if let Some(ring) = i {
if let Some(existing_ring_type) = ring_type { if let Some(existing_ring_type) = ring_type {
if existing_ring_type == *ring { if existing_ring_type == *ring {
count += 1; count += 1;
@ -219,7 +209,6 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
ring_type = Some(*ring); ring_type = Some(*ring);
} }
} }
}
Some((ring_type, count)) Some((ring_type, count))
} }
@ -256,14 +245,12 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
let mut ring_sets = HashMap::new(); let mut ring_sets = HashMap::new();
for t in &towers { for t in &towers {
t.check(); t.check();
for r in &t.rings { for r in t.rings.iter().flatten() {
if let Some(r) = r {
*ring_sets.entry(*r).or_insert(0) += 1; *ring_sets.entry(*r).or_insert(0) += 1;
} }
} }
}
for (ring, _num) in &ring_sets { for ring in ring_sets.keys() {
// ok as long as the goal isn't sets of 4 but single-ringtype towers // ok as long as the goal isn't sets of 4 but single-ringtype towers
// if num != TOWER_HEIGHT { // if num != TOWER_HEIGHT {
// panic!("incomplete ring set: expected {TOWER_HEIGHT} of {ring} but found {num}"); // panic!("incomplete ring set: expected {TOWER_HEIGHT} of {ring} but found {num}");
@ -332,9 +319,14 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
hasher.finish() hasher.finish()
}; };
#[derive(PartialEq, PartialOrd)] #[derive(PartialEq)]
struct Ordf32(f32); struct Ordf32(f32);
impl Eq for Ordf32 {} impl Eq for Ordf32 {}
impl PartialOrd for Ordf32 {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Ordf32 { impl Ord for Ordf32 {
fn cmp(&self, other: &Self) -> std::cmp::Ordering { fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.total_cmp(&other.0) self.0.total_cmp(&other.0)
@ -749,6 +741,12 @@ impl<const TOWER_HEIGHT: usize> UndoStack<TOWER_HEIGHT> {
} }
} }
impl<const TOWER_HEIGHT: usize> Default for UndoStack<TOWER_HEIGHT> {
fn default() -> Self {
Self::new()
}
}
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let mut g = GameGenerator { let mut g = GameGenerator {
num_extra_towers: 2, num_extra_towers: 2,
@ -784,6 +782,12 @@ fn main() -> io::Result<()> {
mod tests { mod tests {
use crate::{Game, Move, Ring, Tower}; use crate::{Game, Move, Ring, Tower};
macro_rules! tower {
($($rings: literal),*) => {
Tower::new(&[$(Ring(std::num::NonZero::new($rings).unwrap())),*])
};
}
#[test] #[test]
fn test_moves() { fn test_moves() {
let mut g = Game::<4>::new(vec![tower![1, 2, 1, 2], Tower::EMPTY, Tower::EMPTY]); let mut g = Game::<4>::new(vec![tower![1, 2, 1, 2], Tower::EMPTY, Tower::EMPTY]);