From 37d80eda28291071a9c42cb570716bbe9502ffa5 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Wed, 16 Oct 2024 15:59:39 -0500 Subject: [PATCH] Use ranges properly --- src/board.rs | 28 +++++++++++++++++----------- src/display/cell.rs | 6 ++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/board.rs b/src/board.rs index 3355efe..f4ea623 100644 --- a/src/board.rs +++ b/src/board.rs @@ -66,7 +66,7 @@ impl Board { pub fn update_square(&mut self, point: &Point, value: CellValue) { let row_bounds = point.row_bounds(); let col_bounds = point.col_bounds(); - for c in self.cells.slice_mut(s![row_bounds.0..row_bounds.1, col_bounds.0..col_bounds.1]) { + for c in self.cells.slice_mut(s![row_bounds, col_bounds]) { c.remove_candidate(value); } } @@ -82,6 +82,12 @@ impl Board { pub fn col_range(&self) -> std::ops::Range { 0..REGION_SIZE } + + pub fn square_iter(&self, point: &Point) -> ndarray::ArrayBase, Dim<[usize; 2]>> { + let row_bounds = point.row_bounds(); + let col_bounds = point.col_bounds(); + self.cells.slice(s![row_bounds, col_bounds]) + } pub fn row_iter(&self, row: usize) -> ndarray::ArrayBase, Dim<[usize; 1]>> { self.cells.slice(s![row, ..]) @@ -109,17 +115,17 @@ impl Point { [self.row, self.col] } - fn _bounds(val: usize) -> (usize, usize) { + fn _bounds(val: usize) -> std::ops::Range { let mut start = val / (SUB_REGION_SIZE as usize); start *= SUB_REGION_SIZE; - (start, start + SUB_REGION_SIZE) + std::ops::Range {start, end: start + SUB_REGION_SIZE} } - fn row_bounds(&self) -> (usize, usize) { + fn row_bounds(&self) -> std::ops::Range { Self::_bounds(self.row) } - fn col_bounds(&self) -> (usize, usize) { + fn col_bounds(&self) -> std::ops::Range { Self::_bounds(self.col) } } @@ -133,14 +139,14 @@ mod test { #[test] fn bounds_are_correct() { let origin = Point{ row: 0, col: 0}; - assert_eq!(origin.row_bounds(), (0, 3)); - assert_eq!(origin.col_bounds(), (0, 3)); + assert_eq!(origin.row_bounds(), 0..3); + assert_eq!(origin.col_bounds(), 0..3); let one = Point{ row: 1, col: 1}; - assert_eq!(one.row_bounds(), (0, 3)); - assert_eq!(one.col_bounds(), (0, 3)); + assert_eq!(one.row_bounds(), 0..3); + assert_eq!(one.col_bounds(), 0..3); let five = Point{ row: 5, col: 7}; - assert_eq!(five.row_bounds(), (3, 6)); - assert_eq!(five.col_bounds(), (6, 9)); + assert_eq!(five.row_bounds(), 3..6); + assert_eq!(five.col_bounds(), 6..9); } #[test] diff --git a/src/display/cell.rs b/src/display/cell.rs index 13ccc95..94fec90 100644 --- a/src/display/cell.rs +++ b/src/display/cell.rs @@ -17,9 +17,15 @@ pub fn widget(cell: &Cell) -> iced::Element { button(text(display)) .style(|theme: &Theme, status| { + // Get the current theme's palette let palette = theme.palette(); + + // Create a default border, since we need to set off the cells + // TODO: Figure out how to get this out of the theme? let mut border = Border::default().color(LIGHT_GRAY); border.width = 1.0; + + // Return a style based on the status match status { button::Status::Active => { button::Style{