2 Commits

Author SHA1 Message Date
kfkama 430ff09228 Add reveal cell function 3 years ago
kfkama c6c2328e4a Changed victory end to reset 3 years ago
  1. 6
      src/main/java/Minesweeper/Cell.java
  2. 11
      src/main/java/Minesweeper/Playfield.java

6
src/main/java/Minesweeper/Cell.java

@ -8,6 +8,7 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JOptionPane;
enum CellType { enum CellType {
Number, Bomb Number, Bomb
@ -50,11 +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!");
playfield.reset(); playfield.reset();
} }
} }
} }
@ -80,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 {

11
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;
@ -13,7 +14,6 @@ public class Playfield {
private int bombAmount; private int bombAmount;
private int cellsFlooded = 0; private int cellsFlooded = 0;
public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) { public Playfield(MinesweeperGame _MsG, int _Size, int _bombAmount) {
MsG = _MsG; MsG = _MsG;
Size = _Size; Size = _Size;
@ -44,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;
} }
} }
@ -62,10 +60,10 @@ public class Playfield {
} }
} }
} }
MsG.repaint();
} }
public void reset() { public void reset() {
JOptionPane.showMessageDialog(MsG,"KABOOM! Try again!");
cellsFlooded = 0; cellsFlooded = 0;
for (int i = 0; i < Size; i++) { for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) { for (int j = 0; j < Size; j++) {
@ -124,14 +122,13 @@ public class Playfield {
} }
} }
cells[row][column].update();
} }
public void cellFlooded() { public void cellFlooded() {
cellsFlooded++; cellsFlooded++;
if(cellsFlooded >= Size * Size - bombAmount) {
if (cellsFlooded >= Size * Size - bombAmount) {
JOptionPane.showMessageDialog(MsG, "You won, congratulations!"); JOptionPane.showMessageDialog(MsG, "You won, congratulations!");
System.exit(0);
reset();
} }
} }

Loading…
Cancel
Save