From ab03a2d55ea4a28f93ba5da6a58076008fc3c0c3 Mon Sep 17 00:00:00 2001 From: DanielLyi <52933151+DanielLyi@users.noreply.github.com> Date: Thu, 18 Jul 2019 16:33:41 +0300 Subject: [PATCH] Now it shows 40 mines left at the bottom. Naming changes. --- src/com/zetcode/Board.java | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/com/zetcode/Board.java b/src/com/zetcode/Board.java index f5a00c7..0fb285b 100644 --- a/src/com/zetcode/Board.java +++ b/src/com/zetcode/Board.java @@ -5,6 +5,7 @@ import java.awt.Image; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.util.Objects; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JLabel; @@ -33,14 +34,12 @@ public class Board extends JPanel { private final int BOARD_WIDTH = N_COLS * CELL_SIZE + 1; private final int BOARD_HEIGHT = N_ROWS * CELL_SIZE + 1; - + private final JLabel statusbar; private int[] field; private boolean inGame; private int minesLeft; private Image[] img; - private int allCells; - private final JLabel statusbar; public Board(JLabel statusbar) { @@ -56,8 +55,9 @@ private void initBoard() { for (int i = 0; i < NUM_IMAGES; i++) { - var path = "src/resources/" + i + ".png"; - img[i] = (new ImageIcon(path)).getImage(); + var path = "resources/" + i + ".png"; //made src as source folder, now this path should be correct + img[i] = (new ImageIcon(Objects.requireNonNull(getClass().getClassLoader().getResource( + path)))).getImage(); //this way it should also show images when executing remote jar file or class from terminal } addMouseListener(new MinesAdapter()); @@ -80,7 +80,7 @@ private void newGame() { field[i] = COVER_FOR_CELL; } - statusbar.setText(Integer.toString(minesLeft)); + statusbar.setText(minesLeft + " mines left"); //not a big change) int i = 0; @@ -331,7 +331,12 @@ public void mousePressed(MouseEvent e) { if (minesLeft > 0) { field[(cRow * N_COLS) + cCol] += MARK_FOR_CELL; minesLeft--; - String msg = Integer.toString(minesLeft); + String msg; + if (minesLeft == 1) { // just for interface purpose + msg = "1 mine left"; + } else { + msg = minesLeft + " mines left"; + } statusbar.setText(msg); } else { statusbar.setText("No marks left"); @@ -340,7 +345,12 @@ public void mousePressed(MouseEvent e) { field[(cRow * N_COLS) + cCol] -= MARK_FOR_CELL; minesLeft++; - String msg = Integer.toString(minesLeft); + String msg; + if (minesLeft == 1) { + msg = "1 mine left"; + } else { + msg = minesLeft + " mines left"; + } statusbar.setText(msg); } }