Dive into the classic fun of Minesweeper, crafted from scratch in Java! Using the Swing library, this game brings a vibrant, interactive twist to your screen. With each click, discover hidden cells rendered with JButtons, all while strategically avoiding concealed mines. Can you clear the board and avoid the explosive surprises lurking beneath the surface? Test your wit and reflexes in this engaging challenge!
Objective: Reveal all safe cells while avoiding hidden bombs. Use numbers as clues - they indicate how many bombs are in the adjacent cells (including diagonals).
Left Mouse Button
- Reveal cellInput Dialog
- Set custom board size and bomb count at startupYes/No Dialog
- Choose to play again after game ends
Tip: Look for patterns! When a cell shows '0', it means there are no bombs in any of the adjacent cells, and they will be automatically revealed for you.
- Java - Core programming language
- Java Swing (javax.swing) - For GUI components (JFrame, JPanel, JButton)
- Java AWT (java.awt) - For graphics, events, and layouts
Customizable board size and bomb count through interactive player input. The game adapts to player preferences for difficulty.
How it works: When initializing, the game prompts for board dimensions and bomb count.
public static int getPlayerInput(String message) {
String input = promptPlayerInput(message);
if (input == null) {
System.exit(0);
return 0;
}
while(!isInt(input) || Integer.parseInt(input) < 1)
input = promptPlayerInput(message);
return Integer.parseInt(input);
}
private static String promptPlayerInput(String message) {
return JOptionPane.showInputDialog(message);
}
Smart cell revealing mechanism that automatically expands empty regions when clicked.
How it works: When a cell with no adjacent bombs is clicked, it recursively reveals neighboring cells.
private boolean showCell(int row, int col) {
if(row < 0 || col < 0) return false;
if(row > (MAX_ROWS - 1) || col > (MAX_COLUMNS - 1)) return false;
if(grid.isBombAtLocation(row, col)) return false;
JButton button = buttons[row][col];
if(!button.isContentAreaFilled()) return false;
int count = grid.getCountAtLocation(row, col);
// ...cell clicked logic...
return count == 0;
}
private void showAdjacentCells(int row, int col) {
/* Above */
if(showCell(row - 1, col)) showAdjacentCells(row - 1, col);
if(showCell(row - 1, col - 1)) showAdjacentCells(row - 1, col - 1);
if(showCell(row - 1, col + 1)) showAdjacentCells(row - 1, col + 1);
/* Adjacent */
if(showCell(row, col - 1)) showAdjacentCells(row, col - 1);
if(showCell(row, col + 1)) showAdjacentCells(row, col + 1);
/* Below */
if(showCell(row + 1, col)) showAdjacentCells(row + 1, col);
if(showCell(row + 1, col - 1)) showAdjacentCells(row + 1, col - 1);
if(showCell(row + 1, col + 1)) showAdjacentCells(row + 1, col + 1);
}
Visual feedback system using different colors to indicate the number of adjacent bombs.
How it works: Each cell is assigned a color based on its proximity to bombs.
private Color getTileColor(int num) {
Color color;
switch (num) {
default -> color = Color.RED;
case 0 -> color = Color.CYAN;
case 1 -> color = Color.GREEN;
case 2 -> color = Color.YELLOW;
case 3 -> color = Color.ORANGE;
}
return color;
}
Simple but effective system for randomly distributing bombs across the game board while ensuring fair gameplay.
How it works: Shuffles a list of bomb and empty cell markers before mapping them to the game grid.
public void createBombGrid() {
List<Boolean> bombs = new ArrayList<>();
for(int i = 0; i < numBombs; i++) bombs.add(true);
for(int i = 0; i < (numRows * numColumns) - numBombs; i++) bombs.add(false);
Collections.shuffle(bombs);
int index = 0;
for(int row = 0; row < bombGrid.length; row++) {
for(int col = 0; col < bombGrid[row].length; col++) {
bombGrid[row][col] = bombs.get(index);
index++;
}
}
}
This project is licensed under the MIT License.