-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinesweeper.java
More file actions
283 lines (262 loc) · 8.05 KB
/
Copy pathMinesweeper.java
File metadata and controls
283 lines (262 loc) · 8.05 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import java.util.*;
public class Minesweeper {
public String[][] squares; //display board
public int[][] squareVals; //value-storing board
public int rows, columns, mines;
public int playState;
public int playMode;
public int svR, svC;
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
final String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*(){}|:<>?[]\',. ";
//default constructor
public Minesweeper() {
playState = 0;
};
//overloaded constructor, set the mine board
public Minesweeper(int r, int c, int m) {
this();
rows = r;
columns = c;
squares = new String[rows+2][columns+2];
squareVals = new int[rows+2][columns+2];
//set placeholder values
for (int i=0; i<rows+2; i++) {
for (int j=0; j<columns+2; j++) {
squareVals[i][j] = 9;
squares[i][j] = "@_@";
}
}
for (int i=1; i<rows+1; i++) {
for (int j=1; j<columns+1; j++) {
squares[i][j] = "█";
squareVals[i][j] = 0;
}
}
//determine mine locations
while(mines != m) {
int rIndex = (int)(Math.random()*(rows)) + 1;
int cIndex = (int)(Math.random()*(columns)) + 1;
if (squareVals[rIndex][cIndex] != -1) {
squareVals[rIndex][cIndex] = -1;
mines ++;
}
}
//set number for each square depending on the # of mines around it
for(int i=1; i<rows+1; i++) {
for (int j=1; j<columns+1; j++) {
if (squareVals[i][j] != -1) {
for (int p=i-1; p<i+2; p++) {
for (int q=j-1; q<j+2; q++) {
if (p != i || q != j) {
if (squareVals[p][q] == -1) {
squareVals[i][j]++;
}
}
}
}
}
}
}
}
//typing playmode
public void processInputI(String e){
int cCor = letters.indexOf(e.substring(0, 1)) + 1;
int rCor;
//check whether flag or reveal
boolean wFlag = e.substring(e.length()-1).toLowerCase().equals("f");
if (wFlag) {
rCor = Integer.parseInt(e.substring(1, e.length()-1));
} else {
rCor = (Integer.parseInt(e.substring(1)));
}
if (squareVals[rCor][cCor] != -1 && !wFlag) {
if (squareVals[rCor][cCor] == 9) { //edge squares
System.out.println("INVALID MOVE. Type 'p' for play guide.");
} else {
reveal(rCor, cCor);
}
} else if (wFlag) {
if (squares[rCor][cCor].equals("█")) { //flag
squares[rCor][cCor] = "⚑";
} else if (squares[rCor][cCor].equals("⚑")){ //unflag
squares[rCor][cCor] = "█";
} else {
System.out.println("INVALID MOVE. Type 'p' for play guide.");
}
} else {
playState = -1;
}
}
//WASD(arrowkey) playmode
public void processInputC(String input) {
for (int i=0; i < input.length(); i++) {
String e = String.valueOf(input.charAt(i));
if (e.toLowerCase().equals("w")) { //move up
if (svR > 1) {
select(svR-1, svC);
} else {}
} else if (e.toLowerCase().equals("a")) { //move left
if (svC > 1) {
select(svR, svC-1);
} else {}
} else if (e.toLowerCase().equals("s")) { //move down
if (svR < rows) {
select(svR+1, svC);
} else {}
} else if (e.toLowerCase().equals("d")) { //move right
if (svC < columns) {
select(svR, svC+1);
}
} else if (e.toLowerCase().equals("e")) { //reveal
if (!(squareVals[svR][svC] == -1)) {
reveal(svR, svC);
} else {
playState = -1;
}
} else if (e.toLowerCase().equals("f")) {
if (squares[svR][svC].equals("█")) { //flag
squares[svR][svC] = "⚑";
} else if (squares[svR][svC].equals("⚑")){ //unflag
squares[svR][svC] = "█";
}
}
}
}
public void select(int rCor, int cCor) {
svR = rCor;
svC = cCor;
}
public void printBoardC() {
for (int i=1; i<svR; i++) {
for (int j=1; j<columns+1; j++) {
System.out.print("|" + squares[i][j]);
}
System.out.println("|");
}
for (int i=1; i<svC; i++) {
System.out.print("|" + squares[svR][i]);
}
System.out.print(ANSI_RED + "|");
System.out.print(ANSI_CYAN_BACKGROUND + squares[svR][svC] + ANSI_RESET);
System.out.print(ANSI_RED + "|" + ANSI_RESET);
for (int i=svC+1; i<columns+1; i++) {
System.out.print(squares[svR][i]+ "|");
}
System.out.println();
for (int i=svR+1; i<rows+1; i++) {
for (int j=1; j<columns+1; j++) {
System.out.print("|" + squares[i][j]);
}
System.out.println("|");
}
}
public void printBoardI() {
System.out.print(" ");
for (int i=0; i<columns; i++) {
System.out.print(" " + letters.substring(i, i+1));
}
System.out.println();
for (int i=1; i<rows+1; i++) {
System.out.print(i);
if (Integer.toString(i).length() > 1) {
System.out.print(" ");
} else {
System.out.print(" ");
}
for (int j=1; j<columns+1; j++) {
System.out.print("|" + squares[i][j] );
}
System.out.println("|");
}
}
public void printBoard(String bkgcolor) {
for (int i=1; i<rows+1; i++) {
for (int j=1; j<columns+1; j++) {
if (!(isRevealed(i, j))) reveal(i, j);
}
}
for (int i=1; i<rows+1; i++) {
for (int j=1; j<columns+1; j++) {
if (squareVals[i][j] != -1) {
System.out.print(bkgcolor + "|" + squares[i][j] );
} else {
System.out.print(bkgcolor + "|☼");
}
}
System.out.println("|" + ANSI_RESET);
}
}
public void reveal(int rCor, int cCor) {
if (squareVals[rCor][cCor] != -1) {
if (squareVals[rCor][cCor] != 0) {
squares[rCor][cCor] = Integer.toString(squareVals[rCor][cCor]);
} else {
squares[rCor][cCor] = " ";
for (int i=rCor-1; i<rCor+2; i++) {
for (int j=cCor-1; j<cCor+2; j++) {
if (i != rCor || j != cCor) {
if (!(isRevealed(i,j))) reveal(i, j);
}
}
}
}
}
}
public boolean isRevealed(int rCor, int cCor) {
return !(squares[rCor][cCor].equals("█") || squares[rCor][cCor].equals("⚑"));
}
public void lose() {
printBoard(ANSI_RED_BACKGROUND);
System.out.println("YOU HIT A MINE BAD JOB");
}
public void checkWin() {
int mCtr = 0;
for (int i=1; i<rows+1; i++) {
for (int j=1; j<columns+1; j++) {
if (squares[i][j].equals("█") || squares[i][j].equals("⚑")) {
mCtr++;
}
}
}
if (mCtr == mines) {
playState = 1;
}
}
public void win() {
printBoard(ANSI_YELLOW_BACKGROUND);
System.out.println("YOU WON GOOD JOB");
}
public void printInstr(int mode){
String tGuide;
String akGuide;
//typing mode
tGuide = "REVEAL: alaphabet/symbol + number (Ex: A1)\n" +
"FLAG/UNFLAG: alaphabet/symbol + number + 'f' (Ex: A1f)";
//arrow key mode
akGuide = "MOVE UP: w" + "\n" +
"MOVE RIGHT: d" + "\n" +
"MOVE DOWN: s" + "\n" +
"MOVE LEFT: a" + "\n" +
"REVEAL: e" + "\n" +
"FLAG/UNFLAG: f" + "\n" +
"(Ex: Type 'dde' to go right 2 squares and reveal it.)";
System.out.print(ANSI_BLUE);
System.out.println("=======================================================");
System.out.println("PLAY GUIDE");
System.out.println("-------------------------------------------------------");
if (mode == 0){
System.out.println(akGuide);
} else {
System.out.println(tGuide);
}
System.out.println("=======================================================");
System.out.println();
System.out.print(ANSI_RESET);
}
}//end of class
// in case we need: ⚑