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

Thread: Maze Game

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Maze Game

    First post in the forum and i am a beginner as far as programming is concerned. The task is to read in a text file that has the size of the maze in the first line then maze follows. The out put im looking for is true that i did escape the maze but i am only seeing false or out of ArrayOutOfBoundsError when i am positive it is still inbound.
    Any feedback is very much appreciated.
    public class Maze
    {
       // public static char map[][];
        public Maze(int row, int col, char map[][])
        {
            Main.row = row;
            Main.col = col;
            Main.map = map;
     
            System.out.println(map[row][col]);
     
        }
     
    public boolean excape(int row, int col)
    {
       boolean exit = false;
     
       if(isValid(row, col))
       {
          Main.map[row][col] = Main.WALL;
     
          if (row == Main.map.length-1 && col == Main.map[0].length-1)
             exit = true; //the maze is solved
          else
          {
             exit = excape(row+1, col); //down
             if (!exit)
                exit = excape(row, col+1); //right
             if (!exit)
                exit = excape(row-1, col); //up
             if (!exit)
                exit = excape(row, col-1); //left         
          }
     
       }
       return exit;
     }
     
        public boolean isValid(int row, int col)
        {
            if(row <9 || col< 9 )
            {
     
            }
              return true;
        }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Maze Game

    It would be a bit more helpful if you posted where the ArrayOutOfBoundsException was occurring.
    It could be down to a number of reasons.

    The initial indices could be out of bounds as your isValid() method always returns true.
    Even if row and col are valid indices, when you start doing stuff like row+1, the index could easily fall off the end of the array.

    If you don't know what ArrayOutOfBoundsException means, then check out:
    ArrayIndexOutOfBoundsException (Java 2 Platform SE v1.4.2)
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Maze Game

    Enter input file name: maze1.txt
    Enter X position: 1
    Enter Y position: 1
    Start Position: 1,1


    * *******
    * * *
    * ***** *
    * * * *
    * * *** *
    * * *
    *** * * *
    * * *
    ******* *

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
    at ch_13_maze.Maze.excape(Maze.java:28)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Maze.excape(Maze.java:34)
    at ch_13_maze.Main.main(Main.java:67)
    Java Result: 1

Similar Threads

  1. java maze move charcter?
    By coolcool1980 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2011, 02:18 PM
  2. Basic Java maze game
    By sciontc1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2011, 10:18 AM
  3. need help creating a maze program
    By helpzor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 03:12 PM
  4. Recursion Maze
    By sman1234 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2010, 11:40 AM
  5. Problem in recursion for Solving Maze Recursively program
    By _Coder1985 in forum Algorithms & Recursion
    Replies: 1
    Last Post: April 29th, 2009, 04:37 AM

Tags for this Thread