Compare commits

...

No commits in common. "v1.0" and "develop" have entirely different histories.

13 changed files with 258 additions and 197 deletions

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="res"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="res"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1,22 +1,12 @@
package dev.kske.minesweeper;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.swing.JPanel;
@ -28,16 +18,17 @@ import javax.swing.JPanel;
*/
public class Board extends JPanel {
private static final long serialVersionUID = -279269871397851420L;
private static final int tileSize = 32;
private static final long serialVersionUID = -279269871397851420L;
private static final int tileSize = 32;
private static Map<String, Image> icons;
private int boardWidth, boardHeight;
private GameState gameState;
private int mines, activeTiles, flaggedTiles;
private Tile[][] board;
private BoardConfig boardConfig;
private int boardWidth, boardHeight;
private GameState gameState;
private int mines, activeTiles, flaggedTiles;
private Tile[][] board;
private BoardConfig boardConfig;
private boolean minesPlaced;
private Instant start, finish;
@ -45,12 +36,16 @@ public class Board extends JPanel {
static {
icons = new HashMap<>();
final String[] names = { "mine2", "mine4", "tile", "tile3" };
for (String name : names) {
for (String name : new String[] {
"mine2", "mine4", "tile", "tile3"
}) {
icons.put(name, TextureLoader.loadScaledImage(name, tileSize));
}
}
/**
* Creates an instance of {@link Board}.
*/
public Board() {
// Not using a layout manager
super(null);
@ -59,10 +54,11 @@ public class Board extends JPanel {
@Override
public void mousePressed(MouseEvent evt) {
int n = evt.getX() / tileSize, m = evt.getY() / tileSize;
Tile tile = board[n][m];
int n = evt.getX() / tileSize, m = evt.getY() / tileSize;
Tile tile = board[n][m];
if (tile.isTouched() || gameState != GameState.ACTIVE) return;
if (tile.isTouched() || gameState != GameState.ACTIVE)
return;
switch (evt.getButton()) {
case MouseEvent.BUTTON1:
touchTile(n, m);
@ -74,18 +70,24 @@ public class Board extends JPanel {
});
}
/**
* Initializes the board with a given configuration. This does not include mine placement.
*
* @param config the configuration used
*/
public void init(BoardConfig config) {
boardConfig = config;
boardWidth = config.width;
boardHeight = config.height;
boardWidth = config.width;
boardHeight = config.height;
setPreferredSize(new Dimension(config.width * tileSize, config.height * tileSize));
gameState = GameState.ACTIVE;
mines = config.mines;
activeTiles = boardWidth * boardHeight;
flaggedTiles = 0;
gameState = GameState.ACTIVE;
mines = config.mines;
activeTiles = boardWidth * boardHeight;
flaggedTiles = 0;
minesPlaced = false;
notifyFlaggedTilesEvent(new FlaggedTilesEvent(this, flaggedTiles));
@ -94,13 +96,15 @@ public class Board extends JPanel {
for (int i = 0; i < boardWidth; i++)
for (int j = 0; j < boardHeight; j++)
board[i][j] = new Tile();
initMines();
repaint();
revalidate();
start = Instant.now();
}
/**
* Re-initializes the board with the cached configuration, thereby resetting it.
*/
public void reset() {
init(boardConfig);
}
@ -110,39 +114,54 @@ public class Board extends JPanel {
super.paintComponent(g);
for (int i = 0; i < boardWidth; i++)
for (int j = 0; j < boardHeight; j++) {
Tile tile = board[i][j];
int x = i * tileSize, y = j * tileSize;
Tile tile = board[i][j];
int x = i * tileSize, y = j * tileSize;
// Draw background
g.setColor(Color.gray);
g.fillRect(x, y, x + tileSize, y + tileSize);
// Draw tile with normal mine
if (gameState == GameState.LOST && tile.isMine()) g.drawImage(icons.get("mine2"), x, y, this);
if (gameState == GameState.LOST && tile.isMine())
g.drawImage(icons.get("mine2"), x, y, this);
// Draw tile with diffused mine
else if (gameState == GameState.WON && tile.isMine()) g.drawImage(icons.get("mine4"), x, y, this);
else if (tile.isTouched()) {
else
if (gameState == GameState.WON && tile.isMine())
g.drawImage(icons.get("mine4"), x, y, this);
else
if (tile.isTouched()) {
// Draw tile with mine
if (tile.isMine()) g.drawImage(icons.get("mine2"), x, y, this);
// Draw tile with mine
if (tile.isMine())
g.drawImage(icons.get("mine2"), x, y, this);
// Draw flagged tile
else if (tile.isDrawSurroundingMines() && tile.getSurroundingMines() > 0) {
// Draw number of surrounding mines
String numStr = String.valueOf(tile.getSurroundingMines());
g.setFont(new Font("Arial", Font.BOLD, 18));
g.setColor(Color.red);
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(numStr), h = fm.getHeight();
g.drawString(numStr, x + (tileSize - w) / 2, y + (tileSize - h) / 2 + fm.getAscent());
}
}
// Draw flagged tile
else
if (
tile.isDrawSurroundingMines() && tile.getSurroundingMines() > 0
) {
// Draw number of surrounding mines
String numStr = String.valueOf(tile.getSurroundingMines());
g.setFont(new Font("Arial", Font.BOLD, 18));
g.setColor(Color.red);
FontMetrics fm = g.getFontMetrics();
int w = fm.stringWidth(numStr), h = fm.getHeight();
g.drawString(
numStr,
x + (tileSize - w) / 2,
y + (tileSize - h) / 2 + fm.getAscent()
);
}
}
// Draw flagged tile
else if (tile.isFlagged()) g.drawImage(icons.get("tile3"), x, y, this);
// Draw flagged tile
else
if (tile.isFlagged())
g.drawImage(icons.get("tile3"), x, y, this);
// Draw normal tile
else g.drawImage(icons.get("tile"), x, y, this);
// Draw normal tile
else
g.drawImage(icons.get("tile"), x, y, this);
// Draw grid
((Graphics2D) g).setStroke(new BasicStroke(2.0f));
@ -151,6 +170,11 @@ public class Board extends JPanel {
}
}
/**
* Registers a game listener that is notified when game events occur.
*
* @param listener the game listener to register
*/
public void registerGameListener(GameListener listener) {
listeners.add(listener);
}
@ -168,15 +192,15 @@ public class Board extends JPanel {
}
private void initMines() {
int remaining = mines;
Random random = new Random();
int remaining = mines;
Random random = new Random();
while (remaining > 0) {
// Randomly select a tile
int n = random.nextInt(boardWidth);
int m = random.nextInt(boardHeight);
int n = random.nextInt(boardWidth);
int m = random.nextInt(boardHeight);
// Check if the selected tile already is a mine and is not touched
if (!board[n][m].isMine()) {
if (!board[n][m].isTouched() && !board[n][m].isMine()) {
// Decrement the counter
remaining--;
@ -189,6 +213,7 @@ public class Board extends JPanel {
board[i][j].setSurroundingMines(board[i][j].getSurroundingMines() + 1);
}
}
minesPlaced = true;
}
private void touchTile(int n, int m) {
@ -204,21 +229,25 @@ public class Board extends JPanel {
flaggedTiles--;
notifyFlaggedTilesEvent(new FlaggedTilesEvent(this, flaggedTiles));
}
// Test if the game is won or lost
if (tile.isMine()) {
gameState = GameState.LOST;
onGameOver();
} else if (mines == activeTiles) {
gameState = GameState.WON;
onGameOver();
}
} else
if (mines == activeTiles) {
gameState = GameState.WON;
onGameOver();
}
// Place the mines if this was the first touch
if (!minesPlaced)
initMines();
// Touch surrounding tiles when there are zero surrounding mines
else if (tile.getSurroundingMines() == 0)
if (tile.getSurroundingMines() == 0)
for (int i = Math.max(0, n - 1); i < Math.min(n + 2, board.length); i++)
for (int j = Math.max(0, m - 1); j < Math.min(m + 2, board[i].length); j++)
if (i != n || j != m) touchTile(i, j);
for (int j = Math.max(0, m - 1); j < Math.min(m + 2, board[i].length); j++)
if (i != n || j != m)
touchTile(i, j);
repaintTile(n, m);
}
@ -248,11 +277,23 @@ public class Board extends JPanel {
notifyGameStateEvent(evt);
}
/**
* @return the total number of mines
*/
public int getMines() { return mines; }
/**
* @return the number of active tiles
*/
public int getActiveTiles() { return activeTiles; }
/**
* @return the number of flagged files
*/
public int getFlaggedTiles() { return flaggedTiles; }
/**
* @return the current configuration
*/
public BoardConfig getBoardConfig() { return boardConfig; }
}

View File

@ -3,6 +3,8 @@ package dev.kske.minesweeper;
import java.io.Serializable;
/**
* Defines board configuration consisting of board with and height as well as mine count.
* <p>
* Project: <strong>Minesweeper</strong><br>
* File: <strong>BoardConfig.java</strong><br>
* Created: <strong>01.04.2019</strong><br>
@ -12,14 +14,15 @@ public class BoardConfig implements Serializable {
private static final long serialVersionUID = -6083006887427383946L;
public static final BoardConfig EASY = new BoardConfig(8, 8, 10), MEDIUM = new BoardConfig(16, 16, 40),
HARD = new BoardConfig(30, 16, 99);
public static final BoardConfig EASY = new BoardConfig(8, 8, 10), MEDIUM
= new BoardConfig(16, 16, 40),
HARD = new BoardConfig(30, 16, 99);
public final int width, height, mines;
public BoardConfig(int width, int height, int mines) {
this.width = width;
this.height = height;
this.mines = mines;
this.width = width;
this.height = height;
this.mines = mines;
}
}

View File

@ -1,16 +1,8 @@
package dev.kske.minesweeper;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
/**
@ -21,13 +13,15 @@ import javax.swing.border.EmptyBorder;
*/
public class CustomDialog extends JDialog {
private static final long serialVersionUID = -4019516811065781434L;
private final JPanel mcontentPanel = new JPanel();
private static final long serialVersionUID = -4019516811065781434L;
private final JPanel mcontentPanel = new JPanel();
private BoardConfig result;
/**
* Create the dialog.
*
* @param owner the frame on top of which the dialog will be displayed
*/
public CustomDialog(Frame owner) {
super(owner, ModalityType.APPLICATION_MODAL);
@ -47,7 +41,9 @@ public class CustomDialog extends JDialog {
lblBoardWidth.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblBoardWidth);
JSlider sliderBoardWidth = new JSlider();
sliderBoardWidth.addChangeListener((evt) -> lblBoardWidth.setText(String.valueOf(sliderBoardWidth.getValue())));
sliderBoardWidth.addChangeListener(
(evt) -> lblBoardWidth.setText(String.valueOf(sliderBoardWidth.getValue()))
);
sliderBoardWidth.setValue(16);
sliderBoardWidth.setMinimum(2);
sliderBoardWidth.setMaximum(30);
@ -76,7 +72,9 @@ public class CustomDialog extends JDialog {
lblNumMines.setFont(new Font("Tahoma", Font.PLAIN, 14));
mcontentPanel.add(lblNumMines);
JSlider sliderNumMines = new JSlider();
sliderNumMines.addChangeListener((evt) -> lblNumMines.setText(String.valueOf(sliderNumMines.getValue())));
sliderNumMines.addChangeListener(
(evt) -> lblNumMines.setText(String.valueOf(sliderNumMines.getValue()))
);
sliderNumMines.setValue(16);
sliderNumMines.setMinimum(2);
sliderNumMines.setMaximum(200);
@ -89,8 +87,11 @@ public class CustomDialog extends JDialog {
JButton okButton = new JButton("Start Game");
okButton.setActionCommand("OK");
okButton.addActionListener((evt) -> {
result = new BoardConfig(sliderBoardWidth.getValue(), sliderBoardHeight.getValue(),
sliderNumMines.getValue());
result = new BoardConfig(
sliderBoardWidth.getValue(),
sliderBoardHeight.getValue(),
sliderNumMines.getValue()
);
dispose();
});
buttonPane.add(okButton);
@ -105,6 +106,11 @@ public class CustomDialog extends JDialog {
}
}
/**
* Displays the dialog.
*
* @return the board configuration defined by the user
*/
public BoardConfig showDialog() {
setVisible(true);
return result;

View File

@ -12,13 +12,13 @@ public class FlaggedTilesEvent extends EventObject {
private static final long serialVersionUID = -5809857531886339312L;
private final Board board;
private final int flagged;
private final Board board;
private final int flagged;
public FlaggedTilesEvent(Object source, int flagged) {
super(source);
board = (Board) source;
this.flagged = flagged;
board = (Board) source;
this.flagged = flagged;
}
public Board getBoard() { return board; }

View File

@ -12,17 +12,19 @@ public class GameOverEvent extends EventObject {
private static final long serialVersionUID = -966111253980213845L;
private final Board board;
private final GameState gameState;
private final BoardConfig boardConfig;
private final int duration;
private final Board board;
private final GameState gameState;
private final BoardConfig boardConfig;
private final int duration;
public GameOverEvent(Object source, GameState gameState, BoardConfig boardConfig, int duration) {
public GameOverEvent(
Object source, GameState gameState, BoardConfig boardConfig, int duration
) {
super(source);
board = (Board) source;
this.gameState = gameState;
this.boardConfig = boardConfig;
this.duration = duration;
board = (Board) source;
this.gameState = gameState;
this.boardConfig = boardConfig;
this.duration = duration;
}
public Board getBoard() { return board; }

View File

@ -1,8 +1,6 @@
package dev.kske.minesweeper;
import static dev.kske.minesweeper.BoardConfig.EASY;
import static dev.kske.minesweeper.BoardConfig.HARD;
import static dev.kske.minesweeper.BoardConfig.MEDIUM;
import static dev.kske.minesweeper.BoardConfig.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
@ -10,17 +8,7 @@ import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.*;
/**
* Project: <strong>Minesweeper</strong><br>
@ -30,17 +18,19 @@ import javax.swing.UIManager;
*/
public class Minesweeper {
private static final String VERSION = "1.0";
private static final String VERSION = "1.1";
private JFrame mframe;
private Board board;
private Timer timer;
private int gameTime;
private ScoreManager scoreManager;
private Board board;
private Timer timer;
private int gameTime;
private ScoreManager scoreManager;
/**
* Launch the application.
*
* @param args command line arguments are ignored
*/
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
@ -71,7 +61,6 @@ public class Minesweeper {
} catch (Exception ex) {
ex.printStackTrace();
}
mframe = new JFrame();
mframe.addWindowListener(new WindowAdapter() {
@ -115,11 +104,16 @@ public class Minesweeper {
JLabel lblRemainingMines = new JLabel("Remaining Mines: " + EASY.mines);
panel.add(lblRemainingMines, BorderLayout.SOUTH);
lblRemainingMines.setHorizontalAlignment(SwingConstants.LEFT);
btnRestart.addActionListener((evt) -> { board.reset(); gameTime = 0; timer.restart(); });
btnRestart.addActionListener((evt) -> {
board.reset();
gameTime = 0;
timer.restart();
});
mframe.pack();
board.registerGameListener(new GameListener() {
@SuppressWarnings("incomplete-switch")
@Override
public void onGameOverEvent(GameOverEvent evt) {
timer.stop();
@ -135,7 +129,8 @@ public class Minesweeper {
@Override
public void onFlaggedTilesEvent(FlaggedTilesEvent evt) {
lblRemainingMines.setText("Remaining Mines: " + (evt.getBoard().getMines() - evt.getFlagged()));
lblRemainingMines
.setText("Remaining Mines: " + (evt.getBoard().getMines() - evt.getFlagged()));
mframe.pack();
}
});
@ -151,10 +146,10 @@ public class Minesweeper {
{
var gameMenu = new JMenu("Game");
var easyMenuItem = new JMenuItem("Easy");
var mediumMenuItem = new JMenuItem("Medium");
var hardMenuItem = new JMenuItem("Hard");
var customMenuItem = new JMenuItem("Custom");
var easyMenuItem = new JMenuItem("Easy");
var mediumMenuItem = new JMenuItem("Medium");
var hardMenuItem = new JMenuItem("Hard");
var customMenuItem = new JMenuItem("Custom");
gameMenu.setMnemonic(KeyEvent.VK_G);
easyMenuItem.setMnemonic(KeyEvent.VK_E);
@ -167,7 +162,8 @@ public class Minesweeper {
hardMenuItem.addActionListener((evt) -> initGame(HARD));
customMenuItem.addActionListener((evt) -> {
BoardConfig cfg = new CustomDialog(mframe).showDialog();
if (cfg != null) initGame(cfg);
if (cfg != null)
initGame(cfg);
});
gameMenu.add(easyMenuItem);
@ -177,13 +173,12 @@ public class Minesweeper {
gameMenu.add(customMenuItem);
menubar.add(gameMenu);
}
{
var highscoreMenu = new JMenu("Highscores");
var easyMenuItem = new JMenuItem("Easy");
var mediumMenuItem = new JMenuItem("Medium");
var hardMenuItem = new JMenuItem("Hard");
var easyMenuItem = new JMenuItem("Easy");
var mediumMenuItem = new JMenuItem("Medium");
var hardMenuItem = new JMenuItem("Hard");
highscoreMenu.setMnemonic(KeyEvent.VK_H);
easyMenuItem.setMnemonic(KeyEvent.VK_E);
@ -199,16 +194,17 @@ public class Minesweeper {
highscoreMenu.add(hardMenuItem);
menubar.add(highscoreMenu);
}
{
var aboutMenuItem = new JMenuItem("About");
aboutMenuItem.addActionListener((evt) -> JOptionPane.showMessageDialog(board,
"Minesweeper version " + VERSION + "\nby Kai S. K. Engelbart"));
aboutMenuItem.addActionListener(
(evt) -> JOptionPane.showMessageDialog(board,
"Minesweeper version " + VERSION + "\nby Kai S. K. Engelbart"
)
);
menubar.add(aboutMenuItem);
}
mframe.setJMenuBar(menubar);
}

View File

@ -12,15 +12,15 @@ import java.util.Date;
public class Score implements Comparable<Score>, Serializable {
private static final long serialVersionUID = 3384023296639779740L;
private final String name;
private final int duration;
private final Date date;
private final String name;
private final int duration;
private final Date date;
public Score(String name, int duration, Date date) {
this.name = name;
this.duration = duration;
this.date = date;
this.name = name;
this.duration = duration;
this.date = date;
}
public String getName() { return name; }

View File

@ -6,11 +6,7 @@ import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.*;
/**
* Project: <strong>Minesweeper</strong><br>
@ -25,33 +21,38 @@ public class ScoreDialog extends JDialog {
/**
* Create the dialog.
*
* @param scores the scores to display
* @param boardConfigName the name of the board configuration with which the scores are
* associated
*/
public ScoreDialog(List<Score> scores, String boardConfigName) {
setModal(true);
setBounds(100, 100, 450, 300);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(0, 0));
String[] columnNames = { "Place", "Name", "Duration", "Date" };
String[][] data = new String[scores.size()][4];
String[] columnNames = {
"Place", "Name", "Duration", "Date"
};
String[][] data = new String[scores.size()][4];
Iterator<Score> iter = scores.iterator();
for(int i = 0; i < data.length; i++) {
for (int i = 0; i < data.length; i++) {
Score s = iter.next();
data[i][0] = String.valueOf(i + 1);
data[i][1] = s.getName();
data[i][2] = String.valueOf(s.getDuration());
data[i][3] = new SimpleDateFormat().format(s.getDate());
data[i][0] = String.valueOf(i + 1);
data[i][1] = s.getName();
data[i][2] = String.valueOf(s.getDuration());
data[i][3] = new SimpleDateFormat().format(s.getDate());
}
mtable = new JTable(data, columnNames);
getContentPane().add(mtable);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BorderLayout(0, 0));
panel.add(mtable.getTableHeader(), BorderLayout.CENTER);
JLabel lblHighscores = new JLabel("Highscores: " + boardConfigName);
panel.add(lblHighscores, BorderLayout.NORTH);
lblHighscores.setFont(new Font("Tahoma", Font.BOLD, 16));

View File

@ -1,15 +1,8 @@
package dev.kske.minesweeper;
import static dev.kske.minesweeper.BoardConfig.EASY;
import static dev.kske.minesweeper.BoardConfig.HARD;
import static dev.kske.minesweeper.BoardConfig.MEDIUM;
import static dev.kske.minesweeper.BoardConfig.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -24,31 +17,39 @@ import javax.swing.JOptionPane;
*/
public class ScoreManager {
private List<Score> easy, medium, hard;
private final String scoresFile = "scores.ser";
private List<Score> easy, medium, hard;
private final String scoresFile = "scores.ser";
public ScoreManager() {
easy = new ArrayList<>();
medium = new ArrayList<>();
hard = new ArrayList<>();
easy = new ArrayList<>();
medium = new ArrayList<>();
hard = new ArrayList<>();
}
public void addScore(GameOverEvent evt) {
// Determine board config
BoardConfig config = evt.getBoardConfig();
if (config == EASY && (easy.size() < 10 || easy.get(9).getDuration() > evt.getDuration())) {
String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date());
String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date());
sortInsert(score, easy);
} else if (config == MEDIUM && (medium.size() < 10 || medium.get(9).getDuration() > evt.getDuration())) {
String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date());
sortInsert(score, medium);
} else if (config == HARD && (hard.size() < 10 || hard.get(9).getDuration() > evt.getDuration())) {
String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date());
sortInsert(score, hard);
}
} else
if (
config == MEDIUM
&& (medium.size() < 10 || medium.get(9).getDuration() > evt.getDuration())
) {
String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date());
sortInsert(score, medium);
} else
if (
config == HARD
&& (hard.size() < 10 || hard.get(9).getDuration() > evt.getDuration())
) {
String name = JOptionPane.showInputDialog("Please enter your name");
Score score = new Score(name, evt.getDuration(), new Date());
sortInsert(score, hard);
}
}
private void sortInsert(Score score, List<Score> list) {
@ -76,16 +77,23 @@ public class ScoreManager {
public void loadScores() {
try (var in = new ObjectInputStream(new FileInputStream(scoresFile))) {
Object obj = in.readObject();
if (obj instanceof ArrayList<?>) easy = (ArrayList<Score>) obj;
if (obj instanceof ArrayList<?>)
easy = (ArrayList<Score>) obj;
obj = in.readObject();
if (obj instanceof ArrayList<?>) medium = (ArrayList<Score>) obj;
if (obj instanceof ArrayList<?>)
medium = (ArrayList<Score>) obj;
obj = in.readObject();
if (obj instanceof ArrayList<?>) hard = (ArrayList<Score>) obj;
else throw new IOException("Serialized object has the wrong class.");
if (obj instanceof ArrayList<?>)
hard = (ArrayList<Score>) obj;
else
throw new IOException("Serialized object has the wrong class.");
} catch (FileNotFoundException ex) {} catch (IOException | ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null,
"The score file seems to be corrupted. It will be replaced when closing the game.", "File error",
JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(
null,
"The score file seems to be corrupted. It will be replaced when closing the game.",
"File error",
JOptionPane.ERROR_MESSAGE
);
}
}

View File

@ -2,7 +2,6 @@ package dev.kske.minesweeper;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
@ -28,7 +27,7 @@ public class TextureLoader {
public static Image loadScaledImage(String name, int scale) {
BufferedImage in = null;
try {
in = ImageIO.read(new File("res" + File.separator + name + ".png"));
in = ImageIO.read(TextureLoader.class.getResourceAsStream("/" + name + ".png"));
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -10,8 +10,8 @@ public class Tile {
private boolean mine, flagged, touched;
private boolean drawSurroundingMines;
private int surroundingMines;
private boolean drawSurroundingMines;
private int surroundingMines;
public Tile() {
mine = flagged = touched = drawSurroundingMines = false;
@ -39,5 +39,7 @@ public class Tile {
public int getSurroundingMines() { return surroundingMines; }
public void setSurroundingMines(int surroundingMines) { this.surroundingMines = surroundingMines; }
public void setSurroundingMines(int surroundingMines) {
this.surroundingMines = surroundingMines;
}
}

View File

@ -1,3 +1,6 @@
/**
* Contains all classes related to the game.
*/
module Minesweeper {
requires java.desktop;
}