-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMineSweeperGame.java
174 lines (156 loc) · 4.54 KB
/
MineSweeperGame.java
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
public class MineSweeperGame {
// size of ROWS + 2, COLUMNS + 2
private boolean[][] board; // true if mine present
private boolean[][] checked; // true if user checked
private int ROWS;
private int COLS;
private int MINES;
private int OPENED = 0;
static final int[] dx = { -1, -1, -1, 0, 0, 1, 1, 1 };
static final int[] dy = { 1, 0, -1, 1, -1, 1, 0, -1 };
/**
* Constructor with no arguments. Makes a game with board of 7 x 7.
*/
public MineSweeperGame() {
this(7, 7);
}
/**
* Constructor with one argument. Makes a game with board of 5 x 5 and
* numOfMines.
*
* @param numOfMines
*/
public MineSweeperGame(int numOfMines) {
this(7, 7, numOfMines);
}
/**
*
* @param rows
* @param columns
*/
public MineSweeperGame(int rows, int columns) {
this(rows, columns, rows * columns / 7);
}
/**
*
* @param rows
* @param columns
* @param mines
*/
public MineSweeperGame(int rows, int columns, int mines) {
ROWS = rows;
COLS = columns;
MINES = mines;
createBoard();
}
public boolean CheckBoard(int x, int y) {
if (x <= 0 || y <= 0 || x >= ROWS + 1 || y >= COLS + 1) {
System.out.println("Cannot Check!");
return false;
}
if (checked[x][y]) {
System.out.println("Already Checked!");
return false;
}
if (board[x][y]) {
System.out.println("Boom!");
return true;
} else {
CheckLeftMines(x, y);
if ((OPENED + MINES) == (ROWS * COLS)) {
System.out.println("You Win!");
return true;
}
return false;
}
}
private void CheckLeftMines(int x, int y) {
OPENED++;
checked[x][y] = true;
for (int i = 0; i < 8; i++) {
if (countMines(x, y) == 0 && !board[x + dx[i]][y + dy[i]] && !checked[x + dx[i]][y + dy[i]]) {
CheckLeftMines(x + dx[i], y + dy[i]);
}
}
}
private void createBoard() {
board = new boolean[ROWS + 2][COLS + 2];
checked = new boolean[ROWS + 2][COLS + 2];
generateWalls();
generateMines();
}
private void generateWalls() {
for (int i = 0; i < ROWS + 2; i++) {
checked[0][i] = true;
checked[COLS + 1][i] = true;
}
for (int i = 0; i < COLS + 2; i++) {
checked[i][0] = true;
checked[i][ROWS + 1] = true;
}
}
private void generateMines() {
int count = 0;
while (count < MINES) {
int x = (int) (Math.random() * ROWS + 1);
int y = (int) (Math.random() * COLS + 1);
if (!board[x][y]) {
count++;
board[x][y] = true;
}
}
}
// mine은 *로, 나머지 타일은 숫자로 표시하는 코드
public void display() {
for (int i = 1; i <= ROWS; i++) {
for (int j = 1; j <= COLS; j++) {
if (!checked[i][j]) {
System.out.print("@ ");
} else {
System.out.print(countMines(i, j) + " ");
}
}
System.out.println();
}
}
// mine은 *로, 나머지 타일은 숫자로 표시하는 코드
public void printBoardStatus() {
for (int i = 1; i <= ROWS; i++) {
for (int j = 1; j <= COLS; j++) {
if (board[i][j]) {
System.out.print("* ");
} else {
System.out.print(countMines(i, j) + " ");
}
}
System.out.println();
}
}
// 타일(x, y) 주변에 지뢰가 얼마나 있는지 세는 코드
private int countMines(int x, int y) {
int count = 0;
for (int i = 0; i < 8; i++) {
if (board[x + dx[i]][y + dy[i]]) {
count++;
}
}
return count;
}
// board의 전체 상태를 보여주는 코드
public void printBoard() {
for (boolean[] row : board) {
for (boolean el : row) {
System.out.print(el + " ");
}
System.out.println();
}
}
public void printCheck() {
for (boolean[] row : checked) {
for (boolean el : row) {
System.out.print(el + " ");
}
System.out.println();
}
}
}