Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find islands in a randomly generated hexagonal map?

I'm programming a Risk like game in Codigniter and JQuery. I've come up with a way to create randomly generated maps by making a full layout of tiles then deleting random ones. However, this sometimes produces what I call islands.

In risk, you can only attack one space over. So if one player happens to have an island all to them self, they would never be able to loose.

I'm trying to find a way that I can check the map before the game begins to see if it has islands.

I have already come up with a function to find out how many adjacent spaces there are to each space, but am not sure how to implement it in order to find islands.

Each missing spot is also identified as "water."

I'm not allowed to use image tags: https://i.sstatic.net/DgI7c.gif

like image 669
J3nnings Avatar asked Dec 06 '25 17:12

J3nnings


1 Answers

There's a standard name for this problem but off the top of my head the following might work:

  • Pick any tile at random
  • Color it
  • Color its neighbours
  • Color its neighbours' neighbours
  • Color its neighbours' neighbours' neighbours, etc.

When you're done (i.e. when all neighbours are colored), loop through the list of all tiles to see whether there are any still/left uncolored (if so, they're an island).

like image 148
ChrisW Avatar answered Dec 08 '25 09:12

ChrisW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!