import java.awt.event.*; import java.io.*; import java.nio.file.*; import java.util.stream.Stream; import java.util.Arrays; import javax.swing.*; import javax.swing.event.*; public class Main { private static enum Difficulty { BEGINNER (9, 9, 10), INTERMEDIATE (16, 16, 40), EXPERT (16, 30, 99); public final int ROWS; public final int COLS; public final int MINES; Difficulty(int rows, int cols, int mines) { this.ROWS = rows; this.COLS = cols; this.MINES = mines; } } public static final String DEFAULT_SKIN = "winxpskin.bmp"; public static final String SKINS_DIR = "Skins/"; public static final String HELP_FILE = "help.html"; public static final String ABOUT_FILE = "about.html"; private static final String[] VALID_EXTENSIONS = {".bmp", ".png", ".webp"}; private static final JFrame FRAME = new JFrame("Minesweeper"); private static Canvas canvas; private static Difficulty difficulty; private static String skin; private static boolean protectedStart; private static boolean sound; public static void main(String[] args) { difficulty = Difficulty.INTERMEDIATE; skin = DEFAULT_SKIN; protectedStart = false; sound = false; setCanvas(); FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FRAME.setResizable(false); FRAME.setJMenuBar(getMenuBar()); FRAME.add(canvas); FRAME.pack(); FRAME.setVisible(true); } public static boolean isProtectedStart() { return protectedStart; } public static boolean hasSound() { return sound; } private static JMenuBar getMenuBar() { ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "new": canvas.restart(); break; case "beginner": setDifficulty(Difficulty.BEGINNER); break; case "intermediate": setDifficulty(Difficulty.INTERMEDIATE); break; case "expert": setDifficulty(Difficulty.EXPERT); break; case "custom": setDifficulty(null); break; case "protectedstart": protectedStart = !protectedStart; break; case "sound": sound = !sound; Sound.initSounds(); break; case "exit": FRAME.dispatchEvent(new WindowEvent(FRAME, WindowEvent.WINDOW_CLOSING)); break; case "help": messageHtmlFile(HELP_FILE, "Failed to read help file, just google it lol.", "Minesweeper Help", JOptionPane.QUESTION_MESSAGE); break; case "about": messageHtmlFile(ABOUT_FILE, "I love microsoft!!", "Minesweeper About", JOptionPane.INFORMATION_MESSAGE); break; } } }; JMenuBar menuBar = new JMenuBar(); JMenu gameMenu = new JMenu("Game"); gameMenu.setMnemonic(KeyEvent.VK_G); JMenuItem newGameItem = new JMenuItem("New game"); newGameItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0)); addMenuItem(newGameItem, gameMenu, KeyEvent.VK_N, "new", listener); gameMenu.addSeparator(); ButtonGroup difficultyButtons = new ButtonGroup(); JRadioButtonMenuItem beginnerItem = new JRadioButtonMenuItem("Beginner"); JRadioButtonMenuItem intermediateItem = new JRadioButtonMenuItem("Intermediate", true); JRadioButtonMenuItem expertItem = new JRadioButtonMenuItem("Advanced"); JRadioButtonMenuItem customItem = new JRadioButtonMenuItem("Custom..."); difficultyButtons.add(beginnerItem); difficultyButtons.add(intermediateItem); difficultyButtons.add(expertItem); difficultyButtons.add(customItem); addMenuItem(beginnerItem, gameMenu, KeyEvent.VK_B, "beginner", listener); addMenuItem(intermediateItem, gameMenu, KeyEvent.VK_I, "intermediate", listener); addMenuItem(expertItem, gameMenu, KeyEvent.VK_E, "expert", listener); addMenuItem(customItem, gameMenu, KeyEvent.VK_C, "custom", listener); gameMenu.addSeparator(); JCheckBoxMenuItem protectedStartItem = new JCheckBoxMenuItem("Protected Start", protectedStart); protectedStartItem.setToolTipText("Guarantees the starting tile has no mines next to it"); addMenuItem(protectedStartItem, gameMenu, KeyEvent.VK_P, "protectedstart", listener); JCheckBoxMenuItem soundItem = new JCheckBoxMenuItem("Sound", sound); addMenuItem(soundItem, gameMenu, KeyEvent.VK_O, "sound", listener); gameMenu.addSeparator(); addMenuItem("Exit", gameMenu, KeyEvent.VK_X, "exit", listener); menuBar.add(gameMenu); JMenu helpMenu = new JMenu("Help"); helpMenu.setMnemonic(KeyEvent.VK_H); JMenuItem helpItem = new JMenuItem("Help"); helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0)); addMenuItem(helpItem, helpMenu, KeyEvent.VK_H, "help", listener); helpMenu.addSeparator(); addMenuItem("About", helpMenu, KeyEvent.VK_A, "about", listener); menuBar.add(helpMenu); JMenu skinsMenu = new JMenu("Skins"); skinsMenu.setMnemonic(KeyEvent.VK_S); // The Skins menu should dynamically generate a menu of all image files // in the skins directory when clicked in order to select one skinsMenu.addMenuListener(new MenuListener() { private static ActionListener skinListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Set the skin to the new one if it isn't already that String newSkin = e.getActionCommand(); if (newSkin.equals(skin)) return; skin = newSkin; replaceCanvas(); } }; @Override public void menuSelected(MenuEvent e) { // Get a stream of all files in the skins directory try (Stream dirStream = Files.list(Paths.get(SKINS_DIR))) { dirStream // Filter for only regular files .filter(file -> Files.isRegularFile(file)) // Cut off the directory name from the resulting string .map(Path::getFileName) .map(Path::toString) // Only allow a filename if it ends with one of the // valid extensions .filter(name -> Arrays.stream(VALID_EXTENSIONS) // Use regionMatches to effectively // case-insensitively endsWith() .anyMatch(ext -> name.regionMatches(true, name.length() - ext.length(), ext, 0, ext.length()))) // Because Files.list does not guarantee an order, and // alphabetical files look objectively nicer in lists .sorted() // Create a radio button menu item for each one, // remembering to have it pre-selected if it's already // the current skin .forEach(name -> addMenuItem(new JRadioButtonMenuItem(name, name.equals(skin)), skinsMenu, name, skinListener)); } catch (IOException ignore) { // It's fine to leave it blank if we can't get results } } @Override public void menuDeselected(MenuEvent e) { // Remove skins when we're done so they don't duplicate every // time we open the menu skinsMenu.removeAll(); } @Override public void menuCanceled(MenuEvent e) { } }); menuBar.add(skinsMenu); return menuBar; } private static void messageHtmlFile(String file, String failText, String title, int messageType) { String text; try { // For some reason, JLabels just stop parsing HTML after they hit a // newline, so swap the newlines out with spaces text = Files.readString(Paths.get(file)).replace('\n', ' '); } catch (IOException e) { text = failText; } JOptionPane.showMessageDialog(FRAME, text, title, messageType); } private static void setDifficulty(Difficulty newDifficulty) { if (newDifficulty == null); else if (difficulty == newDifficulty) return; difficulty = newDifficulty; replaceCanvas(); } private static void addMenuItem(String menuTitle, JMenu menu, int mnemonic, String actionCommand, ActionListener listener) { addMenuItem(new JMenuItem(menuTitle), menu, mnemonic, actionCommand, listener); } private static void addMenuItem(JMenuItem menuItem, JMenu menu, int mnemonic, String actionCommand, ActionListener listener) { menuItem.setMnemonic(mnemonic); addMenuItem(menuItem, menu, actionCommand, listener); } private static void addMenuItem(JMenuItem menuItem, JMenu menu, String actionCommand, ActionListener listener) { menuItem.setActionCommand(actionCommand); menuItem.addActionListener(listener); menu.add(menuItem); } private static void replaceCanvas() { canvas.stop(); canvas.removeAll(); FRAME.remove(canvas); setCanvas(); } private static void setCanvas() { canvas = getCanvas(); FRAME.add(canvas); FRAME.pack(); } private static Canvas getCanvas() { File skinFile = new File(SKINS_DIR, skin); if (difficulty == null) { // TODO: Make the difficulty actually change return new Canvas(40, 50, 500, skinFile); } return new Canvas(difficulty.ROWS, difficulty.COLS, difficulty.MINES, skinFile); } // No construction >:( private Main() { } }