From 3c6f092049c6cb45420b50c9b660e5731234c1e4 Mon Sep 17 00:00:00 2001 From: kfkama Date: Thu, 17 Feb 2022 16:03:42 +0100 Subject: [PATCH] Add flagging logic to cell --- src/main/java/Minesweeper/Cell.java | 33 +++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/main/java/Minesweeper/Cell.java b/src/main/java/Minesweeper/Cell.java index 80e9aac..9f6cc0c 100644 --- a/src/main/java/Minesweeper/Cell.java +++ b/src/main/java/Minesweeper/Cell.java @@ -27,7 +27,8 @@ public class Cell extends JButton { type = _type; cord = _cord; playfield = _playfield; - + + setBackground(Color.white); addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -48,18 +49,30 @@ public class Cell extends JButton { } protected void OnMouseClick() { - if (type != CellType.Bomb) { - flood(); - } else { - playfield.reset(); + if (!flagged) { + if (type != CellType.Bomb) { + flood(); + } else { + playfield.reset(); + } } } 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) { return; } + setBackground(Color.LIGHT_GRAY); setEnabled(false); playfield.cellFlooded(); + if (value == 0) { if (cord.y > 0) { if (playfield.cells[cord.y - 1][cord.x].type == CellType.Number