private GameObject CaptureBiggestClusterOptimal(int targetTeam, List <GameObject> nodesList) { GameObject potentialBestNode = null; int bestClusterSize = 0; // For each node that belongs to the current player foreach (GameObject candidateNode in nodesList) { if (candidateNode.GetComponent <Node>().owner == team) { int clusterSize = TilesHelper.GetSizeOfTargetCluster(candidateNode, targetTeam, nodesList); if (clusterSize > bestClusterSize) { potentialBestNode = candidateNode; bestClusterSize = clusterSize; bool cont = true; int limit = 32; while (cont && limit > 0) { cont = false; limit--; HashSet <GameObject> nodesBehind = TilesHelper.GetNodeBehind(potentialBestNode, nodesList); foreach (GameObject nodeBehind in nodesBehind) { clusterSize = TilesHelper.GetSizeOfTargetCluster(nodeBehind, targetTeam, nodesList); if (clusterSize >= bestClusterSize) { potentialBestNode = nodeBehind; bestClusterSize = clusterSize; cont = true; } } } } } } return(potentialBestNode); }