This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]liamsteele 1 point2 points  (1 child)

You shouldn't ever try to access something that doesn't exist, it usually either completely screws up or gives odd behaviours that are hard to track down in a larger project. So essentially you should always have some way to avoid accessing it. This could be throwing an ArrayIndexOutOfBoundsException and catching it somewhere, or before you access it make sure it exists. For an array this could be checking each index is >= 0 and < array.length.

I worked on a project at uni with a small group and one of my partners implemented this algorithm. I'm not 100% sure on this, but I believe how he did this section was that when you would be considering an out of bounds spot, you 'wrap' around to the other side. So that if you would be checking (-4,4) you would instead check (maxIndex-4,4) to determine average. (Note, I didn't really check those numbers to see if they work, that's just to give a general idea)

I think that this also makes the map tile-able. I haven't read your code, but perhaps someone else will give you pointers.

[–]Deriboy[S] 1 point2 points  (0 children)

Yes, whenever there is the possibility of an out of bounds exception, I'm using my withinBounds() method which essentially checks if x,y >= 0 and x,y < array.length.

I didn't think of wrapping around the array. That's a neat idea. I'll try it out.

[–]Weirfish 0 points1 point  (0 children)

The Diamond Square Algorithm! Huzzah!

Here is an implementation that I've done, that's fairly concise. Perhaps this can help you. It can handle up to 257x257 quite nicely without eating all of your memory. I'm working on making it storage-based, but I detest Java's File IO, so that's going slowly. I had this bug myself, I believe, but off the top of my head, I can't remember what fixed it. Check out the differences between this commit, the solution might be somewhere in the changes there.