A Random Sea

hi,

im currently studying algorithms and am hoping to create a very simple program that makes a random sea. i have having some trouble with it however - i know what i want to do but not sure exactly how to do it.

Currently i have a grid of squares ( 8 by 8 - using an array), all classed as land, and these are called square1 ...square 64. At this stage i want to make 10 squares "ocean".

1) the program randomnly picks on of the outer squares,

2) this is where i am at now. Something i could use is as follows:

Final int TotalOceanCount = 10;

for (int OceanCount = 0 ; OceanCount < TotalOceanCount ; OceanCount++) {

....

i am stuck on the next area. what i want to do is this:

***Choose a random square that touches a current Ocean square and convert it from Land to Ocean*** Using the above loop will limit the number of Ocean squares to 10.

Does anybody have suggestions on what topics i should look at in order to be able to complete this simple problem? im guessing the solution is basic and would be a few lines long, but a few lines is a lot of lines if you dont know where to look :-)

thankyou in advance,

Mark Scott

[1215 byte] By [Only0nea] at [2007-10-1 6:03:50]
# 1
You could simply just choose a square. And if it doesn't touch a current ocean sqare then discard it and try again.You can limit this by using a sub-square that bounds all current ocean squares. You then add 1 to each bound and choose a square in it. You still have to do the
jschella at 2007-7-9 14:20:57 > top of Java-index,Other Topics,Algorithms...
# 2

sounds a lot like solving the maze problem, but without walls. You might look there for strategies. Personally, I would just pick (randomly) from a list of valid ocean squares (i.e. those squares adjacent to an ocean square) until I hit the desired number of squares. If you want to make it really cool, you could use an AI (swarm comes to mind) technique to produce realistic looking coastlines. Of corse, this is overkill for an 8x8 world, but might look cool in a larger area.

Brenta at 2007-7-9 14:20:57 > top of Java-index,Other Topics,Algorithms...
# 3

I would do the following:

Create an empty list of sea candidates (of squares that may turn to sea)

Then start as you did by creating one sea square.

Start loop.

Update the list of candidates by adding all squares adjacent to last created sea square that are not already sea squares.

Pick a random square from the list of candidates, turn it into sea, and remove it from the list.

End loop.

Ragnvald_id2a at 2007-7-9 14:20:57 > top of Java-index,Other Topics,Algorithms...