// find matches at a node public List <Item> FindMatches(FIND_DIRECTION direction = FIND_DIRECTION.NONE, int matches = 3) { var list = new List <Item>(); var countedNodes = new Dictionary <int, Item>(); if (item == null || !item.Matchable()) { return(null); } if (direction != FIND_DIRECTION.COLUMN) { countedNodes = FindMoreMatches(item.color, countedNodes, FIND_DIRECTION.ROW); } if (countedNodes.Count < matches) { countedNodes.Clear(); } if (direction != FIND_DIRECTION.ROW) { countedNodes = FindMoreMatches(item.color, countedNodes, FIND_DIRECTION.COLUMN); } if (countedNodes.Count < matches) { countedNodes.Clear(); } foreach (KeyValuePair <int, Item> entry in countedNodes) { list.Add(entry.Value); } return(list); }
// helper function to find matches Dictionary <int, Item> FindMoreMatches(int color, Dictionary <int, Item> countedNodes, FIND_DIRECTION direction) { if (item == null || item.destroying) { return(countedNodes); } if (item.color == color && !countedNodes.ContainsValue(item) && item.Matchable() && item.node != null) { countedNodes.Add(item.node.OrderOnBoard(), item); if (direction == FIND_DIRECTION.ROW) { if (LeftNeighbor() != null) { countedNodes = LeftNeighbor().FindMoreMatches(color, countedNodes, FIND_DIRECTION.ROW); } if (RightNeighbor() != null) { countedNodes = RightNeighbor().FindMoreMatches(color, countedNodes, FIND_DIRECTION.ROW); } } else if (direction == FIND_DIRECTION.COLUMN) { if (TopNeighbor() != null) { countedNodes = TopNeighbor().FindMoreMatches(color, countedNodes, FIND_DIRECTION.COLUMN); } if (BottomNeighbor() != null) { countedNodes = BottomNeighbor().FindMoreMatches(color, countedNodes, FIND_DIRECTION.COLUMN); } } } return(countedNodes); }