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; } }
/// <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); } } } }
/// <summary> /// Find all deterministic mines and safe tiles of a single iteration /// </summary> public void FindMinesandSafeTiles() { List <Vector2Int> newmines = new List <Vector2Int>(); //Find 'Mine' tiles foreach (var item in Closed) { List <Vector2Int> localmine = new List <Vector2Int>(); for (int i = 0; i < Operators.Length; i++) { Vector2Int newpos = item.Key + Operators[i]; if (Open.ContainsKey(newpos)) { localmine.Add(newpos); } } if (item.Value == localmine.Count) { newmines.AddRange(localmine); } } List <Vector2Int> newmines2 = new List <Vector2Int>(); for (int i = 0; i < newmines.Count; i++) { Vector2Int current = newmines[i]; if (Open.Remove(current)) { Mine.Add(current); newmines2.Add(current); } } //Find 'Safe' tiles for (int i = 0; i < newmines2.Count; i++) { AddingMineCheck(newmines2[i]); } }
/// <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); } }