Add a values() helper function to the iterator
This commit is contained in:
parent
64189981f2
commit
a86542d297
36
src/board.rs
36
src/board.rs
|
@ -28,33 +28,15 @@ impl Board {
|
|||
let self_copy = self.clone();
|
||||
for (index, ref mut square) in self.squares.iter_mut().enumerate() {
|
||||
if let Square::Marks(ref mut marks) = square {
|
||||
self_copy
|
||||
.row_from_index(index)
|
||||
.filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})
|
||||
.for_each(|value| {
|
||||
marks.set(value as usize, false);
|
||||
});
|
||||
self_copy
|
||||
.col_from_index(index)
|
||||
.filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})
|
||||
.for_each(|value| {
|
||||
marks.set(value as usize, false);
|
||||
});
|
||||
self_copy
|
||||
.box_from_index(index)
|
||||
.filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})
|
||||
.for_each(|value| {
|
||||
marks.set(value as usize, false);
|
||||
});
|
||||
self_copy.row_from_index(index).values().for_each(|value| {
|
||||
marks.set(value as usize, false);
|
||||
});
|
||||
self_copy.col_from_index(index).values().for_each(|value| {
|
||||
marks.set(value as usize, false);
|
||||
});
|
||||
self_copy.box_from_index(index).values().for_each(|value| {
|
||||
marks.set(value as usize, false);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use bitmaps::Bitmap;
|
||||
|
||||
use crate::square::Square;
|
||||
|
||||
fn to_row(squares: &[Square; 81], row: usize) -> [Square; 9] {
|
||||
|
@ -71,6 +73,22 @@ impl SquareIter {
|
|||
curr: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn values(self) -> std::iter::FilterMap<SquareIter, impl FnMut(Square) -> Option<u8>> {
|
||||
self.filter_map(|sq| match sq {
|
||||
Square::Value(v) => Some(v),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn marks(
|
||||
self,
|
||||
) -> std::iter::FilterMap<SquareIter, impl FnMut(Square) -> Option<Bitmap<10>>> {
|
||||
self.filter_map(|sq| match sq {
|
||||
Square::Marks(m) => Some(m),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for SquareIter {
|
||||
|
|
|
@ -23,30 +23,21 @@ where
|
|||
|
||||
pub fn validate_board(board: &Board) -> BoardState {
|
||||
for r in 0..9 {
|
||||
if !are_unique(board.row(r).filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})) {
|
||||
if !are_unique(board.row(r).values()) {
|
||||
// TODO: Add the index.
|
||||
return BoardState::Broken(0);
|
||||
}
|
||||
}
|
||||
|
||||
for c in 0..9 {
|
||||
if !are_unique(board.col(c).filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})) {
|
||||
if !are_unique(board.col(c).values()) {
|
||||
// TODO: Add the index.
|
||||
return BoardState::Broken(0);
|
||||
}
|
||||
}
|
||||
|
||||
for b in 0..9 {
|
||||
if !are_unique(board.boxn(b).filter_map(|sq| match sq {
|
||||
Square::Value(x) => Some(x),
|
||||
_ => None,
|
||||
})) {
|
||||
if !are_unique(board.boxn(b).values()) {
|
||||
// TODO: Add the index.
|
||||
return BoardState::Broken(0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue