Results 1 to 14 of 14

Thread: 2D array help

  1. #1
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default 2D array help

    Hello,

    I'm making a Pacman java program. For the maze i'm using a 2d array.
    There are different kinds of integers in the 2D array.

    -1 for the wall
    0 for nothinh
    1 for a pill
    and a number for a bonus

    now I want to draw all those different things, like the wall and the pills.

    Only I dont know what I have to put in the Draw(Graphics g).

    Im now stuck at this point

    Java Code:
    public void Draw(Graphics g)
    	{
    		if(raster.equals(-1))
    		{
    			g.setColor(Color.DARK_GRAY);
    			g.fillRect(????, ????, 60, 60);
    		}
    		
    		if(raster.equals(0))
    		{
    			g.setColor(Color.LIGHT_GRAY);
    			g.fillRect(x, y, width, height);
    		}
    		
    		if(raster.equals(1))
    		{
    		
    		}
    		
    		if(raster.equals(BONUS))
    		{
    			
    		}
    At this point I'm stuck. I dont know how to get the maze working.
    What are the y and x integers. I had thought of the place where its in de 2D array and then double it with that number.

    Example, the wall is in the 2D array on place [8][2], I have to do:

    8 x X and 2 x Y.
    But if this works how do I get the places in the 2D array?
    Last edited by TGH; 12-23-2009 at 01:44 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Random thoughts from a hobbiest:

    You may be over-simplifying things a bit. For instance what is the orientation of a "wall"? up-down? left-right?

    Rather than using non-intuitive ints as constants, consider using enums.

    Consider drawing the non-changing portions of the game in a BufferedImage to help speed up rendering.

    If this were my program, I'd think OOPs and try to decompose it into various classes, solving each object's problems in isolation. The actual drawing of the GUI would be way down on my to-do list and would be something I'd tackle later, after first solving the non-GUI program logic (which is not trivial by any means). When I did start working on the graphics, I'd use Swing not AWT, I'd study the graphics tutorials and would see that I will need to override a JComponent's (such as a JPanel) paintComponent method, not the paint method.

    Corrections most welcome.

  3. #3
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    Well, you know pacman right.
    A wall is a square that pacman cant override.
    Its a simple drawing, but you didn't solved my problem.
    I want to do it this way, so can you solve the probem on my way?

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    This thread is related. I agree with Fubarable. OO is the way to go. Just don't over do it. Defining a scalar, for example, is unnecessary. Recently, I created a maze program in C++ using the chamber algorithm. My matrix was doubled in size and was odd. At any point, a cell can be determined as a wall depending on whether the indices where even or odd. In my maze, a cell was a wall if one of the indices where even and a column if both are even. If both were odd, then the cell is an opening in the maze. This resolves the problem of the wall direction. The matrix can be painted such that the walls appear thinner than the openings. An illusion. Well, that's what I did. ;)
    Last edited by tim; 12-24-2009 at 08:10 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by TGH View Post
    Well, you know pacman right.
    A wall is a square that pacman cant override.
    Its a simple drawing, but you didn't solved my problem.
    I want to do it this way, so can you solve the probem on my way?
    We can solve it that way. We just see that it will not bare fruit. Why settle for peanuts when you can have a coconut? ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  6. #6
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    I must do it the way I described.
    I'm now working with those Arrays, specified 2D arrays so it's better when I use this method.
    I surely know that there will be better/easier ways to do it, but I want to do it with an ArrayList...

    So is there an option to get the index of an integer, like -1? (In a 2D Array)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TGH View Post
    ...but you didn't solved my problem.
    And I'm quite sorry, but I feel obliged to give you what I feel is advice in your best interest. Whether or not you take our advice, and whether or not you solve your problem is of course your responsibility.

    Much luck.
    Last edited by Fubarable; 12-23-2009 at 08:02 PM.

  8. #8
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    Totally agree with that.

    That's why I now ask the question if there is an possibility to get the index of the 2D array, correspond with an integer, like -1...

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Arrays, be they 1-dimensional or multi-dimensional, start with an index of 0 and continue up to length - 1. I know of no such thing as an array index of -1. I'm not sure why you'd want this.

  10. #10
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    I've filled my Array with different numbers, -1, 0, 1, for different things.
    Now the outcome of an Array is -1, with that is an corresponded index from that array (Like [4][6]..)
    My question is how to get such an index if you know that the outcome is for example -1.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you want to find all occurrences of -1 in your array, then you will need to loop through the array with nested for loops

    Java Code:
    for (i = 0; i < myArray.length; i++) {
      for j = 0; j < myArray[i].length; j++) {
        if (myArray[i][j] == -1) {
          // do something
        }
      }
    }
    If this is not what you're trying to do, could you clarify your question a bit?

  12. #12
    TGH
    TGH is offline Member
    Join Date
    Nov 2009
    Posts
    32
    Rep Power
    0

    Default

    This is just EXACTLY what I'm looking for...

    Thanks!

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default

    Better (imho) use 9 (3x3) cells for one room; that way you can draw exactly what you want, e.g. a wall, a wall connected to another wall perpendicular to the south etc. etc. The cell in the middle represents the room itself (bonus, fruit, monster, nothing at all).

    kind regards,

    Jos

  14. #14
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Quote Originally Posted by JosAH View Post
    Better (imho) use 9 (3x3) cells for one room; that way you can draw exactly what you want, e.g. a wall, a wall connected to another wall perpendicular to the south etc. etc. The cell in the middle represents the room itself (bonus, fruit, monster, nothing at all).

    kind regards,

    Jos
    This is similar to my proposal:
    Quote Originally Posted by tim View Post
    ... My matrix was doubled in size and was odd. At any point, a cell can be determined as a wall depending on whether the indices where even or odd. In my maze, a cell was a wall if one of the indices where even and a column if both are even. If both were odd, then the cell is an opening in the maze. This resolves the problem of the wall direction. ...
    Except, if you have 9 (3x3) cells for each room, then you have duplicated wall segments between adjacent rooms. Unless, your rooms overlap. This makes things unnecessarily complicated. It's a simple case of looking at even and odd indices. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •