import java.util.Random;
public class maze {
char[][]m; //The place I plan to stick my maze
int [][] shadow; // holds some int for each room the maze
maze(int rows, int cols){
m = new char[2*rows+1][2*cols+1];
for (int r=0; r<m.length; r++){
if (r%2 == 0){// if r is even
for (int c=0; c<m[r].length; c++){
if (c%2 == 0){ // c is even
m[r][c] = '+';
}
else { // c is odd
m[r][c] = '-';
}
}
}
else {// r is odd
for (int c=0; c<m[r].length; c++){
if (c%2 == 0){ // c is even
m[r][c] = '|';
}
else { // c is odd
m[r][c] = ' ';
}
}
}
}
shadow = new int[rows][cols];
int num = 0;
for (int r = 0; r<shadow.length; r++){
for (int c =0; c<shadow[r].length; c++){
shadow[r][c] = num++;
}
}
// now, randomly choose rooms, and check in a random direction,
// to see if the next room has the same shadow number
Random rg = new Random();
int erasures = 0;
while (erasures < num - 1){
int r = rg.nextInt(shadow.length);
int c = rg.nextInt(shadow[0].length);
int i = shadow[r][c];
int d = rg.nextInt(4);
switch (d){
case 0: // look left
if (c == 0) break;
if (shadow[r][c-1] == i) break;
match(r,c,r,c-1);
m[2*r+1][2*c] = ' '; // erase the wall to the left
erasures += 1;
break;
case 1: // look up
if (r == 0)break;
if (shadow[r-1][c] == i) break;
match(r,c,r-1,c);
m[2*r][2*c+1] = ' '; // erase the wall above
erasures += 1;
break;
case 2: // look right
if (c == shadow[0].length -1) break;
if (shadow[r][c+1] == i) break;
match(r,c,r,c+1);
m[2*r+1][2*c+2] = ' '; // erase the wall to the right
erasures += 1;
break;
case 3: // look down
if (r == shadow.length -1)break;
if (shadow[r+1][c] == i) break;
match(r,c,r+1,c);
m[2*r+2][2*c+1] = ' '; // erase the wall below
erasures += 1;
break;
}
}
}
/**
* The job of match is to find the smallest of the numbers in r0,c0 or r1,c1
* in the shadow array, and change every occurence of the two different numbers
* to the smallest
*
*/
void match(int r0, int c0, int r1, int c1){
int i = shadow[r0][c0];
int i1 = shadow[r1][c1];
int small = Math.min(i,i1);
int big = i + i1 - small;
for (int r = 0; r < shadow.length; r++){
for (int c=0; c< shadow[0].length; c++){
if (shadow[r][c] == big){ shadow[r][c] = small; }
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
maze joe = new maze(2,2);
System.out.println(joe.toString());
joe.numbers();
System.out.println(joe);
joe = new maze(5,5);
System.out.println(joe.toString());
joe.numbers();
joe.mazesolve();
}
private void mazesolve() {
int sr =1;
int sc=1;
//put an * at start room
while (
//check all 4 directions
// TODO Auto-generated method stub
}
static String [] spaces = {
"",
" ",
" ",
" ",
" ",
" " };
/**
* This version of toString()
* combines the walls of the maze with the
* values in the shadow array
* Its job is to permit easy printout of the maze and the shadow
* at various points in debugging the assorted algorithms.
* It could really have a different name; calling it
* toString(), and overriding the Object.toString() method
* means that one can say System.out.println(a);
* instead of System.out.println(a.something());
*/
public String toString() {
int largest = m.length * m[0].length - 1;
int digits = 1 + (int)Math.log10(largest);
String answer = "";
for (int r = 0; r<m.length; r++){
for(int c = 0; c<m[r].length; c++){
if (r%2==1 && c%2 == 1){// if this is a room in the maze
String s = Integer.toString(shadow[r/2][c/2]);
if (s.length() < digits) {
s = spaces[digits-s.length()] + s;
}
answer += s;
}
else { // not a room
if (r%2 == 0 && (m[r][c] == '-'|| m[r][c] == ' ')){
for (int i = 0; i<digits; i++){
answer += m[r][c];
}
}
else {
answer += m[r][c];
}
}
}
answer += '\n';
}
return answer;
}
public void numbers(){
// here I code the assumption about the end
int endrow = shadow.length-1;
int endcol = shadow[endrow].length-1;
int [][] shadow = new int [m.length/2][m[endrow].length/2];
for (int r=0; r<shadow.length; r++){
for (int c=0; c<shadow[r].length; c++){
shadow[r][c] = -1;
}
}
shadow[endrow][endcol] = 0;
// for all the possible distances up to endrow*endcol, starting with zero
for (int d=0; d<=(1+endrow)*(1+endcol); d++){
// find all the squares in the maze marked with that distance
for (int r=0; r<shadow.length; r++){
for (int c=0; c<shadow[r].length; c++){
if (shadow[r][c] == d){
// mark the squares next to them with one more
if (m[2*r+1][2*c] == ' ' /* left */
&& (shadow[r][c-1] == -1 || shadow[r][c-1]>d)){
shadow[r][c-1] = d+1;
}
if (m[1+2*r][1+2*c+1]// right
== ' '
&& (shadow[r][c+1] == -1 || shadow[r][c+1]>d)){
shadow[r][c+1] = d+1;
}
if (m[1+2*r-1][1+2*c] //above
== ' '
&& (shadow[r-1][c] == -1 || shadow[r-1][c]>d)){
shadow[r-1][c] = d+1;
}
if (m[1+2*r+1][1+2*c] == ' ' // below
&& (shadow[r+1][c] == -1 || shadow[r+1][c]>d)){
shadow[r+1][c] = d+1;
}
}
}
}
}
for (int r=0; r<shadow.length; r++){
for (int c=0; c<shadow[r].length; c++){
System.out.printf("%4d ", shadow[r][c]);
}
System.out.println();
}
}
}
Could you help me write a method which solves the maze, and prints the maze on the screen with a series of asterisk characters which show one of the shortest paths through the maze.