Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding "islands" of territories on a map recursively
    primarykey
    data
    text
    <p>I have a map consisting of hexagonal fields. On this map every field(let's call it Hex) belongs to some player and is defined by coordinates.</p> <p><strong>I need to get an <code>List&lt;List&lt;Hex&gt;&gt;</code> of a certain player's hexes. This means that if I input an array of all the hexes player owns, I need to get information about which ones are grouped together.</strong></p> <p>Let me illustrate:</p> <p><img src="https://i.stack.imgur.com/brol7.jpg" alt="Example of a hexagonal field owned by two players"></p> <p>The input -> all hexes of green player -> <code>List&lt;Hex&gt; playersHexes</code> is:</p> <blockquote> <p>{0.0; 0.1; 0.2; 1.0; 1.3; 2.1; 2.3}</p> </blockquote> <p>The output should be -> "islands" of green player: </p> <blockquote> <p>{0.0; 0.1; 0.2; 1.0} , {1.3; 2.3} , {2.1}</p> </blockquote> <p>How can I achieve this recursively? I am able to find neighbours for some Hex without problems, but that's only for one iteration for a certain hex.</p> <pre><code>//playersHexes are all the hexes that a player owns //map is the current map used - contains information about certain hexes //map is not a HashMap! It's my custom object.. private void findIslands(List&lt;Hex&gt; playersHexes, Map map) { List&lt;Hex&gt; island = new ArrayList&lt;Hex&gt;(); int curPos = 0; for(Hex hex : playersHexes){ island.add(hex); //remove the hex if it's allready gone through it? playersHexes.remove(curPos); List&lt;Hex&gt; neighbours = map.getNeighboursOf(hex); for(Hex neighbour : neighbours) { } //hexList is the output place - the target is to fill hexList with //islands(List&lt;Hex&gt;) of hexes.. this.hexList.add(curPos, island); curPos++; } } </code></pre> <p>Any help appreciated - pseudo code with the working recursive function is sufficient. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload