Added a visible game timer
This commit is contained in:
parent
b648db6755
commit
8f5a8b46d2
@ -253,4 +253,6 @@ public class Board extends JPanel {
|
|||||||
public int getActiveTiles() { return activeTiles; }
|
public int getActiveTiles() { return activeTiles; }
|
||||||
|
|
||||||
public int getFlaggedTiles() { return flaggedTiles; }
|
public int getFlaggedTiles() { return flaggedTiles; }
|
||||||
|
|
||||||
|
public BoardConfig getBoardConfig() { return boardConfig; }
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package dev.kske.minesweeper;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
import java.awt.FlowLayout;
|
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
@ -24,6 +23,7 @@ import javax.swing.JMenuItem;
|
|||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.SwingConstants;
|
import javax.swing.SwingConstants;
|
||||||
|
import javax.swing.Timer;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,6 +39,8 @@ public class Minesweeper {
|
|||||||
private JFrame mframe;
|
private JFrame mframe;
|
||||||
|
|
||||||
private Board board;
|
private Board board;
|
||||||
|
private Timer timer;
|
||||||
|
private int gameTime;
|
||||||
private TreeSet<Score> scores;
|
private TreeSet<Score> scores;
|
||||||
private final String scoresFile = "scores.ser";
|
private final String scoresFile = "scores.ser";
|
||||||
private final BoardConfig easyConfig = new BoardConfig(8, 8, 10), mediumConfig = new BoardConfig(16, 16, 40),
|
private final BoardConfig easyConfig = new BoardConfig(8, 8, 10), mediumConfig = new BoardConfig(16, 16, 40),
|
||||||
@ -60,6 +62,8 @@ public class Minesweeper {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the application.
|
* Create the application.
|
||||||
|
*
|
||||||
|
* @wbp.parser.entryPoint
|
||||||
*/
|
*/
|
||||||
public Minesweeper() {
|
public Minesweeper() {
|
||||||
initialize();
|
initialize();
|
||||||
@ -85,7 +89,7 @@ public class Minesweeper {
|
|||||||
});
|
});
|
||||||
mframe.setResizable(false);
|
mframe.setResizable(false);
|
||||||
mframe.setTitle("Minesweeper");
|
mframe.setTitle("Minesweeper");
|
||||||
mframe.setBounds(100, 100, 198, 123);
|
mframe.setBounds(100, 100, 359, 86);
|
||||||
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
createMenuBar();
|
createMenuBar();
|
||||||
@ -97,16 +101,39 @@ public class Minesweeper {
|
|||||||
|
|
||||||
JPanel headerPanel = new JPanel();
|
JPanel headerPanel = new JPanel();
|
||||||
mframe.getContentPane().add(headerPanel, BorderLayout.NORTH);
|
mframe.getContentPane().add(headerPanel, BorderLayout.NORTH);
|
||||||
headerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
|
headerPanel.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
|
JButton btnRestart = new JButton("Restart");
|
||||||
|
btnRestart.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||||
|
headerPanel.add(btnRestart, BorderLayout.EAST);
|
||||||
|
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
headerPanel.add(panel, BorderLayout.WEST);
|
||||||
|
panel.setLayout(new BorderLayout(0, 0));
|
||||||
|
|
||||||
|
JLabel lblTime = new JLabel("Time:");
|
||||||
|
panel.add(lblTime, BorderLayout.NORTH);
|
||||||
|
|
||||||
|
timer = new Timer(1000, e -> lblTime.setText("Time: " + gameTime++ + "s"));
|
||||||
|
timer.setRepeats(true);
|
||||||
|
timer.setInitialDelay(0);
|
||||||
|
timer.setCoalesce(true);
|
||||||
|
|
||||||
JLabel lblRemainingMines = new JLabel("Remaining Mines: " + easyConfig.mines);
|
JLabel lblRemainingMines = new JLabel("Remaining Mines: " + easyConfig.mines);
|
||||||
|
panel.add(lblRemainingMines, BorderLayout.SOUTH);
|
||||||
lblRemainingMines.setHorizontalAlignment(SwingConstants.LEFT);
|
lblRemainingMines.setHorizontalAlignment(SwingConstants.LEFT);
|
||||||
headerPanel.add(lblRemainingMines);
|
btnRestart.addActionListener((evt) -> {
|
||||||
|
board.reset();
|
||||||
|
gameTime = 0;
|
||||||
|
timer.restart();
|
||||||
|
});
|
||||||
|
mframe.pack();
|
||||||
|
|
||||||
board.registerGameListener(new GameListener() {
|
board.registerGameListener(new GameListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onGameOverEvent(GameOverEvent evt) {
|
public void onGameOverEvent(GameOverEvent evt) {
|
||||||
|
timer.stop();
|
||||||
switch (evt.getGameState()) {
|
switch (evt.getGameState()) {
|
||||||
case LOST:
|
case LOST:
|
||||||
JOptionPane.showMessageDialog(mframe, "Game lost!");
|
JOptionPane.showMessageDialog(mframe, "Game lost!");
|
||||||
@ -128,13 +155,8 @@ public class Minesweeper {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
JButton btnRestart = new JButton("Restart");
|
|
||||||
btnRestart.setHorizontalAlignment(SwingConstants.RIGHT);
|
|
||||||
headerPanel.add(btnRestart);
|
|
||||||
btnRestart.addActionListener((evt) -> board.reset());
|
|
||||||
mframe.pack();
|
|
||||||
|
|
||||||
loadScores();
|
loadScores();
|
||||||
|
timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createMenuBar() {
|
private void createMenuBar() {
|
||||||
@ -179,9 +201,7 @@ public class Minesweeper {
|
|||||||
mframe.setJMenuBar(menubar);
|
mframe.setJMenuBar(menubar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings(
|
@SuppressWarnings("unchecked")
|
||||||
"unchecked"
|
|
||||||
)
|
|
||||||
private void loadScores() {
|
private void loadScores() {
|
||||||
try (var in = new ObjectInputStream(new FileInputStream(scoresFile))) {
|
try (var in = new ObjectInputStream(new FileInputStream(scoresFile))) {
|
||||||
Object obj = in.readObject();
|
Object obj = in.readObject();
|
||||||
@ -207,6 +227,8 @@ public class Minesweeper {
|
|||||||
|
|
||||||
private void initGame(BoardConfig config) {
|
private void initGame(BoardConfig config) {
|
||||||
board.init(config);
|
board.init(config);
|
||||||
|
gameTime = 0;
|
||||||
|
timer.restart();
|
||||||
mframe.pack();
|
mframe.pack();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user