|
|
|
@ -119,9 +119,11 @@ impl Piece {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn moves_at(&self, from: &BoardIndex) -> Vec<BoardIndex> {
|
|
|
|
fn moves_at(&self, from: &BoardIndex) -> HashSet<BoardIndex> {
|
|
|
|
self.moves().map(|(x, y)| {
|
|
|
|
self.moves().filter_map(|(x, y)| {
|
|
|
|
*from + (*x, *y)
|
|
|
|
let bi = *from + (*x, *y);
|
|
|
|
|
|
|
|
// Check if this goes out of bounds, if so exclude from the list of possible moves
|
|
|
|
|
|
|
|
(bi.x <= 7 && bi.y <= 3).then_some(bi)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -287,8 +289,8 @@ impl std::ops::Add<(isize, isize)> for BoardIndex {
|
|
|
|
|
|
|
|
|
|
|
|
fn add(self, (a, b): (isize, isize)) -> Self {
|
|
|
|
fn add(self, (a, b): (isize, isize)) -> Self {
|
|
|
|
BoardIndex {
|
|
|
|
BoardIndex {
|
|
|
|
x: self.x.saturating_add_signed(a).max(7),
|
|
|
|
x: self.x.saturating_add_signed(a),
|
|
|
|
y: self.y.saturating_add_signed(b).max(3),
|
|
|
|
y: self.y.saturating_add_signed(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -298,8 +300,8 @@ impl std::ops::Add<(usize, usize)> for BoardIndex {
|
|
|
|
|
|
|
|
|
|
|
|
fn add(self, (a, b): (usize, usize)) -> Self {
|
|
|
|
fn add(self, (a, b): (usize, usize)) -> Self {
|
|
|
|
BoardIndex {
|
|
|
|
BoardIndex {
|
|
|
|
x: self.x.saturating_add(a).max(7),
|
|
|
|
x: self.x.saturating_add(a),
|
|
|
|
y: self.y.saturating_add(b).max(3),
|
|
|
|
y: self.y.saturating_add(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -538,7 +540,7 @@ impl Board {
|
|
|
|
.moves_at(&from)
|
|
|
|
.moves_at(&from)
|
|
|
|
.iter()
|
|
|
|
.iter()
|
|
|
|
// Find if the given `to` move is one of those
|
|
|
|
// Find if the given `to` move is one of those
|
|
|
|
.find(|BoardIndex { x, y }| to == from + (*x, *y))
|
|
|
|
.find(|idx| to == **idx)
|
|
|
|
// Determine if this is valid/legal in this situation
|
|
|
|
// Determine if this is valid/legal in this situation
|
|
|
|
.and_then(|_| {
|
|
|
|
.and_then(|_| {
|
|
|
|
let dest_at = self.at(to);
|
|
|
|
let dest_at = self.at(to);
|
|
|
|
@ -798,6 +800,16 @@ mod test {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn test_piece_moves_at() {
|
|
|
|
|
|
|
|
let given = Piece::Drone.moves_at(&(4, 1).into());
|
|
|
|
|
|
|
|
let expected: HashSet<BoardIndex> = HashSet::from([
|
|
|
|
|
|
|
|
(4, 0).into(), (2, 1).into(), (3, 1).into(), (5, 1).into(), (6, 1).into(), (4, 2).into(), (4, 3).into(),
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(expected, given, "Drone moves at");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for Board {
|
|
|
|
impl std::fmt::Display for Board {
|
|
|
|
|