-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMineMap.java
More file actions
472 lines (383 loc) · 11.3 KB
/
MineMap.java
File metadata and controls
472 lines (383 loc) · 11.3 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.Arrays;
/**
* Class: MineMap
*/
class MineMap {
private int xDimension;
private int yDimension;
private int minePixelWidth;
private int [][] mineMap;
/* Mine field states */
public final int CLOSED = 9;
public final int MINE = 88;
public final int OFF_MAP = 11;
public final int EMPTY = 0;
public final int ONE_M = 1;
public final int TWO_M = 2;
public final int THREE_M = 3;
public final int FOUR_M = 4;
public final int FIVE_M = 5;
public final int SIX_M = 6;
public final int SEVEN_M = 7;
public final int EIGHT_M = 8;
private class ColorInfo {
public int red;
public int blue;
public int green;
}
private final int BYTE = 8;
private final int RED_MASK = 0x00FF0000;
private final int GREEN_MASK = 0x0000FF00;
private final int BLUE_MASK = 0x000000FF;
private final int RED_SHIFT = BYTE * 2;
private final int GREEN_SHIFT = BYTE * 1;
/**
* Function: initMineMap
* Input: MineSweeperField mineField
* Output: /
* Description: This function initializes mine map
*/
public void initMineMap( MineSweeperField mineField )
{
int i , j;
xDimension = mineField.getDimensionX();
yDimension = mineField.getDimensionY();
minePixelWidth = mineField.getWidthOfOneField();
mineMap = new int[yDimension][xDimension];
/* Init map */
for( i=0; i < yDimension; i++)
for( j=0 ; j < xDimension ; j++)
mineMap[i][j] = CLOSED;
return;
}
public int getFieldValue( int x , int y )
{
if( (x<0) || (y<0) ) return OFF_MAP;
if( (x >= xDimension ) ||(y >= yDimension)) return OFF_MAP;
return mineMap[y][x];
}
public void setMineOnMap( int x , int y )
{
if( (x<0) || (y<0) ) return;
if( (x >= xDimension ) ||(y >= yDimension)) return;
mineMap[y][x] = MINE;
return;
}
public void printMineMap()
{
for( int i=0; i < yDimension; i++) {
for( int j=0 ; j < xDimension ; j++) {
System.out.printf("%2d ", mineMap[i][j]);
}
System.out.printf("\n");
}
}
/**
* Function: updateMineMap
* Input: MineSweeperField mineField
* Output: /
* Description: This function updates mine map
*/
public void updateMineMap( MineSweeperField mineField )
{
BufferedImage mineFieldImage;
/* Delay function */
try {
Thread.sleep(40);
} catch(InterruptedException ex) {
System.out.println("Thread.sleep() Failed!");
}
mineFieldImage = mineField.captureMineField();
readMineMap( mineFieldImage , 0 , 0 );
readMineMap( mineFieldImage , 1 , 0 );
/* Debug */
/*
debugPrintImage( mineFieldImage , "mineMap.bmp" );
*/
return;
}
/**
* Function: readMineMap
* Input: mineFieldImage - screen shot of mine field
* xOffset - corection factor in x direction
* yOffset - corection factor in y direction
* Output: /
* Description: This function sets mine map according to mine field image
*/
private void readMineMap( BufferedImage mineFieldImage , int xOffset , int yOffset )
{
int j , i;
int centerOfMine;
centerOfMine = minePixelWidth / 2;
for( i=0; i < yDimension ; i++) {
for( j=0 ; j < xDimension ; j++) {
if( mineMap[i][j] != CLOSED ) continue; /* If already set skip this field */
mineMap[i][j] = readOneField( centerOfMine + (j*minePixelWidth) + xOffset ,
centerOfMine + (i*minePixelWidth) + yOffset ,
mineFieldImage );
}
}
return;
}
/**
* Function: readOneField
* Input: x -
* y -
* mineFieldImage - screen shot of mine field
* Output: EMPTY, CLOSED, ONE_M ...
* Description: This function returns value of one field
*/
private int readOneField( int x , int y , BufferedImage mineFieldImage )
{
int i, j, k;
int retVal = 1;
int xOffset, yOffset;
int xStart, yStart;
int [] sampledPixels = new int[9];
xOffset = minePixelWidth / 4;
yOffset = minePixelWidth / 4;
xStart = x - xOffset;
yStart = y - yOffset;
/* Sample 3x3 array around center of mine field */
for( i=0,k=0 ; i<3 ; i++ ) {
for( j=0 ; j<3 ; j++ ) {
sampledPixels[k] = mineFieldImage.getRGB(xStart+(j*xOffset), yStart+(i*yOffset));
k++;
}
}
retVal = getValue( sampledPixels );
/* debug */
/*
switch( retVal ) {
case EMPTY :
debugMarkDot( x , y , 0x000000FF , mineFieldImage );
break;
case ONE_M :
debugMarkDot( x , y , 0x0000FF00 , mineFieldImage );
break;
case TWO_M :
debugMarkDot( x , y , 0x00FF0000 , mineFieldImage );
break;
case THREE_M :
debugMarkDot( x , y , 0x00FFFF00 , mineFieldImage );
break;
}
*/
return retVal;
}
/**
* Function: getValue
* Input:
* Output: EMPTY, CLOSED, ONE_M ...
* Description: This function returns value of one field
*/
private int getValue( int [] pixelArray )
{
ColorInfo colorAverage = new ColorInfo();
ColorInfo colorMax = new ColorInfo();
ColorInfo colorMin = new ColorInfo();
/* Calcualte some info from sampled array */
getAverageForRGB( pixelArray , colorAverage );
getMaxForRGB( pixelArray , colorMax );
getMinForRGB( pixelArray , colorMin );
/* Dont change order of next statments */
if( isFieldEmpty( colorMin , colorMax )) return EMPTY;
if( isFieldTwo( colorMin , colorMax )) return TWO_M;
if( isFieldOne( colorMin , colorMax )) return ONE_M;
if( isFieldThree( colorMin , colorMax )) return THREE_M;
if( isFieldFour( colorMin , colorMax )) return FOUR_M;
/*
if( isFieldFive( colorMin , colorMax )) return FIVE_M;
*/
return CLOSED;
}
/**
* Function: isFieldOne
*/
private boolean isFieldOne( ColorInfo colorMin , ColorInfo colorMax )
{
if( ((colorMax.red - colorMin.red) > 90) &&
((colorMax.green - colorMin.green) > 90) &&
((colorMax.blue - colorMin.blue) < 60 )) {
return true;
}
return false;
}
/**
* Function: isFieldTwo
*/
private boolean isFieldTwo( ColorInfo colorMin , ColorInfo colorMax )
{
if( (colorMin.red < 50 ) &&
(colorMin.blue < 20) &&
(colorMax.green > 90 )) {
return true;
}
return false;
}
/**
* Function: isFieldThree
*/
private boolean isFieldThree( ColorInfo colorMin , ColorInfo colorMax )
{
if( ((colorMax.red - colorMin.red) < 65) &&
((colorMax.green - colorMin.green) > 190) &&
((colorMax.blue - colorMin.blue) > 190 )) {
return true;
}
return false;
}
/**
* Function: isFieldFour
*/
private boolean isFieldFour( ColorInfo colorMin , ColorInfo colorMax )
{
if( (colorMin.red < 10) &&
(colorMin.green < 10) &&
(colorMax.blue > 110)) {
return true;
}
return false;
}
/**
* Function: isFieldFive
*/
private boolean isFieldFive( ColorInfo colorMin , ColorInfo colorMax )
{
if( (colorMax.red > 100) &&
(colorMin.green < 10) &&
(colorMin.blue < 10)) {
return true;
}
return false;
}
/**
* Function: TODO other numbers
*/
/**
* Function: isFieldEmpty
*/
private boolean isFieldEmpty( ColorInfo colorMin , ColorInfo colorMax )
{
if( ((colorMax.red - colorMin.red) < 25) &&
((colorMax.blue - colorMin.blue) < 15) &&
((colorMax.green - colorMin.green) < 25)) {
return true;
}
return false;
}
/**
* Function: getAverageForRGB
* Input: pixelArray -
* colorAverage - for return
* Output: /
* Description: This function calculates average for each color commponent
* from provided pixel array
*/
private void getAverageForRGB( int [] pixelArray , ColorInfo colorAverage )
{
int i;
int redAvg = 0;
int blueAvg = 0;
int greenAvg = 0;
/* Sum up by color component */
for( i=0 ; i < pixelArray.length ; i++ ) {
blueAvg = blueAvg + ((pixelArray[i]&BLUE_MASK));
redAvg = redAvg + ((pixelArray[i]&RED_MASK) >> RED_SHIFT);
greenAvg = greenAvg + ((pixelArray[i]&GREEN_MASK) >> GREEN_SHIFT);
}
/* Calculate average */
colorAverage.red = redAvg / pixelArray.length;
colorAverage.blue = blueAvg / pixelArray.length;
colorAverage.green = greenAvg / pixelArray.length;
return;
}
/**
* Function: getMinForRGB
* Input: pixelArray -
* colorMin - for return
* Output: /
* Description: This function calculates minimum for each color commponent
* from provided pixel array
*/
private void getMinForRGB( int [] pixelArray , ColorInfo colorMin )
{
int [] tempArray = new int[pixelArray.length];
tempArray = maskPixelArray( pixelArray , RED_MASK , RED_SHIFT );
Arrays.sort(tempArray);
colorMin.red = tempArray[0] ;
tempArray = maskPixelArray( pixelArray , GREEN_MASK , GREEN_SHIFT );
Arrays.sort(tempArray);
colorMin.green = tempArray[0] ;
tempArray = maskPixelArray( pixelArray , BLUE_MASK , 0 );
Arrays.sort(tempArray);
colorMin.blue = tempArray[0];
return;
}
/**
* Function: getMaxForRGB
* Input: pixelArray -
* colorMax - for return
* Output: /
* Description: This function calculates maximum for each color commponent
* from provided pixel array
*/
private void getMaxForRGB( int [] pixelArray , ColorInfo colorMax )
{
int [] tempArray = new int[pixelArray.length];
tempArray = maskPixelArray( pixelArray , RED_MASK , RED_SHIFT );
Arrays.sort(tempArray);
colorMax.red = tempArray[ tempArray.length - 1] ;
tempArray = maskPixelArray( pixelArray , GREEN_MASK , GREEN_SHIFT );
Arrays.sort(tempArray);
colorMax.green = tempArray[ tempArray.length - 1] ;
tempArray = maskPixelArray( pixelArray , BLUE_MASK , 0 );
Arrays.sort(tempArray);
colorMax.blue = tempArray[ tempArray.length - 1];
return;
}
/**
* Function: maskPixelArray
* Input: pixelArray -
* colorMask - select color component
* colorShift -
* Output: maskedArray
* Description: This function masks pixel array
*/
private int [] maskPixelArray( int [] pixelArray , int colorMask , int colorShift )
{
int [] maskedArray = new int[pixelArray.length];
for( int i=0 ; i < pixelArray.length ; i++ ) {
maskedArray[i] = (pixelArray[i]&colorMask) >> colorShift;
}
return maskedArray;
}
/**
* Debug functions
*/
private void debugPrintImage( BufferedImage image , String fileName )
{
File outputFile = new File( fileName );
try {
ImageIO.write( image , "bmp" , outputFile );
} catch ( IOException e ) {
System.out.println("Cannot write image to file!");
return;
}
System.out.println("Debug: Screen shot saved to file " + fileName +"!");
return;
}
private void debugMarkDot( int x , int y , int color , BufferedImage image )
{
image.setRGB( x, y , color );
image.setRGB( x+1, y , color );
image.setRGB( x-1, y , color );
image.setRGB( x, y+1 , color );
image.setRGB( x, y-1 , color );
return;
}
}