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;
@ -38,6 +28,7 @@ public class Board extends JPanel {
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);
@ -62,7 +57,8 @@ public class Board extends JPanel {
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,6 +70,11 @@ 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;
@ -86,6 +87,7 @@ public class Board extends JPanel {
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);
}
@ -118,31 +122,46 @@ public class Board extends JPanel {
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);
if (tile.isMine())
g.drawImage(icons.get("mine2"), x, y, this);
// Draw flagged tile
else if (tile.isDrawSurroundingMines() && tile.getSurroundingMines() > 0) {
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());
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);
else
if (tile.isFlagged())
g.drawImage(icons.get("tile3"), x, y, this);
// Draw normal tile
else g.drawImage(icons.get("tile"), x, y, this);
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);
}
@ -176,7 +200,7 @@ public class Board extends JPanel {
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) {
} 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);
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,7 +14,8 @@ 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),
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;

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;
/**
@ -28,6 +20,8 @@ public class CustomDialog extends JDialog {
/**
* 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

@ -17,7 +17,9 @@ public class GameOverEvent extends EventObject {
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;

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,7 +18,7 @@ import javax.swing.UIManager;
*/
public class Minesweeper {
private static final String VERSION = "1.0";
private static final String VERSION = "1.1";
private JFrame mframe;
@ -41,6 +29,8 @@ public class Minesweeper {
/**
* 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();
}
});
@ -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,7 +173,6 @@ public class Minesweeper {
gameMenu.add(customMenuItem);
menubar.add(gameMenu);
}
{
var highscoreMenu = new JMenu("Highscores");
@ -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

@ -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,6 +21,10 @@ 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);
@ -32,7 +32,9 @@ public class ScoreDialog extends JDialog {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(0, 0));
String[] columnNames = { "Place", "Name", "Duration", "Date" };
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++) {
@ -42,7 +44,6 @@ public class ScoreDialog extends JDialog {
data[i][2] = String.valueOf(s.getDuration());
data[i][3] = new SimpleDateFormat().format(s.getDate());
}
mtable = new JTable(data, columnNames);
getContentPane().add(mtable);

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;
@ -40,11 +33,19 @@ public class ScoreManager {
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())) {
} 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())) {
} 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);
@ -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

@ -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;
}