-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathMinesweeperGame.java
More file actions
187 lines (181 loc) · 6.22 KB
/
MinesweeperGame.java
File metadata and controls
187 lines (181 loc) · 6.22 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cleancode.minesweeper.tobe;
import java.util.Random;
import java.util.Scanner;
public class MinesweeperGame {
private static String[][] board = new String[8][10];
private static Integer[][] landMineCounts = new Integer[8][10];
private static boolean[][] landMines = new boolean[8][10];
private static int gameStatus = 0; // 0: 게임 중, 1: 승리, -1: 패배
public static void main(String[] args) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("지뢰찾기 게임 시작!");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
board[i][j] = "□";
}
}
for (int i = 0; i < 10; i++) {
int col = new Random().nextInt(10);
int row = new Random().nextInt(8);
landMines[row][col] = true;
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
int count = 0;
if (!landMines[i][j]) {
if (i - 1 >= 0 && j - 1 >= 0 && landMines[i - 1][j - 1]) {
count++;
}
if (i - 1 >= 0 && landMines[i - 1][j]) {
count++;
}
if (i - 1 >= 0 && j + 1 < 10 && landMines[i - 1][j + 1]) {
count++;
}
if (j - 1 >= 0 && landMines[i][j - 1]) {
count++;
}
if (j + 1 < 10 && landMines[i][j + 1]) {
count++;
}
if (i + 1 < 8 && j - 1 >= 0 && landMines[i + 1][j - 1]) {
count++;
}
if (i + 1 < 8 && landMines[i + 1][j]) {
count++;
}
if (i + 1 < 8 && j + 1 < 10 && landMines[i + 1][j + 1]) {
count++;
}
landMineCounts[i][j] = count;
continue;
}
landMineCounts[i][j] = 0;
}
}
while (true) {
System.out.println(" a b c d e f g h i j");
for (int i = 0; i < 8; i++) {
System.out.printf("%d ", i + 1);
for (int j = 0; j < 10; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
if (gameStatus == 1) {
System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!");
break;
}
if (gameStatus == -1) {
System.out.println("지뢰를 밟았습니다. GAME OVER!");
break;
}
System.out.println();
System.out.println("선택할 좌표를 입력하세요. (예: a1)");
String input = scanner.nextLine();
System.out.println("선택한 셀에 대한 행위를 선택하세요. (1: 오픈, 2: 깃발 꽂기)");
String input2 = scanner.nextLine();
char c = input.charAt(0);
char r = input.charAt(1);
int col;
switch (c) {
case 'a':
col = 0;
break;
case 'b':
col = 1;
break;
case 'c':
col = 2;
break;
case 'd':
col = 3;
break;
case 'e':
col = 4;
break;
case 'f':
col = 5;
break;
case 'g':
col = 6;
break;
case 'h':
col = 7;
break;
case 'i':
col = 8;
break;
case 'j':
col = 9;
break;
default:
col = -1;
break;
}
int row = Character.getNumericValue(r) - 1;
if (input2.equals("2")) {
board[row][col] = "⚑";
boolean open = true;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
if (board[i][j].equals("□")) {
open = false;
}
}
}
if (open) {
gameStatus = 1;
}
} else if (input2.equals("1")) {
if (landMines[row][col]) {
board[row][col] = "☼";
gameStatus = -1;
continue;
} else {
open(row, col);
}
boolean open = true;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
if (board[i][j].equals("□")) {
open = false;
}
}
}
if (open) {
gameStatus = 1;
}
} else {
System.out.println("잘못된 번호를 선택하셨습니다.");
}
}
}
private static void open(int row, int col) {
if (row < 0 || row >= 8 || col < 0 || col >= 10) {
return;
}
if (!board[row][col].equals("□")) {
return;
}
if (landMines[row][col]) {
return;
}
if (landMineCounts[row][col] != 0) {
board[row][col] = String.valueOf(landMineCounts[row][col]);
return;
} else {
board[row][col] = "■";
}
open(row - 1, col - 1);
open(row - 1, col);
open(row - 1, col + 1);
open(row, col - 1);
open(row, col + 1);
open(row + 1, col - 1);
open(row + 1, col);
open(row + 1, col + 1);
}
}