forked from jana/hanoigame
clippy lints
This commit is contained in:
parent
1e3fca6f51
commit
795de79884
1 changed files with 41 additions and 37 deletions
48
src/main.rs
48
src/main.rs
|
|
@ -1,11 +1,11 @@
|
|||
use palette::{IntoColor, OklabHue, Oklch, Srgb};
|
||||
use std::{
|
||||
collections::{BinaryHeap, HashMap, HashSet, VecDeque},
|
||||
collections::{BinaryHeap, HashMap, HashSet},
|
||||
fmt::Display,
|
||||
hash::{DefaultHasher, Hash, Hasher},
|
||||
io::{self, Write, stdin, stdout},
|
||||
iter,
|
||||
num::{self, NonZero},
|
||||
num::NonZero,
|
||||
ops::ControlFlow,
|
||||
};
|
||||
|
||||
|
|
@ -60,12 +60,6 @@ pub struct Tower<const TOWER_HEIGHT: usize = 4> {
|
|||
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> {
|
||||
const EMPTY: Self = Self {
|
||||
rings: [None; TOWER_HEIGHT],
|
||||
|
|
@ -74,7 +68,7 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
|
|||
pub fn new(rings: &[Ring]) -> Self {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -87,14 +81,12 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
|
|||
let mut max_same_ring_type_score = 0;
|
||||
let mut burried_score = 0;
|
||||
|
||||
for i in self.rings.iter().rev() {
|
||||
if let Some(ring) = i {
|
||||
for ring in self.rings.iter().rev().flatten() {
|
||||
if let Some(existing_ring_type) = ring_type {
|
||||
if existing_ring_type == *ring {
|
||||
same_ring_type_score += 2;
|
||||
} 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);
|
||||
same_ring_type_score = 0;
|
||||
ring_type = Some(*ring);
|
||||
|
|
@ -104,7 +96,6 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
|
|||
ring_type = Some(*ring);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
burried_score
|
||||
}
|
||||
|
|
@ -206,8 +197,7 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
|
|||
let mut ring_type = None;
|
||||
let mut count = 0;
|
||||
|
||||
for i in &self.rings {
|
||||
if let Some(ring) = i {
|
||||
for ring in self.rings.iter().flatten() {
|
||||
if let Some(existing_ring_type) = ring_type {
|
||||
if existing_ring_type == *ring {
|
||||
count += 1;
|
||||
|
|
@ -219,7 +209,6 @@ impl<const TOWER_HEIGHT: usize> Tower<TOWER_HEIGHT> {
|
|||
ring_type = Some(*ring);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some((ring_type, count))
|
||||
}
|
||||
|
|
@ -256,14 +245,12 @@ impl<const TOWER_HEIGHT: usize> Game<TOWER_HEIGHT> {
|
|||
let mut ring_sets = HashMap::new();
|
||||
for t in &towers {
|
||||
t.check();
|
||||
for r in &t.rings {
|
||||
if let Some(r) = r {
|
||||
for r in t.rings.iter().flatten() {
|
||||
*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
|
||||
// if num != TOWER_HEIGHT {
|
||||
// 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()
|
||||
};
|
||||
|
||||
#[derive(PartialEq, PartialOrd)]
|
||||
#[derive(PartialEq)]
|
||||
struct Ordf32(f32);
|
||||
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 {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
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<()> {
|
||||
let mut g = GameGenerator {
|
||||
num_extra_towers: 2,
|
||||
|
|
@ -784,6 +782,12 @@ fn main() -> io::Result<()> {
|
|||
mod tests {
|
||||
use crate::{Game, Move, Ring, Tower};
|
||||
|
||||
macro_rules! tower {
|
||||
($($rings: literal),*) => {
|
||||
Tower::new(&[$(Ring(std::num::NonZero::new($rings).unwrap())),*])
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_moves() {
|
||||
let mut g = Game::<4>::new(vec![tower![1, 2, 1, 2], Tower::EMPTY, Tower::EMPTY]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue