Support expanding a uncovered tile surrounded by unflagged tiles.

This commit is contained in:
uohlhv 2026-04-25 21:14:34 +08:00
parent 8d1714777d
commit 25f99b9447
2 changed files with 18 additions and 0 deletions

View file

@ -115,6 +115,21 @@ public class Board extends JPanel {
setVisible(true);
}
public int getTileFlaggedNum(int row, int col) {
int counter = 0;
for (int i = row - 1; i <= row + 1; i += 1) {
for (int j = col - 1; j <= col + 1; j += 1) {
if (!(i < 0 || j < 0 || i >= this.height || j >= this.width) && !(i == row && j == col)) {
Tile tile = this.tiles[i][j];
if (tile.isFlagged()) {
counter += 1;
}
}
}
}
return counter;
}
public void showMines() {
setVisible(false);
for (int i = 0; i < this.height; i += 1) {

View file

@ -169,6 +169,9 @@ public class Tile extends JLabel implements MouseListener {
}
if (SwingUtilities.isLeftMouseButton(e) && !this.uncovered && !this.flagged) {
this.setTileIcon("0");
} else if (this.isUncovered() && e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e) &&
this.getValue() == this.board.getTileFlaggedNum(row, col)) {
this.board.expand(row, col);
} else if (SwingUtilities.isMiddleMouseButton(e)) {
if (!this.flagged) {
this.setTileIcon("0");