Browse Source

Add reveal cell function

feature_Minesweeper_Playfield
kfkama 3 years ago
parent
commit
430ff09228
  1. 4
      src/main/java/Minesweeper/Cell.java
  2. 5
      src/main/java/Minesweeper/Playfield.java

4
src/main/java/Minesweeper/Cell.java

@ -51,12 +51,14 @@ public class Cell extends JButton {
protected void OnMouseClick() { protected void OnMouseClick() {
if (!flagged) { if (!flagged) {
reveal();
if (type != CellType.Bomb) { if (type != CellType.Bomb) {
flood(); flood();
} else { } else {
JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!"); JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!");
playfield.reset(); playfield.reset();
} }
} }
} }
@ -82,7 +84,7 @@ public class Cell extends JButton {
} }
} }
public void update() {
public void reveal() {
if (type == CellType.Number) { if (type == CellType.Number) {
setText(String.valueOf(value)); setText(String.valueOf(value));
} else { } else {

5
src/main/java/Minesweeper/Playfield.java

@ -1,5 +1,6 @@
package Minesweeper; package Minesweeper;
import java.awt.Color;
import java.awt.Point; import java.awt.Point;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -43,11 +44,9 @@ public class Playfield {
cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2), cells[i][j].setBounds(j * CELLSIZE + (MsG.WIDTH / 2 - Size * CELLSIZE / 2),
i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE); i * CELLSIZE + (MsG.HEIGTH / 2 - Size * CELLSIZE / 2), CELLSIZE, CELLSIZE);
MsG.add(cells[i][j]); MsG.add(cells[i][j]);
for (int k = 0; k < bPlacement.length; k++) { for (int k = 0; k < bPlacement.length; k++) {
if (bPlacement[k] == i * Size + j) { if (bPlacement[k] == i * Size + j) {
cells[i][j].type = CellType.Bomb; cells[i][j].type = CellType.Bomb;
cells[i][j].update();
break; break;
} }
} }
@ -61,6 +60,7 @@ public class Playfield {
} }
} }
} }
MsG.repaint();
} }
public void reset() { public void reset() {
@ -122,7 +122,6 @@ public class Playfield {
} }
} }
cells[row][column].update();
} }
public void cellFlooded() { public void cellFlooded() {

Loading…
Cancel
Save