KEMBAR78
Minesweeper Java | PDF | Software | System Software
0% found this document useful (0 votes)
43 views7 pages

Minesweeper Java

This document contains the code for a Minesweeper game. It initializes a grid of buttons, randomly places mines, calculates the number of neighboring mines for each empty cell, and allows the user to uncover cells by clicking buttons. When a mine is uncovered it ends the game, otherwise uncovered neighboring cells of empty cells.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views7 pages

Minesweeper Java

This document contains the code for a Minesweeper game. It initializes a grid of buttons, randomly places mines, calculates the number of neighboring mines for each empty cell, and allows the user to uncover cells by clicking buttons. When a mine is uncovered it ends the game, otherwise uncovered neighboring cells of empty cells.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import javax.swing.

*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

public class Minesweeper extends JFrame implements ActionListener {

private JPanel panel;

private JButton[][] buttons;

private int[][] counts;

private int size = 10;

private int numMines = 10;

private final int COVERED_CELL = 10;

private final int MINE_CELL = 9;

public Minesweeper() {

setTitle("Minesweeper");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(500, 500);

setLocationRelativeTo(null);

panel = new JPanel();


panel.setLayout(new GridLayout(size, size));

buttons = new JButton[size][size];

counts = new int[size][size];

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

buttons[i][j] = new JButton();

buttons[i][j].addActionListener(this);

panel.add(buttons[i][j]);

add(panel);

generateMines();

setVisible(true);

private void generateMines() {

Random rand = new Random();

int count = 0;

while (count < numMines) {

int x = rand.nextInt(size);

int y = rand.nextInt(size);
if (counts[x][y] != MINE_CELL) {

counts[x][y] = MINE_CELL;

count++;

calculateCounts();

private void calculateCounts() {

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

if (counts[i][j] != MINE_CELL) {

int count = 0;

if (i > 0 && j > 0 && counts[i - 1][j - 1] == MINE_CELL) {

count++;

if (i > 0 && counts[i - 1][j] == MINE_CELL) {

count++;

if (i > 0 && j < size - 1 && counts[i - 1][j + 1] == MINE_CELL) {

count++;

if (j > 0 && counts[i][j - 1] == MINE_CELL) {


count++;

if (j < size - 1 && counts[i][j + 1] == MINE_CELL) {

count++;

if (i < size - 1 && j > 0 && counts[i + 1][j - 1] == MINE_CELL) {

count++;

if (i < size - 1 && counts[i + 1][j] == MINE_CELL) {

count++;

if (i < size - 1 && j < size - 1 && counts[i + 1][j + 1] == MINE_CELL) {

count++;

counts[i][j] = count;

private void uncoverCell(int x, int y) {

if (counts[x][y] == MINE_CELL) {

gameOver();

} else {
buttons[x][y].setEnabled(false);

buttons[x][y].setText(counts[x][y] + "");

buttons[x][y].setBackground(Color.WHITE);

if (counts[x][y] == 0) {

if (x > 0 && y > 0 && buttons[x - 1][y - 1].isEnabled()) {

uncoverCell(x - 1, y - 1);

if (x > 0 && buttons[x - 1][y].isEnabled()) {

uncoverCell(x - 1, y);

if (x > 0 && y < size - 1 && buttons[x - 1][y + 1].isEnabled()) {

uncoverCell(x - 1, y + 1);

if (y > 0 && buttons[x][y - 1].isEnabled()) {

uncoverCell(x, y - 1);

if (y < size - 1 && buttons[x][y + 1].isEnabled()) {

uncoverCell(x, y + 1);

if (x < size - 1 && y > 0 && buttons[x + 1][y - 1].isEnabled()) {

uncoverCell(x + 1, y - 1);

if (x < size - 1 && buttons[x + 1][y].isEnabled()) {

uncoverCell(x + 1, y);
}

if (x < size - 1 && y < size - 1 && buttons[x + 1][y + 1].isEnabled()) {

uncoverCell(x + 1, y + 1);

private void gameOver() {

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

if (counts[i][j] == MINE_CELL) {

buttons[i][j].setText("X");

buttons[i][j].setBackground(Color.RED);

buttons[i][j].setEnabled(false);

JOptionPane.showMessageDialog(this, "Game Over!");

@Override

public void actionPerformed(ActionEvent e) {

JButton button = (JButton) e.getSource();


for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

if (button == buttons[i][j]) {

uncoverCell(i, j);

return;

public static void main(String[] args) {

new Minesweeper();

You might also like