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 9 of 9

Thread: Need some help printing out my maze!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need some help printing out my maze!

    Hi everyone, so I'm trying to get started on a maze project in which I have to manually input the columns, rows, etc. when I run the main method. As a result of inputting, the maze should then be outputted. However, I am having trouble trying to print my maze out because halfway through the input it just prints out a line of numbers that I inputted without completing itself.

    import java.util.Scanner;
    public class Maze{
      int A[] [];
      int c,r;
      //width
      //height
      int sx,sy;
      //start
      //square
      int gx,gy;
      //goal
      //square
     
      public static void main(String[] args){ //Main method to input/print maze
        Scanner sc = new Scanner(System.in);
        int c = sc.nextInt(); // columns
        int r = sc.nextInt(); // rows
        int sx = sc.nextInt(); 
        int sy = sc.nextInt(); 
        int gx = sc.nextInt();
        int gy = sc.nextInt();
        int[][] A = new int[c][r]; 
     
        for(int y = 0; y < r; y++)
        {
          for(int x = 0; x < c; x++)
          {
            A[x] [y] = sc.nextInt();
            System.out.println(A[x][y]);
          }
           System.out.println();
        }
      }
    }

    To give you a better idea, here is an example:

    If I inputted this format

    3 3 <----number of columns/rows I believe.
    0 0 <----starting x,y position
    2 1 <------goal coordinates
    0 0 1
    0 0 0 <----Actual square
    1 0 1

    The output should be:

    0 0 1
    0 0 0 <---------------(The maze!)
    1 0 1


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need some help printing out my maze!

    You have two problems.
    First, is that your call to System.out.println(A[x][y]); occurs immediately after you read in an int. Meaning that it will just print out the int you inputted immediately after you input it. If you want to take all of the input first, then print it all out after you are done inputting, you need to put your print statements in another loop after the one you are using to input your numbers.
    Second, if you want to print out 3 numbers per line, you should be using the System.out.print(...) method instead of the System.out.println(...) method, since the println method prints, and then moves to the next line, while the print method just prints. After you print out the 3 ints, you can call System.out.println(); to move to the next line.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help printing out my maze!

    Thanks! That worked! Just one more question and I shall get out of your hair, I have to implement a "findPath" class within the main Maze method in order for the program to..well find a path. I was wondering if my findPath method was any good at accomplishing this, and if so, how do I go about implementing it?

    public class findPath extends Maze{
     
      boolean solved;
      boolean wall = false;
     
      public boolean findPath(int row, int col){
        Boolean wall = false;
     
    //If attempted move is outside the maze
      if(row<0 || row>this.r-1 || col<0 || col>this.c-1)
        return false;
     
    //If current position is at the end of the maze
      if(row == this.r-1 && col == this.c-1)
      {
        wall = true;
        this.A[r][c] = '7';
        return wall;
      }
     
     
    //If already visited
      if(this.A[r][c] == '3')
        return false;
     
     
      this.A[r][c] = '3';
     
     
      wall = findPath(r+1, c); // down
      wall = findPath(r, c+1); // right
      wall = findPath(r-1, c); //up
      wall = findPath(r, c-1); //left
     
      this.A[r][c] = '7';
     
      return wall;
    }
    }

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need some help printing out my maze!

    That probably won't work, since you are overwriting wall several times here:
    wall = findPath(r+1, c); // down
    wall = findPath(r, c+1); // right
    wall = findPath(r-1, c); //up
    wall = findPath(r, c-1); //left
    ...
    return wall;
    which means that only the result of left will be returned. It is an interesting attempt at recursion (which would be considered the brute-force way and may not necessarily yield the shortest route), but not the *best* way of doing it recursively.
    I guess for sudo-code for the recursion, it should be something like:
    findPath(row,col):{
    if: OUTSIDE_MAZE, return FALSE
    if: END_OF_MAZE, set value to 7 (or whatever) and return TRUE
    if: is a WALL, return FALSE
    if: findPath(row,col-1)==TRUE, set value to 7 and return TRUE //go left
    else if: findPath(row+1,col)==TRUE, set value to 7 and return TRUE //go down
    else if: findPath(row,col+1)==TRUE, set value to 7 and return TRUE //go right
    else if: findPath(row-1,col)==TRUE, set value to 7 and return TRUE //go up
    else: return FALSE //dead end
    }
    Last edited by aussiemcgr; May 3rd, 2012 at 03:24 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help printing out my maze!

    How's this then? Although whenever I now run the findPath class it still just gives me the maze itself, I'm probably once again implementing it wrong.

    public class findPath extends Maze{
     
      boolean solved;
      boolean wall = false;
     
     public void findPath(int row, int col) {
        if (A[r][c] == '7') {
          solved = true;
          return;
        }
     
        A[r][c] = '3';
     
        if (A[r + 1][c] == ' ' ||  A[r + 1][c] == '7') {
          findPath(row + 1, col);
        }
        else if (A[r][c + 1] == ' ' || A[r][c + 1] == '7') {
          findPath(row, col + 1);
        }
        else if (A[r - 1][c] == ' ' || A[r - 1][c] == '7') {
          findPath(row -1, col);
        }
        else if (A[r][c - 1] == ' ' || A[r][c - 1] == '7') {
          findPath(r, c - 1);
        }
        else {
          wall = true;
          return;
        }
     
        if (wall) {
          wall = false;
          findPath(row, col);
        }
     
        if (solved) {
          A[r][c] = '+';
        }
      }
    }

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need some help printing out my maze!

    Three questions.
    1) Have you ever done recursion, and if so, how much?
    2) What are the variables: r and c?
    3) Are you calling the findPath(...) method before or after you print out the maze?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help printing out my maze!

    1) So far I'm attending college as a freshman majoring in Computer Science, so everything is really NEW to me(so excuse my obvious mistakes and lack of knowledge...I really try to learn!). I had a project that was based off of recursion, but it wasn't anything that made me a professional on the topic.
    2) What are the variables: R stands for "rows" and C for "columns" as shown in my Maze class.
    3) I want to say before the maze is printed out. I would assume that the values are being changed where the plus symbols symbolize the correct route whereas the 7 and 3(just randoms I made up, at first I had E and o) just signify the walls and such.

    Maybe I should elaborate on my assignment more. I have to create a maze that has the ability to backtrack once it runs into a wall to find a correct path out of the maze that I myself input. It should be able to really figure out any maze I input, but as a final assignment it's driving me sort of crazy(I haven't slept in a WHILE). So I just figured I would come on here and ask, so I'm very sorry if I come off as inexperienced.

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need some help printing out my maze!

    1. Computer Science? Hopefully your college is better than mine. I came in as a Computer Science major and then changed to Computer Engineering because the conceptual crap was kicking my ass. Ok, well recursion is both very difficult and very easy at the same time. One of the basic rules of a recursive method is that it *shouldn't* return a void (it can return void, but recursive methods rarely do), and there should be at least one exit case. In your case, your findPath() method should be returning a boolean. Setting the global wall variable doesn't help you, since that variable will continuously get changed by the multiple instances of the findPath() method. One of the great things about recursion is that it allows you to backtrace very easily (since it returns to a previous version of itself by design). How much do you understand about recursion?

    2. Where are the variables r and c created, and what are their values? You don't appear to change the values of r and c each time you recursively call the findPath() method, which means it will always run with the same arguments and always yield the same result (which could be an infinite recursion).

    3. Ok, with recursion, you should not have to worry about using the 7 or 3 numbers, just the plus.


    Let's have a look at what my sudo-code does with the example maze:
    1 0 1
    0 0 1
    1 0 0
    where the coordinate 0,1 is the start and 2,2 is the end.

    Here is a copy of my sudo-code with line numbers (for the sake of making the stuff below easier). I also edited it because I noticed it would create an infinite recursion:
    /*
     * type is an int, representing the last move. -1 indicates no last move, 0 indicates left,
     * 1 indicates down, 2 indicates right, and 3 indicates up
     */
    findPath(row,col,type):{ 
    #1 if: OUTSIDE_MAZE, return FALSE
    #2 if: END_OF_MAZE, set value to + (or whatever) and return TRUE
    #3 if: is a WALL, return FALSE
    #4 if: type!=2 and findPath(row,col-1)==TRUE, set value to + and return TRUE //go left
    #5 else if: type!=3 and findPath(row+1,col)==TRUE, set value to + and return TRUE //go down
    #6 else if: type!=0 and findPath(row,col+1)==TRUE, set value to + and return TRUE //go right
    #7 else if: type!=1 and findPath(row-1,col)==TRUE, set value to + and return TRUE //go up
    #8 else: return FALSE //dead end
    }
    }
    We start by sending the findPath(row,col,type) method our starting index, so we say:
    findPath(0,1,-1);
    Now, that we did that, let's look at how it would trace (sudo-trace, I guess). I'll say inst #,call # to indicate the instance of findPath() and the line we are executing:
    inst 1,call #1 -> evaluates to false, moves to #2
    inst 1,call #2 -> evaluates to false, moves to #3
    inst 1,call #3 -> evaluates to false, moves to #4
    inst 1,call #4 -> starts inst 2 - findPath(0,0,0)
    inst 2,call #1 -> evaluates to false, moves to #2
    inst 2,call #2 -> evaluates to false, moves to #3
    inst 2,call #3 -> evaluates to true, returns FALSE back to inst1
    inst 1,call #4 -> returned value is false, moves to #5
    inst 1,call #5 -> starts inst 3 - findPath(1,1,1)
    inst 3,call #1 -> evaluates to false, moves to #2
    inst 3,call #2 -> evaluates to false, moves to #3
    inst 3,call #3 -> evaluates to false, moves to #4
    inst 3,call #4 -> starts inst 4 - findPath(1,0,0)
    inst 4,call #1 -> evaluates to false, moves to #2
    inst 4,call #2 -> evaluates to false, moves to #3
    inst 4,call #3 -> evaluates to false, moves to #4
    inst 4,call #4 -> starts inst 5 - findPath(1,-1,0)
    inst 5,call #1 -> evalutes to true, returns FALSE back to inst 4
    inst 4,call #4 -> returned value is false, moves to #5
    inst 4,call #5 -> starts inst 6 - findPath(2,0,1)
    inst 6,call #1 -> evaluates to false, moves to #2
    inst 6,call #2 -> evaluates to false, moves to #3
    inst 6,call #3 -> evaluates to true, returns FALSE back to inst 4
    inst 4,call #5 -> returned value is false, moves to #6
    inst 4,call #6 -> evaluates to false, moves to #7
    inst 4,call #7 -> starts inst 7 - findPath(0,0,3)
    inst 7,call #1 -> evaluates to false, moves to #2
    inst 7,call #2 -> evaluates to false, moves to #3
    inst 7,call #3 -> evaluates to true, returns FALSE back to inst 4
    inst 4,call #7 -> returned value is false, moves to #8
    inst 4,call #8 -> returns FALSE back to inst 3
    inst 3,call #4 -> returned value is false, moves to #5
    inst 3,call #5 -> starts inst 8 - findPath(2,1,1)
    inst 8,call #1 -> evaluates to false, moves to #2
    inst 8,call #2 -> evaluates to false, moves to #3
    inst 8,call #3 -> evaluates to false, moves to #4
    inst 8,call #4 -> starts inst 9 - findPath(2,0,0)
    inst 9,call #1 -> evaluates to false, moves to #2
    inst 9,call #2 -> evaluates to false, moves to #3
    inst 9,call #3 -> evaluates to true, returns FALSE back to inst 8
    inst 8,call #4 -> returned value is false, moves to #5
    inst 8,call #5 -> starts inst 10 - findPath(3,1,1)
    inst 10,call #1-> evaluates to true, returns FALSE back to inst 8
    inst 8,call #5 -> returned value is false, moves to #6
    inst 8,call #6 -> starts inst 11 - findPath(2,2,2)
    inst 11,call #1-> evaluates to false, moves to #2
    inst 11,call #2-> evaluates to true, sets value at index 2,2 to + and returns TRUE to inst 8
    inst 8,call #6 -> returned value is true, sets value at index 2,1 to + and returns TRUE to inst 3
    inst 3,call #5 -> returned value is true, sets value at index 1,1 to + and returns TRUE to inst 1
    inst 1,call #5 -> returned value is true, sets value at index 2,2 to + and returns TRUE

    And there we are. The maze is solved. Now, read through all that and tell me what does and does not make sense about that trace. From there, we can figure out what aspects of recursion is causing you problems.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need some help printing out my maze!

    I will note that even with the above sudo-code, it will lead to infinite recursions if you have a large open area without walls. The only way to avoid that would be by marking your previous locations, like you were doing earlier.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Recursive Maze Help
    By chono in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 22nd, 2012, 08:02 PM
  2. Mouse Maze..Plz Help
    By TiT_4_tAt in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2012, 07:33 AM
  3. BackTracking in a maze
    By skoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2012, 10:23 AM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM