Make the is_contiguous check more robust and readable
This commit is contained in:
@@ -29,31 +29,29 @@ impl Board {
|
|||||||
diff
|
diff
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Determines whether the given positions are contiguous.
|
||||||
|
///
|
||||||
|
/// Contiguous means that the positions are adjacent in a straight line, either
|
||||||
|
/// horizontally or vertically.
|
||||||
pub fn is_contiguous(positions: &[Position2d]) -> Option<bool> {
|
pub fn is_contiguous(positions: &[Position2d]) -> Option<bool> {
|
||||||
let mut it = positions.iter();
|
let mut it = positions.iter();
|
||||||
let first = *it.next()?;
|
let first = *it.next()?;
|
||||||
let mut second = *it.next()?;
|
let mut second = *it.next()?;
|
||||||
if first.x == second.x {
|
|
||||||
// Vertical
|
let orientation = match (second.x.checked_sub(first.x), second.y.checked_sub(first.y)) {
|
||||||
|
(Some(0), Some(1)) => (0, 1),
|
||||||
|
(Some(1), Some(0)) => (1, 0),
|
||||||
|
(_, _) => return Some(false),
|
||||||
|
};
|
||||||
|
|
||||||
for &pos in it {
|
for &pos in it {
|
||||||
if pos.x != first.x || pos.y != second.y + 1 {
|
if pos.x != second.x + orientation.0 || pos.y != second.y + orientation.1 {
|
||||||
return Some(false);
|
return Some(false);
|
||||||
}
|
}
|
||||||
second = pos;
|
second = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(true)
|
Some(true)
|
||||||
} else if first.y == second.y {
|
|
||||||
// Horizontal
|
|
||||||
for &pos in it {
|
|
||||||
if pos.y != first.y || pos.y != second.y + 1 {
|
|
||||||
return Some(false);
|
|
||||||
}
|
|
||||||
second = pos;
|
|
||||||
}
|
|
||||||
Some(true)
|
|
||||||
} else {
|
|
||||||
Some(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,10 +78,22 @@ mod tests {
|
|||||||
fn test_is_contiguous() {
|
fn test_is_contiguous() {
|
||||||
assert_eq!(Board::is_contiguous(&[]), None);
|
assert_eq!(Board::is_contiguous(&[]), None);
|
||||||
assert_eq!(Board::is_contiguous(&positions(&[(0, 0)])), None);
|
assert_eq!(Board::is_contiguous(&positions(&[(0, 0)])), None);
|
||||||
|
assert_eq!(
|
||||||
|
Board::is_contiguous(&positions(&[(0, 0), (0, 2)])),
|
||||||
|
Some(false)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Board::is_contiguous(&positions(&[(0, 0), (2, 0)])),
|
||||||
|
Some(false)
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Board::is_contiguous(&positions(&[(0, 0), (0, 1), (0, 2)])),
|
Board::is_contiguous(&positions(&[(0, 0), (0, 1), (0, 2)])),
|
||||||
Some(true)
|
Some(true)
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Board::is_contiguous(&positions(&[(1, 0), (2, 0), (3, 0), (4, 0)])),
|
||||||
|
Some(true)
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Board::is_contiguous(&positions(&[(0, 0), (0, 1), (1, 3)])),
|
Board::is_contiguous(&positions(&[(0, 0), (0, 1), (1, 3)])),
|
||||||
Some(false)
|
Some(false)
|
||||||
|
Reference in New Issue
Block a user