-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.java
332 lines (281 loc) · 6.97 KB
/
Minesweeper.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package minesweeper;
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Minesweeper extends MIDlet implements CommandListener {
public int height = 10;
public int length = 10;
public Canvas canvas;
public Space[][] spaces;
public Space selected;
public static Command exitCommand = new Command("Exit", Command.EXIT, 0);
public Command okCommand = new Command("Uncover", Command.OK, 1);
public Command newCommand = new Command("New", Command.HELP, 2);
public static Command flagCommand = new Command("Flag", Command.CANCEL, 2);
public String error = "";
public String message = "";
public CommandListener thi = null;
/** state of game: true: playing, false: game ended */
public boolean playing;
public int[][] mines;
public int numMines;
public Display display;
public void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
public void pauseApp() {
}
public void startApp() throws MIDletStateChangeException {
try {
this.newGame();
} catch (Exception e) {
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) {
try {
if(c == exitCommand){
this.exitMIDlet();
} else if(c == okCommand){
this.selected.uncover();
} else if(c == newCommand){
newGame();
} else if(c == flagCommand){
this.selected.flag();
}
} catch (Exception e) {
this.p("hi");
e.printStackTrace();
}
}
/** Get space based on row and col
* @param col col to get
* @param row row to get
* @return Space instace of result */
public Space getSpace(int col, int row){
return spaces[col][row];
}
public void uncoverMines(){
this.display.vibrate(10000);
this.p("3.0");
Space[] one = {this.selected};
Space[][] toExplode = new Space[this.height+1][this.height*2];
toExplode[1] = one;
int tElen = 0;
this.p("3.1");
for(int i=2; i<this.height+1; i++){
p("her");
int range[][] = generateRanges(i, this.selected.col, this.selected.row);
p(i);
p("3.1."+Integer.toString(i));
p(range.length);
p("swq");
int tE = 0;
for(int t=0; t<range.length; t++){
if(spaceExists(range[t][0], range[t][1])){
Space s = getSpace(range[t][0], range[t][1]);
toExplode[i][tE] = s;
tE++;
}
}
}
this.p("3.2");
for(int i=0; i<toExplode.length; i++){
for(int s=0; s<toExplode[i].length; s++){
if(toExplode[i][s] != null){
toExplode[i][s].explode();
}
}
try {
this.canvas.repaint();
this.canvas.serviceRepaints();
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.p("3.3");
for(int i=0; i<toExplode.length; i++){
for(int s=0; s<toExplode[i].length; s++){
if(toExplode[i][s] != null){
toExplode[i][s].stopExplode();
}
}
try {
this.canvas.repaint();
this.canvas.serviceRepaints();
Thread.sleep(7);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.p("3.4");
this.display.vibrate(0);
}
public int[][] generateRanges(int distance, int col, int row){
p("r");
int len = 8*(distance-1)+4;
int[][] range = new int[len][2];
int[][] relRange = new int[len][2];
int dist = (distance*2)-1;
int nstart = (distance-1)*-1;
int pstart = distance-1;
int c=0;
for(int i=0; i<dist; i++){ //top
range[c][0] = i+nstart;
range[c][1] = pstart;
c++;
}
for(int i=0; i<dist; i++){ //bottom
range[c][0] = i+nstart;
range[c][1] = nstart;
c++;
}
for(int i=0; i<dist; i++){ //left
range[c][0] = nstart;
range[c][1] = i+nstart;
c++;
}
for(int i=0; i<dist; i++){ //right
range[c][0] = pstart;
range[c][1] = i+nstart;
c++;
}
for(int i=0; i<c; i++){
relRange[i][0] = range[i][0] + col;
relRange[i][1] = range[i][1] + row;
}
p("t");
p(c);
p(relRange.length);
return relRange;
}
public boolean spaceExists(int col, int row){
if(col >= 0 && col<this.length && row >= 0 && row<this.height){
return true;
} else {
return false;
}
}
public boolean uncoveredRest(){
for(int col=0; col<spaces.length; col++){
for(int row=0; row<spaces[col].length; row++){
if(spaces[col][row].value != 9 && spaces[col][row].open == false){
return false;
}
}
}
return true;
}
public void lose(){
this.uncoverMines();
message = "You Lose!";
this.playing = false;
canvas.removeCommand(flagCommand);
canvas.addCommand(exitCommand);
canvas.setCommandListener(thi);
canvas.repaint();
}
public void win(){
message = "You Win!";
this.playing = true;
canvas.removeCommand(flagCommand);
canvas.addCommand(exitCommand);
canvas.setCommandListener(thi);
canvas.repaint();
}
public void exitMIDlet() {
notifyDestroyed();
}
public static void p(int i){
System.out.println(i);
}
public void p(String s){
System.out.println(s);
}
public void newGame(){
this.display = Display.getDisplay(this);
this.spaces = this.generate();
spaces[0][0].gohere();
this.message = "";
this.playing = true;
this.canvas = new MCanvas(this);
this.p("letso");
this.canvas.addCommand(okCommand);
this.canvas.addCommand(newCommand);
this.canvas.addCommand(flagCommand);
this.canvas.setCommandListener(this);
this.thi = this;
this.display.setCurrent(this.canvas);
/*
canvas.repaint();
*/
this.p("endNEW");
}
public Space[][] generate(){
p("generate");
int[][] mines = this.generateMines();
p("here");
Space[][] data2 = new Space[this.length][this.height];
for(int c=0; c<this.length; c++){
for(int r=0; r<this.height; r++){
int value;
if(mines[c][r] == 1){
value = 9;
} else {
int surrounding = 0;
if(c>0){
surrounding += mines[c-1][r];
}
if(c<this.length-1){
surrounding += mines[c+1][r];
}
if(r>0){
surrounding += mines[c][r-1];
}
if(r<this.height-1){
surrounding += mines[c][r+1];
}
if(c>0 && r>0){
surrounding += mines[c-1][r-1];
}
if(c>0 && r<this.height-1){
surrounding += mines[c-1][r+1];
}
if(c<this.length-1 && r>0){
surrounding += mines[c+1][r-1];
}
if(c<this.length-1 && r<this.height-1){
surrounding += mines[c+1][r+1];
}
value = surrounding;
}
data2[c][r] = new Space(c, r, value, this);
}
}
this.p("returning");
return data2;
}
/**
* Generates a grid of mines
* @return int[] array of int[] arrays. 1=mine, 0=no mine. (int for purposes of adding up)
*/
public int[][] generateMines(){
System.out.println("generateMines()");
this.numMines = (int) ((this.height+this.length)/2*1.2);
this.mines = new int[this.numMines][2];
int[][] data = new int[this.length][this.height];
Random rand = new Random();
for(int i=0; i<this.numMines; i++){
this.p("1.0");
int c = (int) (rand.nextFloat()*this.length);
int r = (int) (rand.nextFloat()*this.height);
data[c][r] = 1;
this.p("1.1");
this.mines[i][0] = c;
this.mines[i][1] = r;
this.p("1.2");
}
this.p("out");
return data;
}
}