/// <summary> /// Open given tile /// Change closed status of userdBoard to Empty or Numerical /// Recursive function - open all empty cells adjacent to given empty cell /// </summary> /// <param name="userBoard">The board on which it will open the tile</param> /// <param name="position">Tile coordinates to open</param> /// <returns>State of open tile </returns> public Cell Open(ref Board userBoard, Tuple <int, int> position) { int x = position.Item1; int y = position.Item2; if (isFirstMove) { isFirstMove = false; GenerateMineField(width, height, mineNumber, position); } if (board.getBoard()[x][y].cell == Cell.EMPTY) { for (int i = -1; i <= 1; i++) { //check the horizontal range of board if (x + i >= 0 && x + i < width) { for (int j = -1; j <= 1; j++) { //check the vertical range of board and tile state if (y + j >= 0 && y + j < height && userBoard.getBoard()[x + i][y + j].cell == Cell.CLOSED) { //check if empty cell if so open function call itself again with the position of adjacent empty cell //open numerical cell adjacent to empty cell if (board.getBoard()[x + i][y + j].cell == Cell.EMPTY) { userBoard.getBoard()[x][y] = board.getBoard()[x][y]; Tuple <int, int> newPosition = new Tuple <int, int>(x + i, y + j); Open(ref userBoard, newPosition); } else { userBoard.getBoard()[x + i][y + j] = board.getBoard()[x + i][y + j]; } } } } } } //if cell is the mine, return mine and the end of the game else if (board.getBoard()[x][y].cell == Cell.MINE) { userBoard.getBoard()[x][y] = board.getBoard()[x][y];; return(userBoard.getBoard()[x][y].cell); } else { //open numerical cell userBoard.getBoard()[x][y] = board.getBoard()[x][y]; } return(userBoard.getBoard()[x][y].cell); }
/// <summary> /// Set numerical values of tiles based on number of adjacent mines /// </summary> /// <param name="board">Board game</param> /// <param name="x">Horizontal mine position</param> /// <param name="y">Vertical mine position</param> private void SetAdjacentCells(ref Board board, int x, int y) { for (int i = -1; i <= 1; i++) { //check the horizontal range of adjacent cell if (x + i >= 0 && x + i < width) { for (int j = -1; j <= 1; j++) { //check the verticle range of adjacent cell if cell state is not a mine if (y + j >= 0 && y + j < height && board.getBoard()[x + i][y + j].cell != Cell.MINE) { board.getBoard()[x + i][y + j].cell += 1; } } } } }
/// <summary> /// Search revealed tiles and flag obvious mines /// </summary> public void SearchRevealedTilesAndFlagObviousMines() { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //enter if it is opened numerical tile if (userBoard.getBoard()[i][j].cell != Cell.CLOSED && userBoard.getBoard()[i][j].cell != Cell.EMPTY && userBoard.getBoard()[i][j].cell != Cell.FLAG && userBoard.getBoard()[i][j].cell != Cell.MINE) { //Open function that flag the tile where is mine FlagAdjacentHiddenTiles(userBoard.getBoard()[i][j], i, j); } } } }