private Star FindByDirection(List<Star> source, Star item, Direction dir) { int x = item.X; int y = item.Y; switch (dir) { case Direction.Up: x -= 1; break; case Direction.Down: x += 1; break; case Direction.Left: y -= 1; break; case Direction.Right: y += 1; break; default: throw new ArgumentOutOfRangeException(); } return source.Locate(x, y); }
private Button FindButton(Star star) { return buttons.Find(b => star.Equals(b.Tag)); }
public List<Star> FindNeighbour(Star star) { List<Star> result = new List<Star>(); Stack<Star> stack = new Stack<Star>(); List<Star> tracked = new List<Star>(); stack.Push(star); while (stack.Count > 0) { Star item = stack.Pop(); if (!result.Contains(item)) { result.Add(item); } if (!tracked.Contains(item)) { tracked.Add(item); } foreach (Direction dir in Enum.GetValues(typeof(Direction))) { Star s = FindByDirection(source, item, dir); if (s != null && s.Color == item.Color && !tracked.Contains(s)) { stack.Push(s); } } } return result; }