public MapGridObject.Type RevealGridPosition(MapGridObject mapGridObject) { if (mapGridObject == null) { return(MapGridObject.Type.Empty); } // Reveal this object mapGridObject.Reveal(); // Is it an Empty grid object? if (mapGridObject.GetGridType() == MapGridObject.Type.Empty) { // Is Empty, reveal connected nodes // Keep track of nodes already checked List <MapGridObject> alreadyCheckedNeighbourList = new List <MapGridObject>(); // Nodes queued up for checking List <MapGridObject> checkNeighbourList = new List <MapGridObject>(); // Start checking this node checkNeighbourList.Add(mapGridObject); // While we have nodes to check while (checkNeighbourList.Count > 0) { // Grab the first one MapGridObject checkMapGridObject = checkNeighbourList[0]; // Remove from the queue checkNeighbourList.RemoveAt(0); alreadyCheckedNeighbourList.Add(checkMapGridObject); // Cycle through all its neighbours foreach (MapGridObject neighbour in GetNeighbourList(checkMapGridObject)) { if (neighbour.GetGridType() != MapGridObject.Type.Mine) { // If not a mine, reveal it neighbour.Reveal(); if (neighbour.GetGridType() == MapGridObject.Type.Empty) { // If empty, check add it to queue if (!alreadyCheckedNeighbourList.Contains(neighbour)) { checkNeighbourList.Add(neighbour); } } } } } } if (IsEntireMapRevealed()) { // Entire map revealed, game win! OnEntireMapRevealed?.Invoke(this, EventArgs.Empty); } return(mapGridObject.GetGridType()); }
public void RevealEntireMap() { for (int x = 0; x < grid.GetWidth(); x++) { for (int y = 0; y < grid.GetHeight(); y++) { MapGridObject mapGridObject = grid.GetGridObject(x, y); mapGridObject.Reveal(); } } }
public MapGridObject.Type RevealGridPosition(MapGridObject mapGridObject) { mapGridObject.Reveal(); if (mapGridObject.GetGridType() == MapGridObject.Type.Empty) { List <MapGridObject> alreadyCheckedNeighbourList = new List <MapGridObject>(); List <MapGridObject> checkNeighbourList = new List <MapGridObject>(); checkNeighbourList.Add(mapGridObject); while (checkNeighbourList.Count > 0) { MapGridObject checkMapGridObject = checkNeighbourList[0]; checkNeighbourList.RemoveAt(0); alreadyCheckedNeighbourList.Add(checkMapGridObject); foreach (MapGridObject neighbour in GetNeighbourList(checkMapGridObject)) { if (neighbour.GetGridType() != MapGridObject.Type.Mine) { neighbour.Reveal(); if (neighbour.GetGridType() == MapGridObject.Type.Empty) { if (!alreadyCheckedNeighbourList.Contains(neighbour)) { checkNeighbourList.Add(neighbour); } } } } } } if (IsEntireMapRevealed()) { //win game OnEnTireMapRevealed?.Invoke(this, EventArgs.Empty); } return(mapGridObject.GetGridType()); }