-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMineSweeperField.java
More file actions
450 lines (346 loc) · 11.7 KB
/
MineSweeperField.java
File metadata and controls
450 lines (346 loc) · 11.7 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
import java.awt.*;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.Arrays;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
/**
* Class: MineSweeperField
*/
class MineSweeperField {
private int xDimension;
private int yDimension;
private int xPixelPosition;
private int yPixelPosition;
private int fieldPixelWidth;
private boolean debugSwitch;
private class GridLines {
public int numberOfLines;
public int firstLineOffset;
public int distanceBetweenLines;
}
/**
* Function: captureMineField
* Input: /
* Output: Screen shot of users screen
* Description: This function returns screen shot of users screen
*/
public BufferedImage captureMineField()
{
Rectangle mineFieldRect;
BufferedImage mineFieldImage;
mineFieldImage = new BufferedImage( 1 , 1 , 1);
mineFieldRect = new Rectangle( xPixelPosition , yPixelPosition ,
xDimension * fieldPixelWidth , yDimension * fieldPixelWidth );
/* Make screen shot of users screen */
try {
mineFieldImage = new Robot().createScreenCapture( mineFieldRect );
} catch ( AWTException awte ) {
System.out.println("Cannot capture screen!");
}
if( debugSwitch ) {
debugPrintImage( mineFieldImage , "mineField.bmp" );
}
return mineFieldImage;
}
/**
* Function: findMineField
* Input: /
* Output: Screen shot of users screen
* Description: This function returns screen shot of users screen
*/
public boolean findMineField()
{
boolean retVal;
boolean rowFound, colFound;
BufferedImage screenShot;
screenShot = getScreenShot();
colFound = findGridColumns( screenShot );
rowFound = findGridRows( screenShot );
if( colFound && rowFound ) {
retVal = true;
} else {
System.out.println("Mine field not found!");
return false;
}
System.out.println(" ---------------------------------------");
System.out.println(" Grid coordinates: X: " + xPixelPosition + " Y: " + yPixelPosition );
System.out.println(" Number of columns: " + xDimension + " | Number of rows: " + yDimension );
System.out.println(" Width of mine field in pixels: " + fieldPixelWidth );
return retVal;
}
/**
* Function: findGridColumns
* Input: screenShot - buffered image of users screen
* Output: true or false
* Description: This function sets class variables connected to mine field grid columns
*/
private boolean findGridColumns( BufferedImage screenShot )
{
boolean retVal;
GridLines gridLines = new GridLines();
retVal = findVerticalGridLines( screenShot , gridLines );
/* Set class variables */
xDimension = gridLines.numberOfLines;
xPixelPosition = gridLines.firstLineOffset;
fieldPixelWidth = gridLines.distanceBetweenLines;
if( debugSwitch ) {
debugPrintImage( screenShot , "gridCol.bmp" );
}
return retVal;
}
/**
* Function: findGridRows
* Input: screenShot - buffered image of users screen
* Output: true or false
* Description: This function sets class variables connected to mine field grid rows
*/
private boolean findGridRows( BufferedImage screenShot )
{
int i , j;
boolean retVal;
final int IMG_WIDTH;
final int IMG_HEIGHT;
BufferedImage rotScreenShot;
GridLines gridLines = new GridLines();
IMG_WIDTH = screenShot.getWidth();
IMG_HEIGHT = screenShot.getHeight();
/* Rotate screenshot for re-use of findVerticalGridLines function */
rotScreenShot = new BufferedImage( IMG_HEIGHT , IMG_WIDTH , screenShot.getType() );
for( i = 0 ; i< IMG_WIDTH ; i++ ) {
for( j=0; j < IMG_HEIGHT ; j++ ) {
rotScreenShot.setRGB( IMG_HEIGHT -1 - j , i , screenShot.getRGB(i,j));
}
}
/* Same procedure as in method findGridColumns */
retVal = findVerticalGridLines( rotScreenShot , gridLines );
yDimension = gridLines.numberOfLines;
fieldPixelWidth = gridLines.distanceBetweenLines;
/* Set top-left possition */
yPixelPosition = rotScreenShot.getWidth() - ( gridLines.firstLineOffset + ( fieldPixelWidth * yDimension ));
if( debugSwitch ) {
debugPrintImage( rotScreenShot , "gridRow.bmp" );
}
return retVal;
}
/**
* Function: findVerticalGridLines
* Input: screenShot
* gridLines
* Output: true or false
* Description: This function returns screen shot of users screen
*/
private boolean findVerticalGridLines( BufferedImage screenShot, GridLines gridLines )
{
int i;
int xPoss, yPoss;
int curCol;
int okCounter;
int pixelDiff;
int medianIndex;
int prevPixelDiff = 0;
int columnsMatch = 0;
int maxColumnsMatch = 0;
int maxColumnPixelDiff = 0;
int maxColumnStartIndex = 0;
final int EMPTY = (-1);
final int VERTICAL_STEP;
final int IMG_WIDTH;
final int IMG_HEIGHT;
final int MAX_NUM_OF_COL = 300;
int[] columnOffset = new int[MAX_NUM_OF_COL];
final int STAT_DATA_LEN = 100;
int statDataIndex = 0;
int[] startCoordinate = new int[STAT_DATA_LEN];
int[] numberOfColumns = new int[STAT_DATA_LEN];
int[] oneColumnPixelWidth = new int[STAT_DATA_LEN];
/* Init */
for( i=0 ; i<STAT_DATA_LEN ; i++ ) {
startCoordinate[i] = EMPTY;
numberOfColumns[i] = EMPTY;
oneColumnPixelWidth[i] = EMPTY;
}
VERTICAL_STEP = 10;
IMG_WIDTH = screenShot.getWidth();
IMG_HEIGHT = screenShot.getHeight();
/*************************************************************************/
/* Find mine grid */
/*************************************************************************/
/* Move down the lines with VERTICAL_STEP pixel step */
for( yPoss = 0 ; yPoss < IMG_HEIGHT ; yPoss = yPoss + VERTICAL_STEP ) {
okCounter = 0;
for( i = 0 ; i < MAX_NUM_OF_COL ; i++ ) columnOffset[i] = EMPTY;
/* Check line for columns */
for( xPoss = 0 , curCol = 1 ; xPoss < IMG_WIDTH ; xPoss++ ) {
if( isColorForGrid( screenShot.getRGB( xPoss , yPoss )) ) {
/* Increase consecutive counter - for filtering grid rows */
if( okCounter > 0 ) continue;
else okCounter++;
/* Debug - paint found pixel red */
if( debugSwitch ) {
screenShot.setRGB( xPoss , yPoss , 0x00FF0000 );
}
/* Store column possition */
columnOffset[curCol] = xPoss;
curCol = curCol + 1;
if( curCol == MAX_NUM_OF_COL ) break;
/* When found move five pixels */
xPoss = xPoss + 5;
} else {
okCounter = 0; /* Reset OK counter */
}
} /* END Check line for grid columns */
/* Find consecutive columns with same pixel differance - possible grid */
for( curCol = 1 , columnsMatch = 0 ; curCol<MAX_NUM_OF_COL ; curCol++ ) {
if( columnOffset[curCol] == EMPTY ) {
break;
}
pixelDiff = columnOffset[curCol] - columnOffset[curCol-1];
/* Use tolerance of 4 pixels */
if( Math.abs( pixelDiff - prevPixelDiff ) < 4 ) {
columnsMatch++;
if( columnsMatch > maxColumnsMatch ) {
maxColumnsMatch = columnsMatch;
maxColumnStartIndex = curCol - columnsMatch ;
maxColumnPixelDiff = (columnOffset[curCol] - columnOffset[maxColumnStartIndex]) / maxColumnsMatch;
}
} else {
columnsMatch = 1;
}
prevPixelDiff = pixelDiff;
}
/*
* If numbers of columns are in possible MineSweeper limits, than
* store first column x-coordinate and number of grid columns
*/
if( (maxColumnsMatch > 7) && (maxColumnsMatch < 32) ) {
/* Store number of columns */
numberOfColumns[statDataIndex] = maxColumnsMatch;
/* Store first column x-coordinate */
startCoordinate[statDataIndex] = columnOffset[maxColumnStartIndex];
/* Store pixel width of column */
oneColumnPixelWidth[statDataIndex] = maxColumnPixelDiff;
statDataIndex++;
if( statDataIndex == STAT_DATA_LEN ) {
break;
}
}
maxColumnsMatch = 0;
maxColumnPixelDiff = 0;
maxColumnStartIndex = 0;
} /* END Move down the lines */
/* Check if grid was found */
if( statDataIndex < 7 ) {
return false;
}
/*************************************************************************/
/* Statisticaly handle data with median filter */
/*************************************************************************/
Arrays.sort( numberOfColumns , 0 , statDataIndex );
Arrays.sort( startCoordinate , 0 , statDataIndex );
Arrays.sort( oneColumnPixelWidth , 0 , statDataIndex );
medianIndex = statDataIndex / 2;
gridLines.numberOfLines = numberOfColumns[ medianIndex ];
gridLines.firstLineOffset = startCoordinate[ medianIndex ];
gridLines.distanceBetweenLines = oneColumnPixelWidth[ medianIndex ];
return true;
}
/**
* Function: debugPrintImage
* Input: image - buffered image
* fileName - file name
* Output: /
* Description: This function writes image to .bmp file
*/
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;
}
/**
* Function: getScreenShot
* Input: /
* Output: Screen shot of users screen
* Description: This function returns screen shot of users screen
*/
private BufferedImage getScreenShot()
{
Toolkit toolKit;
Dimension wholeScreenDim;
Rectangle wholeScreenRet;
BufferedImage screenShot;
screenShot = new BufferedImage( 1 , 1 , 1);
toolKit = Toolkit.getDefaultToolkit();
wholeScreenDim = toolKit.getScreenSize();
wholeScreenRet = new Rectangle( 0 , 0 , wholeScreenDim.width , wholeScreenDim.height );
/* Make screen shot of users screen */
try {
screenShot = new Robot().createScreenCapture( wholeScreenRet );
} catch ( AWTException awte ) {
System.out.println("Cannot capture screen!");
}
return screenShot;
}
/**
* Function: isColorForGrid
* Input: hexColor -
* Output: True or false
* Description: This function determines if pixel is wright color to be part of minesweeper grid
*/
private boolean isColorForGrid( int hexPixelColor )
{
/* Red componet in borders */
if( ((hexPixelColor & 0x00FF0000) < 0x003F0000 ) &&
((hexPixelColor & 0x00FF0000) > 0x00001E00 )) {
/* Green commponent in borders */
if( ((hexPixelColor & 0x0000FF00) < 0x00006E00 ) &&
((hexPixelColor & 0x0000FF00) > 0x00001000 )) {
/* Blue commponent in borders */
if( ((hexPixelColor & 0x000000FF) < 0x0000008E )) {
return true;
}
}
}
return false;
}
/**
* Function: setDebug
* Input: state - true or false
* Output: /
* Description: This function handles debugSwitch
*/
public void setDebug( boolean state )
{
debugSwitch = state;
return;
}
/**
* Class data access functions
*/
public int getDimensionX() {
return xDimension;
}
public int getDimensionY() {
return yDimension;
}
public int getStartPosX() {
return xPixelPosition;
}
public int getStartPosY() {
return yPixelPosition;
}
public int getWidthOfOneField() {
return fieldPixelWidth;
}
}