//You should add code to the NavigateMaze command to make the robot go from the
ID: 3794904 • Letter: #
Question
//You should add code to the NavigateMaze command to make the robot go from the starting point to the end point!
1 import becker.robots.*;
2
3 class MazeBot extends RobotSE
4 {
5 public MazeBot(City theCity, int str, int ave, Direction dir, int numThings)
6 { super(theCity, str, ave, dir, numThings); }
7
8 public void printTotalNumberOfSpacesMoved()
9 {
10
11 }
12
13 // isAtEndSpot is what's called a 'helper method' - it exists just to make
14 // another command (in this case, NavigateMaze) easier to understand.
15 // It does this by replacing some code that otherwise would be in NavigateMaze
16 // with it's name, and doing that work here, instead.
17 // "private" means that only the MazeBot is allowed to call it.
18 private boolean isAtEndSpot()
19 { // If we're at avenue 9, AND we're at street 10, then we must
20 // be at the intersection (9,10), which is the "way out" of the maze
21 // You can easily change this to tell the robot to stop at a different
22 // intersection.
23 return (this.getAvenue() == 9 && this.getStreet() == 10);
24 }
25
26 public void NavigateMaze()
27 { // While your robot hasn't reached the 'ending spot'
28 // in the maze, take another step.
29 while( !this.isAtEndSpot() )
30 { // What will you have the robot do at each step?
31
32 }
33 }
34
35 }
36
37 public class Maze extends Object
38 {
39 private static void MakeMaze(City theCity)
40 {
41 for(int i = 1; i < 11; i++)
42 { // north wall
43 new Wall(theCity, 1, i, Direction.NORTH);
44 // Second to north wall
45 if (i <= 9)
46 new Wall(theCity, 1, i, Direction.SOUTH);
47 // Third to north wall
48 if (i >= 4)
49 new Wall(theCity, 4, i, Direction.SOUTH);
50 // south wall
51 if (i != 9) // (9, 10, SOUTH), is where the 'exit' is
52 new Wall(theCity, 10, i, Direction.SOUTH);
53 // west wall
54 if( i != 1) // (1,1, WEST) is where the 'entrance' is
55 new Wall(theCity, i, 1, Direction.WEST);
56 // second to west-most wall
57 if (i >= 3 && i < 6)
58 new Wall(theCity, i, 6, Direction.WEST);
59 // east wall
60 new Wall(theCity, i, 10, Direction.EAST);
61 }
62
63 // cul-de-sac
64 new Wall(theCity, 3, 10, Direction.WEST);
65 new Wall(theCity, 3, 10, Direction.SOUTH);
66
67 new Wall(theCity, 2, 8, Direction.WEST);
68 new Wall(theCity, 2, 8, Direction.SOUTH);
69
70 new Wall(theCity, 10, 8, Direction.NORTH);
71 new Wall(theCity, 10, 9, Direction.EAST);
72 new Wall(theCity, 10, 9, Direction.NORTH);
73 MakeSpiral(theCity, 8, 9, 3);
74 new Wall(theCity, 8, 10, Direction.SOUTH);
75
76 MakeSpiral(theCity, 10, 5, 4);
77 }
78
79 public static void MakeSpiral(City theCity, int st, int ave, int size)
80 { // We start out building the wall northward
81 // the walls will be built on the east face of the current
82 // intersection
83 Direction facing = Direction.EAST;
84
85 while( size > 0)
86 { int spacesLeft = size;
87 int aveChange = 0;
88 int stChange = 0;
89 switch(facing)
90 {
91 case EAST:
92 stChange = -1;
93 break;
94 case NORTH:
95 aveChange = -1;
96 break;
97 case WEST:
98 stChange = 1;
99 break;
100 case SOUTH:
101 aveChange = 1;
102 break;
103 }
104
105 while(spacesLeft > 0)
106 { new Wall(theCity, st, ave, facing);
107 ave = ave + aveChange;
108 st = st + stChange;
109 spacesLeft--;
110 }
111 // back up one space
112 ave = ave - aveChange;
113 st = st - stChange;
114
115 switch(facing)
116 { case EAST:
117 facing = Direction.NORTH;
118 break;
119 case NORTH:
120 facing = Direction.WEST;
121 size--;
122 break;
123 case WEST:
124 facing = Direction.SOUTH;
125 break;
126 case SOUTH:
127 facing = Direction.EAST;
128 size--;
129 break;
130 }
131 }
132 }
133
134 public static void main(String[] args)
135 { City calgary = new City(12, 12);
136 MazeBot don = new MazeBot(calgary, 1, 1, Direction.EAST, 0);
137
138 Maze.MakeMaze(calgary);
139
140 don.NavigateMaze();
141 }
142 }
Explanation / Answer
import becker.robots.*;
class MazeBot extends RobotSE
{
public MazeBot(City theCity, int str, int ave, Direction dir, int numThings)
{ super(theCity, str, ave, dir, numThings); }
public void printTotalNumberOfSpacesMoved()
{
}
// isAtEndSpot is what's called a 'helper method' - it exists just to make
// another command (in this case, NavigateMaze) easier to understand.
// It does this by replacing some code that otherwise would be in NavigateMaze
// with it's name, and doing that work here, instead.
// "private" means that only the MazeBot is allowed to call it.
private boolean isAtEndSpot()
{ // If we're at avenue 9, AND we're at street 10, then we must
// be at the intersection (9,10), which is the "way out" of the maze
// You can easily change this to tell the robot to stop at a different
// intersection.
return (this.getAvenue() == 9 && this.getStreet() == 10);
}
public void NavigateMaze()
{ // While your robot hasn't reached the 'ending spot'
// in the maze, take another step.
while( !this.isAtEndSpot() )
{ // What will you have the robot do at each step?
}
}
}
public class Maze extends Object
{
private static void MakeMaze(City theCity)
{
for(int i = 1; i < 11; i++)
{ // north wall
new Wall(theCity, 1, i, Direction.NORTH);
new Wall(theCity, 1, i, Direction.SOUTH);
// Third to north wall
if (i >= 4)
new Wall(theCity, 4, i, Direction.SOUTH);
// south wall
if (i != 9) // (9, 10, SOUTH), is where the 'exit' is
new Wall(theCity, 10, i, Direction.SOUTH);
// west wall
if( i != 1) // (1,1, WEST) is where the 'entrance' is
new Wall(theCity, i, 1, Direction.WEST);
// second to west-most wall
if (i >= 3 && i < 6)
new Wall(theCity, i, 6, Direction.WEST);
// east wall
new Wall(theCity, i, 10, Direction.EAST);
}
// cul-de-sac
new Wall(theCity, 3, 10, Direction.WEST);
new Wall(theCity, 3, 10, Direction.SOUTH);
new Wall(theCity, 2, 8, Direction.WEST);
new Wall(theCity, 2, 8, Direction.SOUTH);
new Wall(theCity, 10, 8, Direction.NORTH);
new Wall(theCity, 10, 9, Direction.EAST);
new Wall(theCity, 10, 9, Direction.NORTH);
MakeSpiral(theCity, 8, 9, 3);
new Wall(theCity, 8, 10, Direction.SOUTH);
MakeSpiral(theCity, 10, 5, 4);
}
public static void MakeSpiral(City theCity, int st, int ave, int size)
{ // We start out building the wall northward
// the walls will be built on the east face of the current
// intersection
Direction facing = Direction.EAST;
while( size > 0)
{ int spacesLeft = size;
int aveChange = 0;
int stChange = 0;
switch(facing)
{
case EAST:
stChange = -1;
break;
case NORTH:
aveChange = -1;
break;
case WEST:
stChange = 1;
break;
case SOUTH:
aveChange = 1;
break;
}
while(spacesLeft > 0)
{ new Wall(theCity, st, ave, facing);
ave = ave + aveChange;
st = st + stChange;
spacesLeft--;
}
// back up one space
ave = ave - aveChange;
st = st - stChange;
switch(facing)
{ case EAST:
facing = Direction.NORTH;
break;
case NORTH:
facing = Direction.WEST;
size--;
break;
case WEST:
facing = Direction.SOUTH;
break;
case SOUTH:
facing = Direction.EAST;
size--;
break;
}
}
}
public static void main(String[] args)
{ City calgary = new City(12, 12);
MazeBot don = new MazeBot(calgary, 1, 1, Direction.EAST, 0);
Maze.MakeMaze(calgary);
don.NavigateMaze();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.