Question: so im making the board game GO for javascript and im having trouble counting the territories. If you didnt know, GO is like a complex checkers where you place stones horizontally and vertically instead of diagonally. A territory comes when the game has ended and you have made a border of your stones (the board edges count as anyones). 

[![territory](https://i.stack.imgur.com/nIizS.png)](https://i.stack.imgur.com/nIizS.png)

So the image above is what i have so far.

White = player 1, Black = player 2, Green = territory

So the green only comes at the end of the game. The game has now finished, black has control of the top left corner of the board taking 2 prisoners. The group inside has been identified and coloured in green. Problem is, how do i know what player the green territory belongs to? 

Any human can see black owns all sides/ border of the territory (remember the board edges are anyones). It starts to get hard when there are prisoners inside. I could just check every cell adjacent to every green cell. If there is black and white, its not a territory but that wouldnt work if there is prisoners.

One way im thinking might be an option is to try identify the border going around it. If i can do that i can easily check whos territory it is. Calculating the border peices would be easy with a square but a circle or any morphed shape? im not sure.

Any help is appriciated :)

Answer: 
I managed to do this very effectively in the end using my own algorithm. 

It will first identify the territory in question and get the lowX, lowY, highX, highY. So in this case it would be 0, 0, 5, 5 respectivly. I then do a for loop like this:

```
    for (var j = lowX; j < highX + 1; j++)
    {       
        var lowColumnY = null;
        var highColumnY = null;

        for (var k = 0; k < territories[i].length; k++)
        {               
            if (territories[i][k].x == j)
            {
                if ((lowColumnY == null) || (territories[i][k].y < lowColumnY))
                {
                    lowColumnY = territories[i][k].y;
                    console.log(lowColumnY);
                }

                if ((territories[i][k].y > highColumnY) || (highColumnY == null))
                {
                    highColumnY = territories[i][k].y;
                }
            }                               
        }

```

So that scrolls through all the columns of the territory and works out the low and high point which are the first 2 variables declared. I then repeat the for loop for the Y and in the end im left with every coordinate of the border. I have demonstrated this by drawing red circles so its easy to visualist and as you can see it doesnt interfere with any prisoners.

[![territory](https://i.stack.imgur.com/jJsIU.png)](https://i.stack.imgur.com/jJsIU.png)

So lastly i just check each colour of the stones where the red circles are. If there is any conflicting colours, it cant be a territory. As in this case the only colour was black, we know its a black territory.
Im going to post an idea myself and try this method. So i will first get the most left column in the territory, in this case its index 0. i will use a for loop to scroll through every column finding out the most up and the most down stone in the column. So first is column 0. the most up stone is 0 and the most down is 5. (ignore the board markings). As that must be the top and bottom border for this shape I will check the stones above the top one and below the bottom one and note them down. 

As the top of stone 0,0 is the edge of the board, its marked "both". Now for the bottom, below it will be marked "black". We now have an owner of this territory. I will repeat this for all the columns and then all the rows and if it gets any conflicting owner, i will break the loop as it cant be a territory.

I just thought of this in my head and im going to try it now