So basically what I'm trying to do is creating a maze solving algorithm with the left hand rule but I'm encountering an issue that I can't seem to get passed.
The first two codes are the ones I'm working with and the problem I'm having is that when I try to loop the if-statements it just continues to draw a straight line up instead of turning left (in this case) and THEN looping back to the beginning and drawing a line until it hits a wall again. Note that I'm aware that this aint the finished product, I just want to make sure that alteast the first left turn and the loop works correctly.
import se.lth.cs.ptdc.window.SimpleWindow;
import se.lth.cs.ptdc.maze.*;
public class MazeTurtle extends Turtle {
protected int Maze;
public MazeTurtle(SimpleWindow w, int x, int y) {
super(w, x, y);
}
public void walk(Maze maze) {
Maze m = new Maze(1);
Turtle t = new Turtle(w, m.getXEntry(), m.getYEntry());
t.penDown();
while(true){
if(m.wallAtLeft(getDirection(), getX(), getY())){
t.forward(1);
}
if(m.wallAtLeft(getDirection(), getX(), getY())){
t.left(90);
}
/** The "wallInFront" could be ignored for now */
if(m.wallInFront(getDirection(), getX(), getY())) {
t.left(-90);
}
if(m.wallInFront(getDirection(), getX(), getY())){
t.forward(1);
}
SimpleWindow.delay(10);
}
}
}
Here is the the "Test" which tries to solve the maze with the given algorithm:
import se.lth.cs.ptdc.window.SimpleWindow;
import se.lth.cs.ptdc.maze.*;
public class MazeTest {
public static void main(String args[]) {
Maze m = new Maze(1);
SimpleWindow w = new SimpleWindow(600, 600, "MazeTest");
MazeTurtle t = new MazeTurtle(w, m.getXEntry(), m.getYEntry());
t.penDown();
m.draw(w);
t.walk(m);
}
}
Here is the maze class which i'm reffering to: http://pastebin.com/gxSeEc2U
And the turtle class that I'm using: http://pastebin.com/0RqbVudn
I'm pretty sure this should work out now
while ((m.atExit(x1, y1)) == false) {
if (m.wallAtLeft(dir, x1, y1) == true) {
t.forward(1);
SimpleWindow.delay(10);
}
else if (m.wallAtLeft(dir, x1, y1) == false) {
t.left(90);
t.forward(1);
SimpleWindow.delay(10);
}
if (m.wallInFront(dir, x1, y1) == true) {
t.left(-90);
t.forward(1);
}
Try the following. This should check if there is a wall on the left, and go forward 1 if there is. If there is no wall on the left, it will turn left 90 degrees and move forward 1.
if(m.wallAtLeft(getDirection(), getX(), getY())){
t.forward(1);
} else {
t.left(90);
t.forward(1);
}
I'm fairly new to Java but I'd try something like this
for (int i=0; i<500; i++) {
SimpleWindow.delay(10);
if (m.wallInFront(getDirection(), getX(), getY()) == false) {
t.forward(1);
}
if (m.wallInFront(getDirection(), getX(), getY()) == true) {
t.left(90);
}
}
To not hit the wall
while (true) {
if (m.wallAtLeft(getDirection(), getX(), getY())) {
if (m.wallInFront(getDirection(), getX(), getY())) {
t.left(-90);
} else {
t.forward(1);
SimpleWindow.delay(10);
}
} else {
t.left(90);
t.forward(1);
SimpleWindow.delay(10);
}
}
You are confused because the "wall at left" gives the answer relative to where the turtle is facing.
With your current algorithm....
Any time you turn left because there is a wall to your left, you will immediately turn right because now the wall is in front of you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.