Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Java Maze Help

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] = ' ';
}
}
}
}
int num = 0;
for (int r = 0; r<shadow.length; r++){
for (int c =0; c<shadow[r].length; c++){
}
}
// 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 d = rg.nextInt(4);
switch (d){
case 0: // look left
if (c == 0) 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;
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;
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
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 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++){
}
}
}
/**
* @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);
*/
public String toString() {
int largest = m.length * m[0].length - 1;
int digits = 1 + (int)Math.log10(largest);
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
if (s.length() < digits) {
s = spaces[digits-s.length()] + s;
}
}
else { // not a room
if (r%2 == 0 && (m[r][c] == '-'|| m[r][c] == ' ')){
for (int i = 0; i<digits; i++){
}
}
else {
}
}
}
}

}
public void numbers(){
// here I code the assumption about the end

int [][] shadow = new int [m.length/2][m[endrow].length/2];
}
}

// 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
// mark the squares next to them with one more
if (m[2*r+1][2*c] == ' ' /* left */
}
if (m[1+2*r][1+2*c+1]// right
== ' '
}
if (m[1+2*r-1][1+2*c] //above
== ' '
}
if (m[1+2*r+1][1+2*c] == ' ' // below
}
}
}
}

}
}
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.

2. ## Re: Java Maze Help

Sure. Where are you stuck? What have you tried? What's your specific technical question?

3. ## Re: Java Maze Help

Originally Posted by lalala096
import java.util.Random;

public class maze {
....
Your code has been posted without code tags and so has lost all its formatting and is unreadable. You will probably get better help faster if folks can read your code, and so it is in your best interest to read the forum FAQ to learn how to place [code] [/code] tags around your code so that it retains its formatting.

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.
We won't write this for you of course, and we can only help if you ask direct specific questions, questions that are answerable and that tell us where exactly you are stuck. So where exactly are you stuck? Are you unsure of the logic/algorithm of your problem? Or is your problem with translating an algorithm you understand into Java code? Again the more specific the question usually the better the help.

4. ## Re: Java Maze Help

I'm just not even sure where to start

[code=java]