protected override void CheckingNewRevealed(Vector2Int currentnewrevealed) { //count the number of neighbores of this tile List <Vector2Int> newClosedHiddenNeighbors = new List <Vector2Int>(); ClosedHiddenNeighbores.Add(currentnewrevealed, newClosedHiddenNeighbors); for (int j = 0; j < Operators.Length; j++) { Vector2Int current = currentnewrevealed + Operators[j]; if (Flaged.Contains(current)) { Closed[currentnewrevealed]--; } else if (MinesweeperElementInfo.InBounds(current, BoardSize.x, BoardSize.y)) { if (Open.ContainsKey(current) || Safe.Contains(current) || Mine.Contains(current)) { newClosedHiddenNeighbors.Add(current); List <Vector2Int> clist = HiddenClosedRelations[current]; clist.Add(currentnewrevealed); } else if (!InvalidTiles[current.x, current.y] && !Closed.ContainsKey(current)) { Open.Add(current, 0); newClosedHiddenNeighbors.Add(current); List <Vector2Int> clist = new List <Vector2Int>(); HiddenClosedRelations.Add(current, clist); clist.Add(currentnewrevealed); } } } }
public void GetSteps(Node parent, Vector3Int target) { Closed[parent.position] = parent; if (Open.ContainsKey(parent.position)) { Open.Remove(parent.position); } n = GetNeighbours(parent.position).ToList(); foreach (var v in n) { if (!Closed.ContainsKey(v)) { Node node = new Node(); node.parent = parent.position; node.position = v; node.H = GetEffort(v, target); node.G = parent.G + GetEffort(parent.position, v); node.F = node.H + node.G; node.Distance = GetEffort(v, target); if (Open.ContainsKey(v)) { if (Open[v].F > node.F) { Open[v] = node; } } else { Open[v] = node; } if (v == target) { Closed[v] = node; return; } } } if (Open.Count > 0 && Open.Count < Range) { int min = Open.Min(x => x.Value.F); var nd = Open.Where(x => x.Value.F == min).First(); GetSteps(nd.Value, target); } else { return; } }
protected override void AddingMineCheck(Vector2Int newmine) { for (int j = 0; j < Operators.Length; j++) { Vector2Int current = newmine + Operators[j]; if (Closed.ContainsKey(current)) { if (Closed[current] > 0) { Closed[current]--; if (Closed[current] <= 0) { WhenRemovingClosedNeighborCheck(current); } } } } }
private IEnumerable <Vector3Int> Path(Vector3Int start, Vector3Int target) { path = new List <Node>(); if (Closed.ContainsKey(target)) { var current = Closed[target]; while (GetEffort(start, current.position) != 0) { path.Add(Closed[current.position]); current = Closed[current.parent]; } return(path.Select(x => x.position).Reverse()); } else { return(new List <Vector3Int>()); } }
/// <summary> /// Checks the whole board /// </summary> /// <param name="table"> The whole minesweeper table</param> public override void GetBoard(MinesweeperElementInfo[,] table) { List <Vector2Int> newpositions = new List <Vector2Int>(); for (int y = 0; y < table.GetLength(1); y++) { for (int x = 0; x < table.GetLength(0); x++) { MinesweeperElementInfo current = table[x, y]; Vector2Int currentpos = new Vector2Int(x, y); if (!current.hidden && current.value > 0) { if (!Closed.ContainsKey(currentpos)) { newpositions.Add(currentpos); } } } } for (int i = 0; i < newpositions.Count; i++) { for (int j = 0; j < Operators.Length; j++) { Vector2Int current = newpositions[i] + Operators[j]; //in bounds if (current.x >= 0 && current.y >= 0 && current.x < table.GetLength(1) && current.y < table.GetLength(0)) { if (table[current.x, current.y].hidden && !table[current.x, current.y].flaged && !Open.ContainsKey(current)) { Open.Add(current, 0); } } } Closed.Add(newpositions[i], table[newpositions[i].x, newpositions[i].y].value); } }
public IList <Node> Find(Node start, Node goal) { Start = start; Goal = goal; Parent.Add(start, start); Open.Enqueue(Start, Heuristic(Start)); while (Open.Count > 0) { Node node = Open.Dequeue(); if (node.Id == Goal.Id) { return(BuildPath()); } AddUpdate(Closed, node.Id, node); foreach (ProposedStep step in Grid.Neighbours(node)) { Node neighour = step.Node; if (!Closed.ContainsKey(neighour.Id)) { if (!Open.Contains(neighour)) { AddUpdate(GHistory, neighour.Id, double.PositiveInfinity); TryRemove(Parent, neighour); } double gOld = TryGetValue(GHistory, neighour.Id); // Compute Cost if (UseLineOfSight && LineOfSight(TryGetValue(Parent, node), neighour)) { var sParent = TryGetValue(Parent, node); var sParentCost = Cost(sParent, neighour); if (TryGetValue(GHistory, sParent.Id) + sParentCost < TryGetValue(GHistory, neighour.Id)) { AddUpdate(Parent, neighour, sParent); AddUpdate(GHistory, neighour.Id, TryGetValue(GHistory, node.Id) + sParentCost); } } else { if (TryGetValue(GHistory, node.Id) + step.Direction.Cost < TryGetValue(GHistory, neighour.Id)) { AddUpdate(Parent, neighour, node); AddUpdate(GHistory, neighour.Id, TryGetValue(GHistory, node.Id) + step.Direction.Cost); } } // Compute Cost End if (TryGetValue(GHistory, neighour.Id) < gOld) { if (Open.Contains(neighour)) { Open.Remove(neighour); } Open.Enqueue(neighour, TryGetValue(GHistory, neighour.Id) + Heuristic(neighour)); } } } } return(new List <Node>()); }
/// <summary> /// Iterates through 'currentnewrevealed', checking if 'currentnewrevealed' can be discarded, /// while adding new open tile /// </summary> /// <param name="currentnewrevealed"></param> protected virtual void CheckingNewRevealed(Vector2Int currentnewrevealed) { for (int j = 0; j < Operators.Length; j++) { Vector2Int current = currentnewrevealed + Operators[j]; if (Flaged.Contains(current)) { Closed[currentnewrevealed]--; } else if (MinesweeperElementInfo.InBounds(current, BoardSize.x, BoardSize.y)) { //i wanted to make the minesweeper and the solver independent from eachother, thats why i didn't simply passed the minesweeperelementinfo object at 'current' tile if (!InvalidTiles[current.x, current.y] && !Open.ContainsKey(current) && !Closed.ContainsKey(current) && !Safe.Contains(current) && !Mine.Contains(current)) { Open.Add(current, 0); } } } }