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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java Maze Help

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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] = ' ';
    }
    }
    }
    }
    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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Maze Help

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

    Please see the link in my signature on asking questions the smart way, then try again.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Java Maze Help

    Quote Originally Posted by lalala096 View Post
    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. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Maze Help

    I'm just not even sure where to start

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Maze Help

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    where to start
    How would you do it manually using the tip of your pencil to touch the squares in the maze as it moved through the maze? Touch the pencil to the starting square. How would you decide the next square to move to? The first factor would be if the square is not a wall or boundary. When the tip on the pencil is moved to the next avaiable square, again check where it can move to next.
    The process of finding an algorithm will be iterative and repetitive and will bump into many dead ends and have to be restarted and redesigned as approaches fail and others need to be tried.
    Its not as simple as writing some nested loops to print out a triangle of *s
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Maze Help

    Quote Originally Posted by lalala096 View Post
    I'm just not even sure where to start
    Starting Writing a Program
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Recursive Maze Help
    By Deejay1992 in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 26th, 2012, 11:25 PM
  2. Need some help printing out my maze!
    By erock618 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 3rd, 2012, 04:50 PM
  3. Java Maze Problem (Please Help Guys)
    By acash229 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 5th, 2012, 09:04 AM
  4. java maze move charcter?
    By coolcool1980 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2011, 02:18 PM
  5. Basic Java maze game
    By sciontc1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2011, 10:18 AM