-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMover.java
More file actions
68 lines (62 loc) · 1.69 KB
/
Mover.java
File metadata and controls
68 lines (62 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import info.gridworld.grid.*;
import info.gridworld.actor.*;
import java.awt.Color;
// a Mover is an Actor that can move up and to the right
public class Mover extends Actor
{
public Mover()
{
setColor(Color.RED);
}
/**
* up and right are copies of the move method from Bug with the direction changed
*/
public void up()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(Location.NORTH); // changed this line
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public void right()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(Location.EAST); // and this one
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public void down()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(Location.SOUTH); // changed this line
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
public void left()
{
Grid<Actor> gr = getGrid();
if (gr == null)
return;
Location loc = getLocation();
Location next = loc.getAdjacentLocation(Location.WEST); // and this one
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}