Initializing mines after first touch

This commit is contained in:
Kai S. K. Engelbart 2019-07-29 17:45:50 +02:00
parent e092f43fb5
commit e94376468c
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
1 changed files with 8 additions and 3 deletions

View File

@ -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);