private void CreateBranch(NodeVO startNode, List <NodeVO> edgeNodes) { NodeVO randomNeighbour; NodeVO currentNode = startNode; do { //1. if node exists in edge nodes, remove it if (edgeNodes.Contains(currentNode)) { edgeNodes.Remove(currentNode); } //1.1 append new edge nodes List <NodeVO> notProcessedNeighbours = GetNotProcessedNeighboursOf(currentNode); foreach (NodeVO nodeData in notProcessedNeighbours) { if (!edgeNodes.Contains(nodeData)) { edgeNodes.Add(nodeData); } } currentNode.AddFlag(NodeVO.PROCESSED); //2. go to random direction and get a neighbour randomNeighbour = GetRandomNeighbour(currentNode, false); //3. if it exists (didn't got a dead end) - expand the maze if (randomNeighbour != null) { Link(currentNode, randomNeighbour); //3.1 attach it to tree Merge(currentNode, randomNeighbour); //3.2 process it on next loop entry currentNode = randomNeighbour; } else { if (deadEnds.Count > 0 && (currentNode.GetDistance() > deadEnds [0].GetDistance())) { deadEnds.Insert(0, currentNode); } else { deadEnds.Add(currentNode); } } } while (randomNeighbour != null); }