mazewalker
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
mazewalker [2019/08/29 22:52] – frchris | mazewalker [2019/08/29 23:52] – frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | Write an instruction '' | + | Write an instruction '' |
- | {{::maze_moves.png|}} | + | |
+ | ====== Before ====== | ||
+ | |||
+ | {{::turnrightbefore.png|}} | ||
+ | ====== After ====== | ||
+ | {{:: | ||
+ | |||
+ | 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? | ||
- | 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 | + | |
- | { | + | public |
- | MazeWalker mayzie = new MazeWalker(1,1,East,0); | + | |
- | mayzie.escapeMaze(); | + | |
- | mayzie.turnOff(); | + | super(street, avenue, direction, beepers); |
+ | |||
} | } | ||
- | public | + | /** |
+ | * 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 | ||
+ | * mentioned. | ||
+ | */ | ||
+ | public void followRightWall() | ||
{ | { | ||
- | World.reset(); | + | |
- | World.setVisible(); | + | |
- | World.setDelay(10); | + | |
- | World.setBeeperColor(Color.RED); | + | |
- | String maze = " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | World.getWorld(maze); | + | |
- | task(); | + | |
} | } | ||
+ | |||
+ | public void turnRight() { | ||
+ | for (int i=0; i<3; i++) | ||
+ | turnLeft(); | ||
+ | } | ||
+ | |||
} | } | ||
</ | </ | ||
+ | |||
+ | First use the '' | ||
+ | |||
+ | {{ :: | ||
+ | |||
+ | Once the tester shows that the four cases are handled correctly, here is a maze runner class with its own maze: | ||
+ | |||
+ | {{ :: | ||
+ |
mazewalker.txt · Last modified: 2022/09/05 12:25 by frchris