Added a visible game timer

This commit is contained in:
Kai S. K. Engelbart 2019-05-15 19:02:01 +02:00
parent b648db6755
commit 8f5a8b46d2
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 41 additions and 17 deletions

View File

@ -38,7 +38,7 @@ public class Board extends JPanel {
private int mines, activeTiles, flaggedTiles;
private Tile[][] board;
private BoardConfig boardConfig;
private Instant start, finish;
private List<GameListener> listeners;
@ -97,7 +97,7 @@ public class Board extends JPanel {
initMines();
repaint();
revalidate();
start = Instant.now();
}
@ -242,15 +242,17 @@ public class Board extends JPanel {
private void onGameOver() {
finish = Instant.now();
int duration = (int) Duration.between(start, finish).toMillis();
repaint();
GameOverEvent evt = new GameOverEvent(this, gameState, boardConfig, duration);
notifyGameStateEvent(evt);
}
public int getMines() { return mines; }
public int getActiveTiles() { return activeTiles; }
public int getFlaggedTiles() { return flaggedTiles; }
public BoardConfig getBoardConfig() { return boardConfig; }
}

View File

@ -2,7 +2,6 @@ package dev.kske.minesweeper;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@ -24,6 +23,7 @@ import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.UIManager;
/**
@ -39,6 +39,8 @@ public class Minesweeper {
private JFrame mframe;
private Board board;
private Timer timer;
private int gameTime;
private TreeSet<Score> scores;
private final String scoresFile = "scores.ser";
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.
*
* @wbp.parser.entryPoint
*/
public Minesweeper() {
initialize();
@ -85,7 +89,7 @@ public class Minesweeper {
});
mframe.setResizable(false);
mframe.setTitle("Minesweeper");
mframe.setBounds(100, 100, 198, 123);
mframe.setBounds(100, 100, 359, 86);
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMenuBar();
@ -97,16 +101,39 @@ public class Minesweeper {
JPanel headerPanel = new JPanel();
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);
panel.add(lblRemainingMines, BorderLayout.SOUTH);
lblRemainingMines.setHorizontalAlignment(SwingConstants.LEFT);
headerPanel.add(lblRemainingMines);
btnRestart.addActionListener((evt) -> {
board.reset();
gameTime = 0;
timer.restart();
});
mframe.pack();
board.registerGameListener(new GameListener() {
@Override
public void onGameOverEvent(GameOverEvent evt) {
timer.stop();
switch (evt.getGameState()) {
case 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();
timer.start();
}
private void createMenuBar() {
@ -179,9 +201,7 @@ public class Minesweeper {
mframe.setJMenuBar(menubar);
}
@SuppressWarnings(
"unchecked"
)
@SuppressWarnings("unchecked")
private void loadScores() {
try (var in = new ObjectInputStream(new FileInputStream(scoresFile))) {
Object obj = in.readObject();
@ -207,6 +227,8 @@ public class Minesweeper {
private void initGame(BoardConfig config) {
board.init(config);
gameTime = 0;
timer.restart();
mframe.pack();
}
}