change position x,y type + method is_valid and test for move robot
This commit is contained in:
parent
9de4c1ff6b
commit
62433b0fd2
31
src/main.rs
31
src/main.rs
@ -32,8 +32,19 @@ impl World {
|
||||
|
||||
/// Struct to store robot position.
|
||||
struct Position {
|
||||
x: u32,
|
||||
y: u32,
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
impl Position {
|
||||
/// Check if position is in the map.
|
||||
fn is_valid(self, w: World) -> Result<(), String> {
|
||||
if (self.x, self.y) > (0, 0) && (self.x, self.y) < (w.x as i32, w.y as i32) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(String::from("Invalid position."))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Robot *aka droid* is represented here.
|
||||
@ -257,6 +268,22 @@ mod tests {
|
||||
assert_eq!(w.map, vec!['↑', '.', '.', '.']);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_move_robot() {
|
||||
let mut r: Robot = Robot {
|
||||
id: 0,
|
||||
o: Orientation::N,
|
||||
p: Position { x: 1, y: 1 },
|
||||
q: Queue::new(),
|
||||
};
|
||||
r.move_forward();
|
||||
assert_eq!(0, r.p.y);
|
||||
r.move_right();
|
||||
assert_eq!(2, r.p.x);
|
||||
r.move_left();
|
||||
assert_eq!(1, r.p.x);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_position() {
|
||||
let mut w: World = World {
|
||||
|
Loading…
Reference in New Issue
Block a user