Skip to content

Commit 4da5ce3

Browse files
committed
round the torpedo coords properly, add a fuzzy test
1 parent 5238435 commit 4da5ce3

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

84_Super_Star_Trek/rust/src/commands.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ fn find_torpedo_path(start_sector: Pos, course: f32) -> Vec<Pos> {
538538
if nx < 0.0 || ny < 0.0 || nx >= 8.0 || ny >= 8.0 {
539539
break;
540540
}
541-
let step = Pos(nx as u8, ny as u8);
541+
let step = Pos(nx.round() as u8, ny.round() as u8);
542542
if step != last_sector {
543543
last_sector = step;
544544
path.push(last_sector);
@@ -554,7 +554,7 @@ mod tests {
554554

555555
#[test]
556556
fn test_find_torpedo_path() {
557-
let path = find_torpedo_path(Pos(0, 0), 7.5);
557+
let path = find_torpedo_path(Pos(0, 0), 7.45);
558558
assert_eq!(
559559
*path.last().unwrap(),
560560
Pos(7, 3),
@@ -563,5 +563,22 @@ mod tests {
563563
path,
564564
vec!(Pos(1, 0), Pos(2, 1), Pos(3, 1), Pos(4, 2), Pos(5, 2), Pos(6, 3), Pos(7, 3))
565565
);
566+
567+
assert_eq!(
568+
find_torpedo_path(Pos(4, 7), 4.5),
569+
vec!(Pos(4, 6), Pos(3, 5), Pos(3, 4), Pos(2, 3), Pos(2, 2), Pos(1, 1), Pos(1, 0))
570+
);
571+
572+
let enterprise = Pos(3, 3);
573+
for y in 0..=7 {
574+
for x in 0..=7 {
575+
let pos = Pos(y, x);
576+
if pos != enterprise {
577+
let course = enterprise.direction(pos);
578+
let path = find_torpedo_path(enterprise, course);
579+
assert!(path.contains(&pos));
580+
};
581+
};
582+
};
566583
}
567584
}

0 commit comments

Comments
 (0)