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

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

@ -1,6 +1,5 @@
package Minesweeper; package Minesweeper;
import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -18,9 +17,9 @@ public class MinesweeperGame extends JPanel {
public static void main(String[] args) { public static void main(String[] args) {
JFrame f = new JFrame(); 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.setSize(WIDTH, HEIGTH);
f.setLayout(null); f.setLayout(null);
f.setVisible(true); f.setVisible(true);

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

@ -1,6 +1,5 @@
package Minesweeper; package Minesweeper;
import java.awt.Color;
import java.awt.Point; import java.awt.Point;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -127,11 +126,23 @@ public class Playfield {
public void cellFlooded() { public void cellFlooded() {
cellsFlooded++; cellsFlooded++;
if (cellsFlooded >= Size * Size - bombAmount) { if (cellsFlooded >= Size * Size - bombAmount) {
revealAllBombs();
JOptionPane.showMessageDialog(MsG, "You won, congratulations!"); JOptionPane.showMessageDialog(MsG, "You won, congratulations!");
reset(); 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() { public void cellDried() {
cellsFlooded--; cellsFlooded--;
} }
Loading…
Cancel
Save