-
Notifications
You must be signed in to change notification settings - Fork 566
Expand file tree
/
Copy pathMinesweeperGame.java
More file actions
245 lines (216 loc) · 8.64 KB
/
MinesweeperGame.java
File metadata and controls
245 lines (216 loc) · 8.64 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package cleancode.minesweeper.tobe;
import java.util.Random;
import java.util.Scanner;
public class MinesweeperGame {
public static final int BOARD_ROW_SIZE = 8;
public static final int BOARD_COLUMN_SIZE = 8;
public static final int LAND_MINE_COUNT = 10;
public static final String FLAG_SIGN = "⚑";
public static final String LAND_MINE_SIGN = "☼";
private static final String[][] BOARD = new String[BOARD_ROW_SIZE][BOARD_COLUMN_SIZE];
private static final Integer[][] NEARBY_LAND_MINE_COUNTS = new Integer[BOARD_ROW_SIZE][BOARD_COLUMN_SIZE];
private static final boolean[][] LAND_MINES = new boolean[BOARD_ROW_SIZE][BOARD_COLUMN_SIZE];
public static final String CLOSED_CELL_SIGN = "□";
public static final String OPENED_CELL_SIGN = "■";
// 얘는 ENUM이야
private static int gameStatus = 0; // 0: 게임 중, 1: 승리, -1: 패배
public static void main(String[] args) {
showGameStartComments();
Scanner scanner = new Scanner(System.in);
initializeGame();
while (true) {
showBoard();
// 게임이 이기는 건데, 나같으면 메서드로 추출을 하거나 gamestatus == WIN 뭐 이런식으로
if (doesUserWinTheGame()) {
System.out.println("지뢰를 모두 찾았습니다. GAME CLEAR!");
break;
}
if (doesUserLoseTheGame()) {
System.out.println("지뢰를 밟았습니다. GAME OVER!");
break;
}
String cellInput = getCellInputFromUser(scanner);
String userActionInput = getUserActionInputUser(scanner);
int selectedColIndex = getSelectedColIndex(cellInput);
int selectedRowIndex = getSelectedRowIndex(cellInput);
if (doesUserChooseToPlantFlag(userActionInput)) {
BOARD[selectedRowIndex][selectedColIndex] = FLAG_SIGN;
checkIfAllCellIsOpened();
} else if (doesUserChooseToOpenCell(userActionInput)) {
if (LAND_MINES[selectedRowIndex][selectedColIndex]) {
BOARD[selectedRowIndex][selectedColIndex] = LAND_MINE_SIGN;
changeGameStatusToLost();
continue; // 여기더 early return
} else {
open(selectedRowIndex, selectedColIndex);
}
checkIfAllCellIsOpened();
} else {
System.out.println("잘못된 번호를 선택하셨습니다.");
}
}
}
private static void changeGameStatusToLost() {
gameStatus = -1;
}
private static boolean doesUserChooseToOpenCell(String userActionInput) {
return userActionInput.equals("1");
}
private static boolean doesUserChooseToPlantFlag(String userActionInput) {
return userActionInput.equals("2");
}
private static int getSelectedRowIndex(String cellInput) {
char cellInputRow = cellInput.charAt(1);
return convertRowFrom(cellInputRow);
}
private static int getSelectedColIndex(String cellInput) {
char cellInputCol = cellInput.charAt(0);
return convertColFrom(cellInputCol);
}
private static String getUserActionInputUser(Scanner scanner) {
System.out.println("선택한 셀에 대한 행위를 선택하세요. (1: 오픈, 2: 깃발 꽂기)");
return scanner.nextLine();
}
private static String getCellInputFromUser(Scanner scanner) {
System.out.println();
System.out.println("선택할 좌표를 입력하세요. (예: a1)");
return getUserActionInputUser(scanner);
}
private static boolean doesUserLoseTheGame() {
return gameStatus == -1;
}
private static boolean doesUserWinTheGame() {
return gameStatus == 1;
}
private static void checkIfAllCellIsOpened() {
boolean isAllOpened = isAllOpened();
if (isAllOpened) {
changeGameStatusToWin();
}
}
private static void changeGameStatusToWin() {
gameStatus = 1;
}
private static boolean isAllOpened() {
boolean isAllOpend = true;
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 10; col++) {
if (BOARD[row][col].equals(CLOSED_CELL_SIGN)) {
isAllOpend = false;
}
}
}
return isAllOpend;
}
private static int convertRowFrom(char cellInputRow) {
return Character.getNumericValue(cellInputRow) - 1;
}
private static int convertColFrom(char cellInputCol) {
return switch (cellInputCol) {
case 'a' ->
// selectedColIndex = 0;
// break;
0;
case 'b' -> 1;
case 'c' -> 2;
case 'd' -> 3;
case 'e' -> 4;
case 'f' -> 5;
case 'g' -> 6;
case 'h' -> 7;
case 'i' -> 8;
case 'j' -> 9;
default -> -1;
};// defualt에서 에러를 던져주는 것으로 바꿔줨
}
private static void showBoard() {
System.out.println(" a b c d e f g h i j");
for (int row = 0; row < 8; row++) {
System.out.printf("%d ", row + 1);
for (int col = 0; col < 10; col++) {
System.out.print(BOARD[row][col] + " ");
}
System.out.println();
}
}
private static void initializeGame() {
for (int row = 0; row < BOARD_ROW_SIZE; row++) {
for (int col = 0; col < BOARD_COLUMN_SIZE; col++) {
BOARD[row][col] = CLOSED_CELL_SIGN;
}
}
// for문의 row, col로 변경, 그리고 밑에 메서드명 지뢰심기?
for (int i = 0; i < LAND_MINE_COUNT; i++) {
int col = new Random().nextInt(10);
int row = new Random().nextInt(8);
LAND_MINES[row][col] = true;
}
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 10; col++) {
int count = 0;
if (!LAND_MINES[row][col]) {
// 내 위치 기준으로 왼쪽 대각선위로 지뢰가 있다면 count 증가
if (row - 1 >= 0 && col - 1 >= 0 && LAND_MINES[row - 1][col - 1]) {
count++;
}
if (row - 1 >= 0 && LAND_MINES[row - 1][col]) {
count++;
}
if (row - 1 >= 0 && col + 1 < 10 && LAND_MINES[row - 1][col + 1]) {
count++;
}
if (col - 1 >= 0 && LAND_MINES[row][col - 1]) {
count++;
}
if (col + 1 < 10 && LAND_MINES[row][col + 1]) {
count++;
}
if (row + 1 < 8 && col - 1 >= 0 && LAND_MINES[row + 1][col - 1]) {
count++;
}
if (row + 1 < 8 && LAND_MINES[row + 1][col]) {
count++;
}
if (row + 1 < 8 && col + 1 < 10 && LAND_MINES[row + 1][col + 1]) {
count++;
}
NEARBY_LAND_MINE_COUNTS[row][col] = count;
continue;
}
NEARBY_LAND_MINE_COUNTS[row][col] = 0;
}
}
}
private static void showGameStartComments() {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("지뢰찾기 게임 시작!");
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
private static void open(int row, int col) {
if (row < 0 || row >= 8 || col < 0 || col >= 10) {
return;
}
// 여기서도 뭐 메서드로 추출한다거나, 아니면 if문의 조건을 메서드로 추출
if (!BOARD[row][col].equals(CLOSED_CELL_SIGN)) {
return;
}
if (LAND_MINES[row][col]) {
return;
}
if (NEARBY_LAND_MINE_COUNTS[row][col] != 0) {
BOARD[row][col] = String.valueOf(NEARBY_LAND_MINE_COUNTS[row][col]);
return;
} else {
BOARD[row][col] = OPENED_CELL_SIGN;
}
// 이것 또한 while문이라던가 for문 혹은 stream으로 재귀호출할 수 있도록 변경 가능하지 않나?
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);
}
}