void OnButtonClick(Button uiButton, Minesweeper.MinesweeperNode graphNode) { if (!isLosing) // again, please ekt do this better { graph.UncoverSafeNodes(graphNode); UpdateMatrixRendering(); // sconfitta if (graphNode.mined) { isLosing = true; ShowAllBombs(); StartCoroutine(FadeResetMatrix()); return; } CheckSolution(); } }
public void AddLink(MinesweeperNode from, MinesweeperNode to) { if (links.ContainsKey(to) && links[to].Contains(from)) { return; } // NON-directed link. if (!links.ContainsKey(from)) { links.Add(from, new List <MinesweeperNode>()); } if (!links.ContainsKey(to)) { links.Add(to, new List <MinesweeperNode>()); } links[from].Add(to); links[to].Add(from); }
public void UncoverSafeNodes(MinesweeperNode source) { if (source.flag) { return; } source.covered = false; Stack <MinesweeperNode> frontier = new Stack <MinesweeperNode>(); HashSet <MinesweeperNode> visited = new HashSet <MinesweeperNode>(); frontier.Push(source); while (frontier.Count != 0) { MinesweeperNode current = frontier.Pop(); if (!current.mined && !current.flag) { if (current.nearMines == 0) { if (!visited.Contains(current)) { visited.Add(current); current.covered = false; foreach (MinesweeperNode near in links[current]) { frontier.Push(near); } } } else { current.covered = false; } } } }
public void SetFlag(MinesweeperNode source) { source.flag = true; }
void Start() { for (var i = 0; i < matrix.Length; ++i) { matrix[i] = new Minesweeper.MinesweeperNode[COLS]; buttonsMatrix[i] = new Button[COLS]; } string pattern = @"^Button_(\d+)_(\d+)$"; var buttons = matrixRoot.GetComponentsInChildren <Button>(); foreach (var button in buttons) { var name = button.gameObject.name; var res = Regex.Split(name, pattern); int row = int.Parse(res[1]); int col = int.Parse(res[2]); var graphNode = new Minesweeper.MinesweeperNode(string.Format("{0}_{1}", row, col)); var buttonExtraState = button.gameObject.GetComponent <BottoneCampoMinato>(); if (buttonExtraState != null) { switch (buttonExtraState.state) { case CellState.Mined: graphNode.mined = true; break; default: break; } } matrix[row][col] = graphNode; buttonsMatrix[row][col] = button; button.onClick.AddListener(() => { OnButtonClick(button, graphNode); }); } for (var i = 0; i < matrix.Length; ++i) { for (var j = 0; j < matrix[i].Length; ++j) { if (matrix[i][j] != null) { if (i != 0 && matrix[i - 1][j] != null) // prev row exists { AddConnection(i, j, i - 1, j); // top, center if (j != 0 && matrix[i - 1][j - 1] != null) { AddConnection(i, j, i - 1, j - 1); // top, left } if (j < matrix[i].Length - 1 && matrix[i - 1][j + 1] != null) { AddConnection(i, j, i - 1, j + 1); // top, right } } if (i < matrix.Length - 1 && matrix[i + 1][j] != null) // next row exists { AddConnection(i, j, i + 1, j); // bottom, center if (j != 0 && matrix[i + 1][j - 1] != null) { AddConnection(i, j, i + 1, j - 1); // bottom, left } if (j < matrix[i].Length - 1 && matrix[i + 1][j + 1] != null) { AddConnection(i, j, i + 1, j + 1); // bottom, right } } if (j < matrix[i].Length - 1 && matrix[i][j + 1] != null) { AddConnection(i, j, i, j + 1); // right, center } if (j != 0 && matrix[i][j - 1] != null) { AddConnection(i, j, i, j - 1); // left, center } } } } graph.ComputeNearMines(); }