Repository navigation
Expand file tree
/
Copy pathMinesweeperMini.java
More file actions
137 lines (95 loc) · 3.56 KB
/
Copy pathMinesweeperMini.java
File metadata and controls
137 lines (95 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.util.*;
public class MinesweeperMini {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int size = 5;
int mines = 5;
boolean[][] mineGrid = new boolean[size][size];
boolean[][] revealed = new boolean[size][size];
// Place mines randomly
int placedMines = 0;
while (placedMines < mines) {
int row = random.nextInt(size);
int col = random.nextInt(size);
if (!mineGrid[row][col]) {
mineGrid[row][col] = true;
placedMines++;
}
}
int safeCells = 0;
int totalSafeCells = size * size - mines;
System.out.println("================================");
System.out.println(" MINESWEEPER MINI");
System.out.println("================================");
while (true) {
// Display grid
System.out.println();
System.out.print(" ");
for (int col = 1; col <= size; col++) {
System.out.print(col + " ");
}
System.out.println();
for (int row = 0; row < size; row++) {
System.out.print((row + 1) + " ");
for (int col = 0; col < size; col++) {
if (revealed[row][col]) {
System.out.print("✓ ");
} else {
System.out.print("■ ");
}
}
System.out.println();
}
System.out.println();
System.out.print("Enter row (1-5): ");
int row = sc.nextInt() - 1;
System.out.print("Enter column (1-5): ");
int col = sc.nextInt() - 1;
// Check valid position
if (row < 0 || row >= size ||
col < 0 || col >= size) {
System.out.println("❌ Invalid position!");
continue;
}
// Check already selected
if (revealed[row][col]) {
System.out.println("⚠️ You already selected this cell!");
continue;
}
// Check mine
if (mineGrid[row][col]) {
System.out.println();
System.out.println("💣 BOOM!");
System.out.println("You hit a mine!");
// Show all mines
System.out.println("\nMine locations:");
for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
if (mineGrid[r][c]) {
System.out.print("💣 ");
} else {
System.out.print("■ ");
}
}
System.out.println();
}
System.out.println("\nGame Over!");
break;
} else {
revealed[row][col] = true;
safeCells++;
System.out.println("✅ Safe cell!");
// Check win
if (safeCells == totalSafeCells) {
System.out.println();
System.out.println("🎉 Congratulations!");
System.out.println("You cleared the entire board!");
System.out.println("You avoided all " + mines + " mines!");
break;
}
}
}
sc.close();
}
}