Array Passing/Manipulation


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Array Passing/Manipulation

  1. #1
    Join Date
    Feb 2004
    Posts
    1

    Question Array Passing/Manipulation

    Disclaimer: Java Newbie On Board

    Hey,
    I'm creating a 3D Rat In The Maze problem, but Im creating a 2D version just for starters right now.
    Anyway, I created my 2d array named maze with the values 0, 1, 2 and 5 where...

    0 = a valid path
    1 = a wall
    2 = stairs up/down (not necessary since i'm doing just a 2d version right now)
    5 = the starting point

    I left my code as is, but it's incorrect...I'm not too sure on a few things.

    Code:
    import javax.swing.*;
    
    public class Rat
    {
    	public static void main(String [] args)
    	{
    		int maze[][] = { {1,1,1,1,1}, {1,2,0,5,1}, {1,1,0,1,1}, {1,2,0,2,1}, {1,1,0,1,1} };
    		startpoint(maze);
                    // traverse_maze()  Is this needed???????
    		System.exit(0);
    	}
    
    	//function startpoint determines where the rat's initial position is
    	public static void startpoint(int a[][])
    	{
    		boolean flag = true;
    					
    		for( int row = 0; row < a.length && flag; row++)
    		{
    			for(int column = 0; column < a[row].length && flag; column++)
    			{
    				if( a[row][column] == 5 )
    				   new int x[][] = a[row][column];  //this line was proved invalid
                                        flag = false;
    			}
    		
    		} 
    		System.out.println( x );  //incorrect
                    traverse(x);
    	}
    	
    }
    Anyway, I created a function named starting point(int a[][]) that takes in the whole maze and scans it to search for the starting point. Once it finds the starting point, I want that specific row/column to be called up in maze[][], where I can begin traversing the maze.

    Can I use another function in startingpoint() like, traverse(array) that will take that starting point found in maze?

    If traverse works, will the passed array argument be set at the starting point, and will it include the rest of the 2d maze as well? Do I need a traverse() function, or can I do all of this in startingpoint()? How can I do this?

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    i have a little confusion here...

    why is your maze implemented inside a class called Rat? remember that we are sort of modeling the real world, and it is not often that we see a little furry rat, that has-a maze inside it..

    infact, more likely that there is a Maze that has-a Rat

    so you have declared your maze in the constructor, but it will vanish when the constructor finishes... what will you do then?

    im not too clear on what traverse does, either...

    for a start, get the objects right.. a Maze should have as a class-variable, an int[][] that describes the layout.

    Maze may also have a Rat object, and Rat will have a locationX and location Y (and later a z?).. you can scan the maze for the start point, and then put the rat at that point by setting it's location variables...

    --
    i know its all very hard to think about at the moment, it was difficult for me to write a CD Player when i first started java.. so many things didnt make sense.. persevere though!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center