import java.awt.*; public class OverlappingMaze extends java.applet.Applet implements Runnable { int[][] cell, // 0 = untouched, 1 = crumb trail back to start, 2 = used and de-trailed L , // connects left 0, 1, or 2 U , // connects up 0, 1, or 2 R , // connects right 0, 1, or 2 D ; // connects down 0, 1, or 2 int X, Y, W=0, H=0 ; Thread thread ; boolean done ; Graphics gfx ; char direc[]=new char[4] ; int dist []=new int[4], options ; private void fillCorner(Graphics g, int centerX, int centerY, int A, int B, int RAD, boolean isBLACK) { double rad2INNER=(RAD-.5)*(RAD-.5); double rad2OUTER=(RAD+.5)*(RAD+.5); double radDIFF =rad2OUTER-rad2INNER; for (int j=B; jrad2INNER) { int greyLEVEL=255; if (dist2A) { // horizontal path gfx .clearRect(21+20*A, 9+20*B, 8,12); if (C-A>1) gfx.clearRect(41+20*A, 9+20*B, 8,12); } else { // vertical path gfx .clearRect( 9+20*A,21+20*B,12, 8); if (D-B>1) gfx.clearRect( 9+20*A,41+20*B,12, 8); } if (C-A>1 || D-B>1) drawBridge(gfx,(A+C)/2,(B+D)/2); } public void paint(Graphics g) { // Draw the entire maze in its current state. if (W<1 || H<1) return; // <-- Sometimes, paint() can be called before first call to run(). for (int y=0; y0 && x0 && y0 && cell[X-1][Y ]==0) { direc[options]='L'; dist[options]=1; options++; } if (Y>0 && cell[X ][Y-1]==0) { direc[options]='U'; dist[options]=1; options++; } if (X1 && cell[X-1][Y ]!=0 && L[X-1][Y ]==0 && U[X-1][Y ]!=0 && R[X-1][Y ]==0 && D[X-1][Y ]!=0 && cell[X-2][Y ]==0) { direc[options]='L'; dist[options]=2; options++; } if (Y>1 && cell[X ][Y-1]!=0 && L[X ][Y-1]!=0 && U[X ][Y-1]==0 && R[X ][Y-1]!=0 && D[X ][Y-1]==0 && cell[X ][Y-2]==0) { direc[options]='U'; dist[options]=2; options++; } if (X0) { // Extend the path. int i=(int) (Math.random()*options); if (direc[i]=='L') { L[X][Y]=dist[i]; drawPath(X-dist[i],Y,X,Y); X-=dist[i]; R[X][Y]=dist[i]; } if (direc[i]=='U') { U[X][Y]=dist[i]; drawPath(X,Y-dist[i],X,Y); Y-=dist[i]; D[X][Y]=dist[i]; } if (direc[i]=='R') { R[X][Y]=dist[i]; drawPath(X,Y,X+dist[i],Y); X+=dist[i]; L[X][Y]=dist[i]; } if (direc[i]=='D') { D[X][Y]=dist[i]; drawPath(X,Y,X,Y+dist[i]); Y+=dist[i]; U[X][Y]=dist[i]; } crumb(); } else { // Retreat the path. noCrumb(); if (cell[X-L[X][Y]][Y ]==1) X-=L[X][Y]; else if (cell[X ][Y-U[X][Y]]==1) Y-=U[X][Y]; else if (cell[X+R[X][Y]][Y ]==1) X+=R[X][Y]; else if (cell[X ][Y+D[X][Y]]==1) Y+=D[X][Y]; else done=true; }} // Open up the start and end of the maze. U[0][0]=1; D[W-1][H-1]=1; // Redraw the entire maze to open up the start and end, and to perform other pretty touch-ups. repaint(); }}