import java.awt.*; import javax.swing.*; public class Maze2DArray extends JApplet { int rows, cols, scale; double wallProbability; public void init() { String input; input = JOptionPane.showInputDialog("Enter # of rows","100"); rows = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter # of columns","100"); cols = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter probabilty of 'wall'","0.5"); wallProbability = Double.parseDouble(input); if (wallProbability<0 || wallProbability>1) { JOptionPane.showInputDialog(null,"Sorry dude. wall probability must be >=0 and <=1"); System.exit(1); } input = JOptionPane.showInputDialog("Enter scale factor","10"); scale = Integer.parseInt(input); } public void paint(Graphics g) { super.paint(g); int [][] maze = new int[rows][cols]; //initalized to 0: "clear" for (int i=0; i0 && maze[r][c-1] == 0) { //left is clear c--; maze[r][c] = cheese++; //"visited" put a cheese g.fillRect(c*scale,r*scale,scale,scale); } else if (c0 && maze[r-1][c] == 0) { //up r--; maze[r][c] = cheese++; //"visited" put a cheese g.fillRect(c*scale,r*scale,scale,scale); } else if (r0) left = maze[r][c-1]; else left = -1; if (c0) up = maze[r-1][c]; else up = -1; if (rdown && up>left && up>right) //came from up nbor r--; //step back into there else if (down>up && down>left && down>right) //came from down nbor r++; else if (left>up && left>down && left>right) //came from left nbor c--; else if (right>up && right>down && right>left) //came from right nbor c++; } //if backedup to start and no moves available, trapped if (r==startR && c==startC && maze[r][c-1]!=0 && maze[r][c+1]!=0 && maze[r-1][c]!=0 && maze[r+1][c]!=0) trapped = true; } while (!trapped); g.setColor(Color.BLUE); g.fillRect(0,0,20,20); //done signal } }