aspose file tools*
The moose likes Beginning Java and the fly likes Moving within a 2 dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Moving within a 2 dimensional array" Watch "Moving within a 2 dimensional array" New topic
Author

Moving within a 2 dimensional array

Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
So i have generated some code that lets me make a map of images randomly.

I have additional code i cant quite perfect to randomly generate a river through this same array.

What im having constant trouble with is how do i keep my code inside my array? I keep setting my bounds to 0 and 9 (a 10x10 array) and i constantly get errors when my code goes outside of these bounds despite my code:



This is part of a large nested switch statement that is looped through to make the river. Im trying to basically turn it away from the edges of the map (the array bounds) but it just ignores these statements and proceeds to go out of bounds.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

scott kimberly wrote:What im having constant trouble with is how do i keep my code inside my array? I keep setting my bounds to 0 and 9 (a 10x10 array) and i constantly get errors when my code goes outside of these bounds despite my code:

Well, you know what the bounds are; just throw an error if a move in a particular direction would cause you to go out of bounds.

You may find this discussion useful. In his case, OP was looking for a maze-traversal, but the two things are closely related.

Winston

[Edit] PS: Another alternative might be to do the move in a try...catch block and back out if you get an ArrayIndexOutOfBoundsException. Not sure I like that style too much though; smacks of using Exceptions to alter program flow, which is generally not a good idea.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3658
    
  15
What are you trying to do on lines 5 and 13 ? You set a value for the riverChoice1 variable which you immediately overwrite on lines 7 and 15 respectively.


Joanne
Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
Joanne Neal wrote:What are you trying to do on lines 5 and 13 ? You set a value for the riverChoice1 variable which you immediately overwrite on lines 7 and 15 respectively.


Yea i just realize that my code did that so im trying to fix it now. I adjusted it and now it works more often then not.

Now i just have to figure out how to keep it from going back over itself. (i have 0 clue as to how ill accomplish this)

Winston: Thanks for the link, that will probably be useful to me later on. I added in another part to my If..else if statement that catches what the original parts missed. I was only testing to see if it was outside of the bounds and something else and never checked if it was entirely within the bounds which im pretty sure is what was giving me issues.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to the Ranch

You do not appear to have an array in your code. Do you mean moving around a 2D display?
Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
Campbell Ritchie wrote:Welcome to the Ranch

You do not appear to have an array in your code. Do you mean moving around a 2D display?


I was reluctant to post all of my code. The array is made here:



That code makes my base map. It fills the array with images of either a forest, mountain, or grass.

It then takes map list into this method to draw the images to the screen.



I have no idea how efficient this way of doing it is, but it works for the moment and thats what i was more concerned with.

The river code is a series of 4 cases of a switch statement that each have their own 3 cases of a switch statement. The 3 lower lever cases make the river go Left, Up, Right or some other variant of Left, Up, Right, Down (always missing one though so it never goes directly back on itself)

Im having issues with it drawing back on itself and now im starting to wonder about my efficiency. Ive heard that polymorphism can make my switch statements better but i dont know how id do that since theyre nested and i have large if-else if loops governing which of the 4 main cases my code goes to.

It makes sense to me but its confusing to everyone else, so im probably doing something wrong :/
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Scott Kimberly wrote:I have no idea how efficient this way of doing it is, but it works for the moment and thats what i was more concerned with.

Well I'm no expert on this stuff, but it seems to me that it might only be worth redrawing the image if it's changed. I know that there are quite a lot of fancy XOR-like algorithms for it, but I'm not sure if you need them (or if you wouldn't actually be duplicating Swing's own by implementing them).

Winston
Scott Kimberly
Greenhorn

Joined: Mar 04, 2012
Posts: 25
Winston Gutkowski wrote:
Scott Kimberly wrote:I have no idea how efficient this way of doing it is, but it works for the moment and thats what i was more concerned with.

Well I'm no expert on this stuff, but it seems to me that it might only be worth redrawing the image if it's changed. I know that there are quite a lot of fancy XOR-like algorithms for it, but I'm not sure if you need them (or if you wouldn't actually be duplicating Swing's own by implementing them).

Winston


Thats actually more or less what i do. The array is initially filled with images and then my switch statement block goes in an individually overwrites parts of the array with parts of my river. Id be real surprised if the way that part of my code is done isnt efficient. Its every other aspect of my code that probably isnt, like my giant nested switch statement blocks and the if-else blocks governing each of them.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Scott Kimberly wrote:Thats actually more or less what i do. The array is initially filled with images and then my switch statement block goes in an individually overwrites parts of the array with parts of my river. Id be real surprised if the way that part of my code is done isnt efficient.

The only way to check that is to test it. Thoroughly. And the usual rule of thumb is: if it's fast enough, don't sweat it.

Its every other aspect of my code that probably isnt, like my giant nested switch statement blocks and the if-else blocks governing each of them.

Sounds to me like you're more worried about complexity than speed then; and the simplest way to solve that? Break it up into methods.

Winston
 
Don't get me started about those stupid light bulbs.
 
subject: Moving within a 2 dimensional array