From e94376468cc2cccda1e9897491455ac649e054c5 Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 29 Jul 2019 17:45:50 +0200 Subject: [PATCH] Initializing mines after first touch --- src/dev/kske/minesweeper/Board.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/dev/kske/minesweeper/Board.java b/src/dev/kske/minesweeper/Board.java index ec8ee9b..09910a3 100644 --- a/src/dev/kske/minesweeper/Board.java +++ b/src/dev/kske/minesweeper/Board.java @@ -38,6 +38,7 @@ public class Board extends JPanel { private int mines, activeTiles, flaggedTiles; private Tile[][] board; private BoardConfig boardConfig; + private boolean minesPlaced; private Instant start, finish; @@ -86,6 +87,7 @@ public class Board extends JPanel { mines = config.mines; activeTiles = boardWidth * boardHeight; flaggedTiles = 0; + minesPlaced = false; notifyFlaggedTilesEvent(new FlaggedTilesEvent(this, flaggedTiles)); @@ -94,7 +96,6 @@ public class Board extends JPanel { for (int i = 0; i < boardWidth; i++) for (int j = 0; j < boardHeight; j++) board[i][j] = new Tile(); - initMines(); repaint(); revalidate(); @@ -176,7 +177,7 @@ public class Board extends JPanel { int m = random.nextInt(boardHeight); // Check if the selected tile already is a mine and is not touched - if (!board[n][m].isMine()) { + if (!board[n][m].isTouched() && !board[n][m].isMine()) { // Decrement the counter remaining--; @@ -189,6 +190,7 @@ public class Board extends JPanel { board[i][j].setSurroundingMines(board[i][j].getSurroundingMines() + 1); } } + minesPlaced = true; } private void touchTile(int n, int m) { @@ -214,8 +216,11 @@ public class Board extends JPanel { onGameOver(); } + // Place the mines if this was the first touch + if (!minesPlaced) initMines(); + // Touch surrounding tiles when there are zero surrounding mines - else if (tile.getSurroundingMines() == 0) + if (tile.getSurroundingMines() == 0) for (int i = Math.max(0, n - 1); i < Math.min(n + 2, board.length); i++) for (int j = Math.max(0, m - 1); j < Math.min(m + 2, board[i].length); j++) if (i != n || j != m) touchTile(i, j);