3 Commits

Author SHA1 Message Date
kfkama 968c93804d Refactore Minesweeper, Playfield 3 years ago
kfkama 02c2b01d95 Reveal all bombs on game end 3 years ago
kfkama 7c6706bc20 Changed Colors 3 years ago
  1. 15
      src/main/java/Minesweeper/Cell.java
  2. 5
      src/main/java/Minesweeper/MinesweeperGame.java
  3. 13
      src/main/java/Minesweeper/Playfield.java

15
src/main/java/Minesweeper/Cell.java

@ -16,6 +16,10 @@ enum CellType {
public class Cell extends JButton {
private static final Color FLAGCOLOR = Color.RED;
private static final Color FLOODEDCOLOR = Color.LIGHT_GRAY;
private static final Color HIDDENCOLOR = Color.GRAY;
private static final Color MINECOLOR = Color.BLACK;
private static final long serialVersionUID = 1L;
private Playfield playfield;
@ -55,6 +59,7 @@ public class Cell extends JButton {
if (type != CellType.Bomb) {
flood();
} else {
playfield.revealAllBombs();
JOptionPane.showMessageDialog(getParent(),"KABOOM! Try again!");
playfield.reset();
}
@ -68,15 +73,15 @@ public class Cell extends JButton {
flagged = false;
if (type == CellType.Number) {
setBackground(Color.gray);
setBackground(HIDDENCOLOR);
playfield.cellDried();
} else {
setBackground(Color.red);
setBackground(MINECOLOR);
}
} else {
flagged = true;
setBackground(Color.cyan);
setBackground(FLAGCOLOR);
if (type == CellType.Number) {
playfield.cellFlooded();
}
@ -88,7 +93,7 @@ public class Cell extends JButton {
if (type == CellType.Number) {
setText(String.valueOf(value));
} else {
setBackground(Color.RED);
setBackground(MINECOLOR);
}
}
@ -97,7 +102,7 @@ public class Cell extends JButton {
return;
}
setBackground(Color.LIGHT_GRAY);
setBackground(FLOODEDCOLOR);
setEnabled(false);
playfield.cellFlooded();

5
src/main/java/Minesweeper/MinesweeperGame.java

@ -1,6 +1,5 @@
package Minesweeper;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@ -18,9 +17,9 @@ public class MinesweeperGame extends JPanel {
public static void main(String[] args) {
JFrame f = new JFrame();
MinesweeperGame ttt = new MinesweeperGame(8, 10);
MinesweeperGame MsG = new MinesweeperGame(8, 10);
f.add(ttt);
f.add(MsG);
f.setSize(WIDTH, HEIGTH);
f.setLayout(null);
f.setVisible(true);

13
src/main/java/Minesweeper/Playfield.java

@ -1,6 +1,5 @@
package Minesweeper;
import java.awt.Color;
import java.awt.Point;
import javax.swing.JOptionPane;
@ -127,11 +126,23 @@ public class Playfield {
public void cellFlooded() {
cellsFlooded++;
if (cellsFlooded >= Size * Size - bombAmount) {
revealAllBombs();
JOptionPane.showMessageDialog(MsG, "You won, congratulations!");
reset();
}
}
public void revealAllBombs() {
for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
if(cells[i][j].type == CellType.Bomb) {
cells[i][j].reveal();
}
}
}
MsG.repaint();
}
public void cellDried() {
cellsFlooded--;
}
Loading…
Cancel
Save