User Tools

Site Tools


mazewalker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
mazewalker [2019/08/29 22:52]
frchris
mazewalker [2019/08/29 23:59] (current)
frchris
Line 1: Line 1:
-Write an instruction ''​followWallRight''​ for the ''​MazeWalker''​ class, assuming that whenever a robot executes this instruction there is a wall directly to the right. ​  +Write an instruction ''​followWallRight''​ for the ''​MazeWalker''​ class, assuming that whenever a robot executes this instruction there is a wall directly to the right.  ​It should be able to handle these four possible situations:  
-{{::maze_moves.png|}}+ 
 +====== Before ====== 
 + 
 +{{::turnrightbefore.png|}} 
 +====== After ====== 
 +{{::​turnrightafter.png|}} 
 + 
 +These four different position changes is the cornerstone for the algorithm that directs a robot to escape from a maze simply by following the right wall.  It isn't the most efficient algorithm, and it won't work on mazes that have islands (Can you imagine why?​). ​ Do you think following the left walls would be better? ​
  
-Here are four of the different position changes that the robot must be able to make.  This instruction is the cornerstone for a program that directs a robot to escape from a maze (for homework later) 
  
-====== MazeWalkerRunner.java ====== 
 <code java> <code java>
-import java.awt.Color;​ 
 import kareltherobot.*;​ import kareltherobot.*;​
-public class MazeWalkerRunner implements Directions+public class MazeWalker extends Robot
 { {
- public ​static void task() + 
-+ public ​MazeWalker(int street, int avenue,  
- MazeWalker mayzie = new MazeWalker(1,1,East,0); +                       ​Direction direction, int beepers)  
- mayzie.escapeMaze();​ +       ​
- mayzie.turnOff();​+ super(streetavenuedirectionbeepers); 
 +
  }  }
- public ​static ​void main(String[] args)+    /** 
 +     * This is an algorithm to run a maze. It isn't the fastest method, 
 +     * and won't work if the maze has any islands (Can you imagine why?) 
 +     * Would it be better to follow the leftWalls?​ 
 +     */ 
 + public void escapeMaze() { 
 + while (! nextToABeeper() ) 
 + followRightWall();​  
 +
 +    /** 
 +     * This will move the Robot according to the diagram 
 +     * above. ​ Use if () else statements to handle the 4 cases 
 +     */ 
 + public void followRightWall() 
  {  {
- World.reset();​ +
- World.setVisible();​ +
- World.setDelay(10);​ +
- World.setBeeperColor(Color.RED);​ +
- String maze = "​streets 10\n" +  +
- "​avenues 10\n" +  +
- "​beepers 9 10 1\n" +  +
- "​eastwestwalls 6 9 9\n" +  +
- "​eastwestwalls 3 4 4\n" +  +
- "​eastwestwalls 6 8 8\n" +  +
- "​eastwestwalls 3 3 3\n" +  +
- "​eastwestwalls 6 7 7\n" +  +
- "​eastwestwalls 3 2 2\n" +  +
- "​eastwestwalls 6 6 6\n" +  +
- "​eastwestwalls 6 5 5\n" +  +
- "​eastwestwalls 9 8 8\n" +  +
- "​eastwestwalls 6 2 2\n" +  +
- "​eastwestwalls 9 7 7\n" +  +
- "​eastwestwalls 6 1 1\n" +  +
- "​eastwestwalls 9 6 6\n" +  +
- "​eastwestwalls 9 5 5\n" +  +
- "​eastwestwalls 2 8 8\n" +  +
- "​eastwestwalls 9 4 4\n" +  +
- "​eastwestwalls 2 7 7\n" +  +
- "​eastwestwalls 2 6 6\n" +  +
- "​eastwestwalls 5 4 4\n" +  +
- "​eastwestwalls 1 9 9\n" +  +
- "​eastwestwalls 8 3 3\n" +  +
- "​eastwestwalls 8 2 2\n" +  +
- "​eastwestwalls 8 1 1\n" +  +
- "​eastwestwalls 1 5 5\n" +  +
- "​eastwestwalls 1 4 4\n" +  +
- "​eastwestwalls 1 2 2\n" +  +
- "​eastwestwalls 1 1 1\n" +  +
- "​eastwestwalls 7 6 6\n" +  +
- "​northsouthwalls 9 9 9\n" +  +
- "​northsouthwalls 7 9 9\n" +  +
- "​northsouthwalls 3 9 9\n" +  +
- "​northsouthwalls 9 7 7\n" +  +
- "​northsouthwalls 5 7 7\n" +  +
- "​northsouthwalls 2 7 7\n" +  +
- "​northsouthwalls 9 5 5\n" +  +
- "​northsouthwalls 6 5 5\n" +  +
- "​northsouthwalls 3 5 5\n" +  +
- "​northsouthwalls 9 3 3\n" +  +
- "​northsouthwalls 1 5 5\n" +  +
- "​northsouthwalls 7 3 3\n" +  +
- "​northsouthwalls 5 3 3\n" +  +
- "​northsouthwalls 4 3 3\n" +  +
- "​northsouthwalls 9 1 1\n" +  +
- "​northsouthwalls 1 3 3\n" +  +
- "​northsouthwalls 9 8 8\n" +  +
- "​northsouthwalls 7 8 8\n" +  +
- "​northsouthwalls 2 8 8\n" +  +
- "​northsouthwalls 9 6 6\n" +  +
- "​northsouthwalls 6 6 6\n" +  +
- "​northsouthwalls 4 6 6\n" +  +
- "​northsouthwalls 9 4 4\n" +  +
- "​northsouthwalls 7 4 4\n" +  +
- "​northsouthwalls 5 4 4\n" +  +
- "​northsouthwalls 3 4 4\n" +  +
- "​northsouthwalls 9 2 2\n" +  +
- "​northsouthwalls 1 4 4\n" +  +
- "​northsouthwalls 5 2 2\n" +  +
- "​northsouthwalls 4 2 2"; +
- World.getWorld(maze);​ +
- task();+
  }  }
 +
 + public void turnRight() {
 + for (int i=0; i<3; i++)
 + turnLeft();​
 + }
 +
 } }
 </​code>​ </​code>​
 +
 +
 +First use the ''​MazeWalkerTester''​ to see if your code deals with the four situations correctly.
 + 
 +{{ ::​mazewalkertester.java |MazeWalkerTester.java}}
 +
 +Once the tester shows that the four cases are handled correctly, here is a maze runner class with its own maze:
 +
 +{{ ::​mazewalkerrunner.java |MazeWalkerRunner.java}}
 +
 +Hints:
 +  * Case One check: is the front clear?
 +  * Case 2,3,4 you can move
 +  * Case 2 versus case 3,4: is there a wall on the right?
 +  * In case 3 and 4 you can move to the right.
 +  * Case 3 versus case 4: is there a wall on the right?
mazewalker.1567133542.txt.gz · Last modified: 2019/08/29 22:52 by frchris