Skip to content
Browse files

add default path finder

  • Loading branch information...
1 parent 4652bb2 commit 47bfaabc7cb4bfe385ec41b0bf885349b0447193 @liweinan committed Mar 24, 2013
View
64 src/main/java/io/maze/Maze.java
@@ -21,6 +21,20 @@
private Stack<CellState> histories = new Stack<CellState>();
private int visited_cnt = 0;
+ public CellState getCell(int x, int y) {
+ if (in_scope(x, y))
+ return cells[x][y];
+ return null;
+ }
+
+ public CellState getIn() {
+ return in;
+ }
+
+ public CellState getOut() {
+ return out;
+ }
+
public Maze(int m, int n) {
this.m = m;
this.n = n;
@@ -49,7 +63,7 @@ public void generate() {
visited_cnt++;
System.out.println("LEGEND: C- CANDIDATE, D- DECISION, R- RESULT, G- GO BACK");
- while (histories.size() > 0 && visited_cnt < m*n) {
+ while (histories.size() > 0 && visited_cnt < m * n) {
while (currentCell != null) {
currentCell = nextCell(currentCell);
if (currentCell != null) {
@@ -94,6 +108,46 @@ private CellState nextCell(CellState currentCell) {
return null;
}
+ public void resetCellState() {
+ for (int x = 0; x < m; x++) {
+ for (int y = 0; y < n; y++) {
+ cells[x][y].setVisited(false);
+ }
+ }
+ }
+
+ public CellState eastOf(CellState current) {
+ int x = current.getX() + 1;
+ int y = current.getY();
+ if (in_scope(x, y))
+ return getCell(x, y);
+ return null;
+ }
+
+ public CellState southOf(CellState current) {
+ int x = current.getX();
+ int y = current.getY() + 1;
+ if (in_scope(x, y))
+ return getCell(x, y);
+ return null;
+ }
+
+ public CellState westOf(CellState current) {
+ int x = current.getX() - 1;
+ int y = current.getY();
+ if (in_scope(x, y))
+ return getCell(x, y);
+ return null;
+ }
+
+ public CellState northOf(CellState current) {
+ int x = current.getX();
+ int y = current.getY() - 1;
+ if (in_scope(x, y))
+ return getCell(x, y);
+ return null;
+ }
+
private class Selection {
private Direction direction;
private CellState cell;
@@ -179,14 +233,18 @@ public void print() {
CellState cell = cells[x][y];
String part = new String(token);
- if (x==0 && y==0) { // in
+ if (x == 0 && y == 0) { // in
part = part.replaceAll(" \\|", " > |");
}
- if (x==m-1 && y==n-1) { //out
+ if (x == m - 1 && y == n - 1) { //out
part = part.replaceAll(" \\|", " V |");
}
+ // draw path
+ if (cell.isInPath()) {
+ part = part.replaceAll(" \\|", " * |");
+ }
if (cell.getSouthDoor().isOpened()) {
part = part.replaceAll("\\-", " ");
View
7 src/main/java/io/maze/PlayMaze.java
@@ -2,6 +2,8 @@
import io.maze.Maze;
import io.maze.MazeFactory;
+import io.maze.finder.DefaultPathFinder;
+import io.maze.finder.PathFinder;
/**
* Created with IntelliJ IDEA.
@@ -13,7 +15,10 @@
public class PlayMaze {
public static final void main(String[] args) {
- Maze maze = MazeFactory.createMaze(3, 3);
+ Maze maze = MazeFactory.createMaze(30, 30);
+ maze.print();
+ PathFinder finder = new DefaultPathFinder();
+ finder.solve(maze);
maze.print();
}
}
View
76 src/main/java/io/maze/finder/DefaultPathFinder.java
@@ -0,0 +1,76 @@
+package io.maze.finder;
+
+import io.maze.Maze;
+import io.maze.cell.CellState;
+
+import java.util.Stack;
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: weinanli
+ * Date: 3/24/13
+ * Time: 3:52 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class DefaultPathFinder implements PathFinder {
+
+ @Override
+ public void solve(Maze maze) {
+ if (maze == null)
+ return;
+ maze.resetCellState();
+
+ CellState current = maze.getIn();
+ current.setVisited(true);
+
+ Stack<CellState> route = new Stack<CellState>();
+ route.push(current);
+ boolean poped = false;
+
+ while (!maze.getOut().equals(current)) {
+ CellState prev = current;
+ current = walkToNext(maze, current);
+
+ if (current != null) {
+ if (poped) {
+ route.push(prev);
+ poped = false;
+ }
+
+ route.push(current);
+
+ } else {
+ current = route.pop();
+ poped = true;
+ }
+ }
+
+ current.setInPath(true);
+ route.push(current);
+
+ for (CellState step : route) {
+ step.setInPath(true);
+ System.out.printf("(%d, %d)", step.getX(), step.getY());
+ }
+ System.out.println("");
+ }
+
+ private CellState walkToNext(Maze maze, CellState current) {
+ CellState next = nextAvaliable(maze, current);
+ if (next != null)
+ next.setVisited(true);
+ return next;
+ }
+
+ private CellState nextAvaliable(Maze maze, CellState current) {
+ if (current.getNorthDoor().isOpened() && !maze.northOf(current).isVisited())
+ return maze.northOf(current);
+ if (current.getEastDoor().isOpened() && !maze.eastOf(current).isVisited())
+ return maze.eastOf(current);
+ if (current.getSouthDoor().isOpened() && !maze.southOf(current).isVisited())
+ return maze.southOf(current);
+ if (current.getWestDoor().isOpened() && !maze.westOf(current).isVisited())
+ return maze.westOf(current);
+ return null;
+ }
+}

0 comments on commit 47bfaab

Please sign in to comment.
Something went wrong with that request. Please try again.