package PartA;
public class Location{
public int popped,state,x,y=0;
char item;
public Location(Map maze,char[][] map, int inx, int iny){
x=inx;
y=iny;
if (x>=0 && x<maze.width && y>=0 && y<maze.height){
item=map[x][y];
}
}
}
Heres the actual maze class, the map class is functioning properly as I use the same one in the recursion program.
package PartA;
import Collections.*;
public class Maze{
char[][] maze;
Map map;
Location loc;
Stack<Location> locStack;
int c;
int startx,starty,x,y;
public Maze(){
map=new Map();
locStack=new ConStack<Location>(1000);
map.drawMap();
map.printMap();
startx=map.startx;
starty=map.starty;
System.out.println("Starting position ("+startx+","+starty+")");
loc=new Location(map,map.map,startx,starty);
locStack.push(loc);
while (locStack!=null){
loc=locStack.pop();
loc.state++;
x=loc.x;
y=loc.y;
map.printMap();
switch (loc.state){
case 0:
locStack.push(loc);
if (x>=0 && x<map.width && y>=0 && y<map.height){
loc=new Location(map,map.map,x+1,y);
loc.state++;
locStack.push(loc);
}
case 1:
locStack.push(loc);
if (x>=0 && x<map.width && y>=0 && y<map.height){
loc=new Location(map,map.map,x,y+1);
loc.state++;
locStack.push(loc);
}
case 2:
locStack.push(loc);
if (x>=0 && x<map.width && y>=0 && y<map.height){
loc=new Location(map,map.map,x-1,y);
loc.state++;
locStack.push(loc);
}
case 3:
locStack.push(loc);
if (x>=0 && x<map.width && y>=0 && y<map.height){
loc=new Location(map,map.map,x,y-1);
loc.state++;
locStack.push(loc);
}
case 4:
loc=locStack.pop();
if (x>=0 && x<map.width && y>=0 && y<map.height && loc.item!='#'){
map.map[loc.x][loc.y]='.';
}
}
}
map.printMap();
}
public static void main ( String[] args ) { new Maze(); };
}
Heres an example of a maze in which im trying to solve is in the attachments
Its stored in a 2d char array and starts at location 1,1 and has to reach E using stacks, and backtracking functions and marks the proper path with * and position tried but failed with .
Thanks for your help in advance, not looking for code but more of help on how to reach a solution, examples of code would be helpful but not needed as I want to learn and not just think I understand.
Attached File(s)
-
maze1.txt (114bytes)
Number of downloads: 135

New Topic/Question
Reply




MultiQuote







|