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();
|
let self_copy = self.clone();
|
||||||
for (index, ref mut square) in self.squares.iter_mut().enumerate() {
|
for (index, ref mut square) in self.squares.iter_mut().enumerate() {
|
||||||
if let Square::Marks(ref mut marks) = square {
|
if let Square::Marks(ref mut marks) = square {
|
||||||
self_copy
|
self_copy.row_from_index(index).values().for_each(|value| {
|
||||||
.row_from_index(index)
|
marks.set(value as usize, false);
|
||||||
.filter_map(|sq| match sq {
|
});
|
||||||
Square::Value(x) => Some(x),
|
self_copy.col_from_index(index).values().for_each(|value| {
|
||||||
_ => None,
|
marks.set(value as usize, false);
|
||||||
})
|
});
|
||||||
.for_each(|value| {
|
self_copy.box_from_index(index).values().for_each(|value| {
|
||||||
marks.set(value as usize, false);
|
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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use bitmaps::Bitmap;
|
||||||
|
|
||||||
use crate::square::Square;
|
use crate::square::Square;
|
||||||
|
|
||||||
fn to_row(squares: &[Square; 81], row: usize) -> [Square; 9] {
|
fn to_row(squares: &[Square; 81], row: usize) -> [Square; 9] {
|
||||||
|
@ -71,6 +73,22 @@ impl SquareIter {
|
||||||
curr: 0,
|
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 {
|
impl Iterator for SquareIter {
|
||||||
|
|
|
@ -23,30 +23,21 @@ where
|
||||||
|
|
||||||
pub fn validate_board(board: &Board) -> BoardState {
|
pub fn validate_board(board: &Board) -> BoardState {
|
||||||
for r in 0..9 {
|
for r in 0..9 {
|
||||||
if !are_unique(board.row(r).filter_map(|sq| match sq {
|
if !are_unique(board.row(r).values()) {
|
||||||
Square::Value(x) => Some(x),
|
|
||||||
_ => None,
|
|
||||||
})) {
|
|
||||||
// TODO: Add the index.
|
// TODO: Add the index.
|
||||||
return BoardState::Broken(0);
|
return BoardState::Broken(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for c in 0..9 {
|
for c in 0..9 {
|
||||||
if !are_unique(board.col(c).filter_map(|sq| match sq {
|
if !are_unique(board.col(c).values()) {
|
||||||
Square::Value(x) => Some(x),
|
|
||||||
_ => None,
|
|
||||||
})) {
|
|
||||||
// TODO: Add the index.
|
// TODO: Add the index.
|
||||||
return BoardState::Broken(0);
|
return BoardState::Broken(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for b in 0..9 {
|
for b in 0..9 {
|
||||||
if !are_unique(board.boxn(b).filter_map(|sq| match sq {
|
if !are_unique(board.boxn(b).values()) {
|
||||||
Square::Value(x) => Some(x),
|
|
||||||
_ => None,
|
|
||||||
})) {
|
|
||||||
// TODO: Add the index.
|
// TODO: Add the index.
|
||||||
return BoardState::Broken(0);
|
return BoardState::Broken(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue