Results 1 to 14 of 14
Thread: 2D array help
- 12-23-2009, 01:41 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
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
At this point I'm stuck. I dont know how to get the maze working.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)) { }
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.
-
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.
- 12-23-2009, 02:21 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
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?
- 12-23-2009, 02:28 PM #4
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.
- 12-23-2009, 02:32 PM #5
- 12-23-2009, 07:35 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
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)
-
- 12-24-2009, 12:06 AM #8
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
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...
-
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.
- 12-24-2009, 12:16 AM #10
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
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.
-
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
If this is not what you're trying to do, could you clarify your question a bit?Java Code:for (i = 0; i < myArray.length; i++) { for j = 0; j < myArray[i].length; j++) { if (myArray[i][j] == -1) { // do something } } }
- 12-24-2009, 02:38 PM #12
Member
- Join Date
- Nov 2009
- Posts
- 32
- Rep Power
- 0
This is just EXACTLY what I'm looking for...
Thanks!
- 12-24-2009, 04:33 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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
- 12-24-2009, 08:08 PM #14
This is similar to my proposal:
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
-
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
Array length and printing out uninitialized array.
By nicolek808 in forum New To JavaReplies: 4Last Post: 09-10-2009, 09:12 AM -
How to transfer 1D array in JAVA to 3D array in C
By fishwater00 in forum New To JavaReplies: 0Last Post: 07-31-2009, 06:24 PM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks