private List <NodeLink> CreateNodeLinkWithWeight(NodeLink nodeLink, List <Node> neighbours) { List <NodeLink> links = new List <NodeLink>(); bool isForkChain = neighbours.Count > 1; Node prev = nodeLink.current; int nextWeight = nodeLink.weight + 1; foreach (Node node in neighbours) { NodeLink link = new NodeLink(prev, node, nodeLink, nextWeight); link.Link(nodeLink.linkIdChain, isForkChain ? ++m_masterId : -1); node.nodeLinks.Add(link); links.Add(link); } return(links); }
public List <Tile> FindLongestMatchPath(Tile startTile) { List <Tile> possibleTiles = FindPossibleLinkedTiles(startTile); if (possibleTiles.Count <= 2) { return(possibleTiles); } int maxWeight = possibleTiles.Count - 1; NodeLink maxWeightLink = null; List <Node> nodes = WrapTileToNodeGraph(possibleTiles); Node startNode = nodes.Find(n => n.tile == startTile); NodeLink initLink = new NodeLink(null, startNode, null, 0); initLink.Link(new List <int>(new int[] { 0 })); startNode.nodeLinks.Add(initLink); Stack <NodeLink> linkQueue = new Stack <NodeLink>(); linkQueue.Push(initLink); int roundCount = 0; while (linkQueue.Count > 0 && maxWeightLink == null && roundCount++ < 1000) { NodeLink nextLink = linkQueue.Pop(); List <Node> unconnectedMinNeighbours = FindNodesWithMinimulNeighbours(nextLink); if (unconnectedMinNeighbours.Count > 0) { List <NodeLink> nodeLinks = CreateNodeLinkWithWeight(nextLink, unconnectedMinNeighbours); nodeLinks.ForEach(link => { linkQueue.Push(link); if (link.weight == maxWeight) { maxWeightLink = link; } }); } else { if (nextLink.prev != null && nextLink.prevLink != null) { linkQueue.Push(nextLink.prevLink); } } } if (maxWeightLink == null) { maxWeight = 0; foreach (Node node in nodes) { foreach (NodeLink link in node.nodeLinks) { if (link.weight > maxWeight) { maxWeightLink = link; maxWeight = link.weight; } } } } List <Tile> bestPath = new List <Tile>(); while (maxWeightLink.prev != null) { bestPath.Add(maxWeightLink.current.tile); maxWeightLink = maxWeightLink.prevLink; } bestPath.Add(maxWeightLink.current.tile); bestPath.Reverse(); //StartCoroutine(SlowHighLight(bestPath)); return(bestPath); }