Skip to content

Commit d347f93

Browse files
authored
Merge pull request #1 from abhijeetSaroha/mybranch
initial commit
2 parents 8b8a0d9 + a06fabd commit d347f93

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

Main.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
new Minesweeper();
4+
}
5+
}

Minesweeper.java

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import java.awt.*;
2+
import java.awt.event.ActionEvent;
3+
import java.awt.event.ActionListener;
4+
import java.util.Random;
5+
import javax.swing.*;
6+
7+
public class Minesweeper extends JFrame {
8+
private JButton[][] buttons;
9+
private boolean[][] mines;
10+
private int[][] surroundingMines;
11+
private int uncoveredCells;
12+
13+
public Minesweeper() {
14+
setTitle("Minesweeper");
15+
setDefaultCloseOperation(EXIT_ON_CLOSE);
16+
setLayout(new GridLayout(10, 10));
17+
18+
buttons = new JButton[10][10];
19+
mines = new boolean[10][10];
20+
surroundingMines = new int[10][10];
21+
uncoveredCells = 0;
22+
23+
for (int i = 0; i < 10; i++) {
24+
for (int j = 0; j < 10; j++) {
25+
buttons[i][j] = new JButton();
26+
buttons[i][j].addActionListener(new CellClickListener(i, j));
27+
add(buttons[i][j]);
28+
}
29+
}
30+
31+
placeMines();
32+
countSurroundingMines();
33+
34+
pack();
35+
setVisible(true);
36+
}
37+
38+
private void placeMines() {
39+
Random random = new Random();
40+
int placedMines = 0;
41+
while (placedMines < 10) {
42+
int i = random.nextInt(10);
43+
int j = random.nextInt(10);
44+
if (!mines[i][j]) {
45+
mines[i][j] = true;
46+
placedMines++;
47+
}
48+
}
49+
}
50+
51+
private void countSurroundingMines() {
52+
for (int i = 0; i < 10; i++) {
53+
for (int j = 0; j < 10; j++) {
54+
if (!mines[i][j]) {
55+
int count = 0;
56+
if (i > 0 && mines[i - 1][j]) count++;
57+
if (i < 9 && mines[i + 1][j]) count++;
58+
if (j > 0 && mines[i][j - 1]) count++;
59+
if (j < 9 && mines[i][j + 1]) count++;
60+
if (i > 0 && j > 0 && mines[i - 1][j - 1]) count++;
61+
if (i < 9 && j < 9 && mines[i + 1][j + 1]) count++;
62+
if (i > 0 && j < 9 && mines[i - 1][j + 1]) count++;
63+
if (i < 9 && j > 0 && mines[i + 1][j - 1]) count++;
64+
surroundingMines[i][j] = count;
65+
}
66+
}
67+
}
68+
}
69+
70+
private void uncoverCell(int i, int j) {
71+
if (mines[i][j]) {
72+
loseGame();
73+
} else {
74+
buttons[i][j].setText(Integer.toString(surroundingMines[i][j]));
75+
buttons[i][j].setEnabled(false);
76+
uncoveredCells++;
77+
if (uncoveredCells == 90) {
78+
winGame();
79+
}
80+
if (surroundingMines[i][j] == 0) {
81+
uncoverSurroundingCells(i, j);
82+
}
83+
}
84+
}
85+
86+
private void uncoverSurroundingCells(int i, int j) {
87+
if (i > 0 && buttons[i - 1][j].isEnabled()) uncoverCell(i - 1, j);
88+
if (i < 9 && buttons[i + 1][j].isEnabled()) uncoverCell(i + 1, j);
89+
if (j > 0 && buttons[i][j - 1].isEnabled()) uncoverCell(i, j - 1);
90+
if (j < 9 && buttons[i][j + 1].isEnabled()) uncoverCell(i, j + 1);
91+
if (i > 0 && j > 0 && buttons[i - 1][j - 1].isEnabled()) uncoverCell(
92+
i - 1,
93+
j - 1
94+
);
95+
if (i < 9 && j < 9 && buttons[i + 1][j + 1].isEnabled()) uncoverCell(
96+
i + 1,
97+
j + 1
98+
);
99+
if (i > 0 && j < 9 && buttons[i - 1][j + 1].isEnabled()) uncoverCell(
100+
i - 1,
101+
j + 1
102+
);
103+
if (i < 9 && j > 0 && buttons[i + 1][j - 1].isEnabled()) uncoverCell(
104+
i + 1,
105+
j - 1
106+
);
107+
}
108+
109+
private void winGame() {
110+
JOptionPane.showMessageDialog(this, "You won!");
111+
System.exit(0);
112+
}
113+
114+
private void loseGame() {
115+
for (int i = 0; i < 10; i++) {
116+
for (int j = 0; j < 10; j++) {
117+
if (mines[i][j]) {
118+
buttons[i][j].setText("*");
119+
}
120+
buttons[i][j].setEnabled(false);
121+
}
122+
}
123+
JOptionPane.showMessageDialog(this, "You lost.");
124+
System.exit(0);
125+
}
126+
127+
private class CellClickListener implements ActionListener {
128+
private int i;
129+
private int j;
130+
131+
public CellClickListener(int i, int j) {
132+
this.i = i;
133+
this.j = j;
134+
}
135+
136+
public void actionPerformed(ActionEvent e) {
137+
uncoverCell(i, j);
138+
}
139+
}
140+
}

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
# minesweeper-in-java
1+
# Minesweeper Game in Java
2+
This repository contains the source code for a Minesweeper game in Java. The game is a classic puzzle game in which the player must uncover all cells that do not contain mines, while avoiding those that do. The game is implemented using the Java Swing framework for GUI.
3+
4+
## Code Structure
5+
The repository contains two code files:
6+
7+
1. **Main.java**: This is the main entry point for the application. It creates an instance of the `Minesweeper` class and starts the game.
8+
9+
2. **Minesweeper.java**: This is the implementation of the game logic. It extends the `JFrame` class and implements the game logic using the `ActionListener` interface.
10+
11+
## How to Play
12+
1. Clone the repository to your local machine.
13+
2. Compile and run the `Main.java` file.
14+
3. The game window will appear on the screen, with 10x10 cells represented by buttons.
15+
4. Click on the buttons to uncover cells. If a cell contains a mine, the game is lost. If a cell does not contain a mine, the number of surrounding mines will be displayed.
16+
5. The goal of the game is to uncover all cells that do not contain mines. If all 90 cells are uncovered, the player wins the game.
17+
18+
## Conclusion
19+
This Minesweeper game in Java is a fun and educational project for anyone interested in learning Java and game development. The project demonstrates the basics of GUI development in Java and the implementation of game logic.

0 commit comments

Comments
 (0)