Created
January 5, 2015 12:16
-
-
Save beaussan/4c22603622dc5628ae16 to your computer and use it in GitHub Desktop.
mazeGeneratorIjava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class mazeGenerator extends Program { | |
static class Point{ | |
Integer r; | |
Integer c; | |
Point parent; | |
} | |
final int r=30, c=30; | |
char[][] initArray(int row, int col){ | |
char[][] retVal=new char[row][col]; | |
for (int i=0; i<length(retVal); i++) { | |
for (int j=0;j<length(retVal) ; j++) { | |
retVal[i][j]='#'; | |
} | |
} | |
return retVal; | |
} | |
Point newPoint(int x, int y, Point p){ | |
Point retVal= new Point(); | |
retVal.r=x; | |
retVal.c=y; | |
retVal.parent=p; | |
return retVal; | |
} | |
Point opposite(Point source){ | |
if (compareTo(source.r,source.parent.r)!=0) { | |
return newPoint(source.r+compareTo(source.r,source.parent.r),source.c,source); | |
} | |
if (compareTo(source.c,source.parent.c)!=0) { | |
return newPoint(source.r,source.c+compareTo(source.c,source.parent.c),source); | |
} | |
return null; | |
} | |
int compareTo(int a, int b){ | |
if (a==b) { | |
return 0; | |
}else if (a<b) { | |
return -1; | |
}else{ | |
return 1; | |
} | |
} | |
Point[] arrayPointAdd(Point[] data, Point a){ | |
Point[] retVal = new Point[length(data)+1]; | |
for (int i=0;i<length(data) ; i++) { | |
retVal[i]=data[i]; | |
} | |
retVal[length(data)]=a; | |
return retVal; | |
} | |
Point[] arrayPointRemove(Point[] data, int ind){ | |
Point[] retVal = new Point[length(data)-1]; | |
for (int i=0;i<ind ; i++) { | |
retVal[i]=data[i]; | |
} | |
for (int i = ind+1;i<length(data) ; i++) { | |
retVal[i-1]=data[i]; | |
} | |
return retVal; | |
} | |
char[][] makeWals(char[][] data, int row, int colum){ | |
char[][] retVal = new char[row][colum]; | |
for (int i=0;i<row ; i++) { | |
for (int j=0;j<colum ; j++) { | |
if ( i==0 || i==row-1 || j==0 || j==colum-1) { | |
retVal[i][j]='#'; | |
}else{ | |
retVal[i][j]=data[i-1][j-1]; | |
} | |
} | |
} | |
return retVal; | |
} | |
int getRnd(int size){ | |
return (int)(random()*size); | |
} | |
void algorithm() | |
{ | |
// build maze and initialize with only walls | |
char[][] maz = initArray(r-2,c-2); | |
// select random point and open as start node | |
Point st = newPoint(getRnd(r-2),getRnd(c-2),null); | |
maz[st.r][st.c] = 'S'; | |
// iterate through direct neighbors of node | |
Point[] frontier = new Point[0]; | |
for(int x=-1;x<=1;x++) | |
for(int y=-1;y<=1;y++){ | |
if(x==0&&y==0||x!=0&&y!=0) | |
continue; | |
try{ | |
if(maz[st.r+x][st.c+y]==' ') continue; | |
}catch(Exception e){ // ignore ArrayIndexOutOfBounds | |
continue; | |
} | |
// add eligible points to frontier | |
frontier = arrayPointAdd(frontier,newPoint(st.r+x,st.c+y,st)); | |
} | |
Point last=null; | |
int tmp; | |
while(length(frontier)!=0){ | |
// pick current node at random | |
tmp=getRnd(length(frontier)); | |
Point cu = frontier[tmp]; | |
frontier=arrayPointRemove(frontier,tmp); | |
Point op = opposite(cu); | |
try{ | |
// if both node and its opposite are walls | |
if(maz[cu.r][cu.c]=='#'){ | |
if(maz[op.r][op.c]=='#'){ | |
// open path between the nodes | |
maz[cu.r][cu.c]=' '; | |
maz[op.r][op.c]=' '; | |
// store last node in order to mark it later | |
last = op; | |
// iterate through direct neighbors of node, same as earlier | |
for(int x=-1;x<=1;x++) | |
for(int y=-1;y<=1;y++){ | |
if(x==0&&y==0||x!=0&&y!=0) | |
continue; | |
try{ | |
if(maz[op.r+x][op.c+y]==' ') continue; | |
}catch(Exception e){ // ignore ArrayIndexOutOfBounds | |
continue; | |
} | |
frontier = arrayPointAdd(frontier,newPoint(op.r+x,op.c+y,op)); | |
} | |
} | |
} | |
// if algorithm has resolved, mark end node | |
if(length(frontier)==0) | |
maz[last.r][last.c]='E'; | |
}catch(Exception e){ // ignore NullPointer and ArrayIndexOutOfBounds | |
} | |
} | |
println(); | |
println("sans les murs"); | |
println(); | |
// print final maze | |
for(int i=0;i<r-2;i++){ | |
for(int j=0;j<c-2;j++) | |
print(maz[i][j]); | |
println(); | |
} | |
println(); | |
println("avec les murs"); | |
println(); | |
maz = makeWals(maz,r,c); | |
// print final maze witch walls | |
for(int i=0;i<r;i++){ | |
for(int j=0;j<c;j++) | |
print(maz[i][j]); | |
println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment