Browse Source

Add flagging logic to cell

feature_Minesweeper_Playfield
kfkama 3 years ago
parent
commit
3c6f092049
  1. 31
      src/main/java/Minesweeper/Cell.java

31
src/main/java/Minesweeper/Cell.java

@ -28,6 +28,7 @@ public class Cell extends JButton {
cord = _cord; cord = _cord;
playfield = _playfield; playfield = _playfield;
setBackground(Color.white);
addActionListener(new ActionListener() { addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -48,18 +49,30 @@ public class Cell extends JButton {
} }
protected void OnMouseClick() { protected void OnMouseClick() {
if (type != CellType.Bomb) {
flood();
} else {
playfield.reset();
if (!flagged) {
if (type != CellType.Bomb) {
flood();
} else {
playfield.reset();
}
} }
} }
protected void OnMouseRightClick() { protected void OnMouseRightClick() {
if (flagged) {
flagged = false;
} else {
flagged = true;
if (isEnabled()) {
if (flagged) {
flagged = false;
if (type == CellType.Number) {
setBackground(Color.gray);
} else {
setBackground(Color.red);
}
} else {
flagged = true;
setBackground(Color.cyan);
}
} }
} }
@ -75,9 +88,11 @@ public class Cell extends JButton {
if (type == CellType.Bomb) { if (type == CellType.Bomb) {
return; return;
} }
setBackground(Color.LIGHT_GRAY); setBackground(Color.LIGHT_GRAY);
setEnabled(false); setEnabled(false);
playfield.cellFlooded(); playfield.cellFlooded();
if (value == 0) { if (value == 0) {
if (cord.y > 0) { if (cord.y > 0) {
if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number

Loading…
Cancel
Save