-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
340 lines (307 loc) · 9.49 KB
/
index.js
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
// Get pixel color under the mouse.
var fs = require("fs");
var robot = require("robotjs");
var windowManipulation = require("../window-manipulation");
windowManipulation.getInfo("winmine").then(function (winpos) {
console.log(winpos);
// Get mouse position.
var mouse = robot.getMousePos();
// Get pixel color in hex format.
var hex = robot.getPixelColor(mouse.x, mouse.y);
console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y);
const UNKNOWN = 9;
const MINE = 13;
const FLAG = 11;
const CLICKED = 14;
const FAILMINE = 10;
//Returns whether the smiley has glasses
function isWin(img) {
var hex = img.colorAt(140, 74);
if (hex == "000000") {
console.log("win");
return true;
}
return false;
}
function getGrid() {
console.log(winpos.TopLeft.X);
console.log(winpos.TopLeft.Y);
console.log(winpos.Width);
console.log(winpos.Height);
var img = robot.screen.capture(winpos.TopLeft.X, winpos.TopLeft.Y, winpos.Size.Width, winpos.Size.Height);
if (isWin(img)) {
return [];
}
let cases = [];
for (let y=0; y<16; y++) {
cases[y] = Array(16).fill(UNKNOWN);
for (let x=0; x<16; x++) {
//robot.moveMouse(22+16*x, 110+16*y);
cases[y][x] = getKind(img, 15+16*x, 100+16*y);
}
}
return cases;
}
function getAjustedGrid(grid) {
let cases = [];
for (let y=0; y<16; y++) {
cases[y] = Array(16).fill(UNKNOWN);
for (let x=0; x<16; x++) {
cases[y][x] = getAjustedValue(grid, x, y);
}
}
return cases;
}
function getAjustedValue(grid, x, y) {
let value = grid[y][x];
if (value > 0 && value < 6) value -= countFlag(grid, x, y);
return value;
}
function getValue(grid, x, y) {
let value = grid[y][x];
return value;
}
function countFlag(grid, x, y) {
let nb = 0;
if (isFlag(grid, x-1, y-1)) nb++;
if (isFlag(grid, x+0, y-1)) nb++;
if (isFlag(grid, x+1, y-1)) nb++;
if (isFlag(grid, x-1, y+0)) nb++;
if (isFlag(grid, x+1, y+0)) nb++;
if (isFlag(grid, x-1, y+1)) nb++;
if (isFlag(grid, x+0, y+1)) nb++;
if (isFlag(grid, x+1, y+1)) nb++;
return nb;
}
function countClickable(grid, x, y) {
let nb = 0;
if (isClickable(grid, x-1, y-1)) nb++;
if (isClickable(grid, x+0, y-1)) nb++;
if (isClickable(grid, x+1, y-1)) nb++;
if (isClickable(grid, x-1, y+0)) nb++;
if (isClickable(grid, x+1, y+0)) nb++;
if (isClickable(grid, x-1, y+1)) nb++;
if (isClickable(grid, x+0, y+1)) nb++;
if (isClickable(grid, x+1, y+1)) nb++;
return nb;
}
function isSunrrounded(grid, x, y) {
let nb = getSunrrounded(grid, x, y);
return countClickable(grid, x, y) == nb;
}
function getSunrrounded(grid, x, y) {
if (x == 0 && y ==0) return 3;
if (x == 0 && y ==15) return 3;
if (x == 15 && y ==0) return 3;
if (x == 15 && y ==15) return 3;
if (x == 0 || x ==15) return 5;
if (y == 0 || y ==15) return 5;
return 8;
}
function isFlag(grid, x, y) { //flag
return y>=0 && x>=0 && y<16 && x<16 && grid[y][x] == FLAG;
}
function isClickable(grid, x, y) { //clickable
return y>=0 && x>=0 && y<16 && x<16 && grid[y][x] == 0;
}
function isNumber(grid, x, y) {
return y>=0 && x>=0 && y<16 && x<16 && grid[y][x] > 0 && grid[y][x] < 9;
}
function getProba(grid, x, y) {
var probaMine = 0;
var probaVide = 0;
if (grid[y][x]==0) {
probaMine = 0;
probaVide = 0;
if (isSunrrounded(grid, x, y)) probaMine = 0;
if (isNumber(grid, x-1, y-1)) {
let dd=getValue(grid, x-1, y-1) / (countClickable(grid, x-1, y-1) + countFlag(grid, x-1, y-1));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x-1, y-1)-countFlag(grid, x-1, y-1)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
if (isNumber(grid, x, y-1)) {
let dd=getValue(grid, x, y-1) / (countClickable(grid, x, y-1) + countFlag(grid, x, y-1));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x, y-1)-countFlag(grid, x, y-1)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
if (isNumber(grid, x+1, y-1)) {
let dd=getValue(grid, x+1, y-1) / (countClickable(grid, x+1, y-1) + countFlag(grid, x+1, y-1));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x+1, y-1)-countFlag(grid, x+1, y-1)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
if (isNumber(grid, x-1, y)) {
let dd=getValue(grid, x-1, y) / (countClickable(grid, x-1, y) + countFlag(grid, x-1, y));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x-1, y)-countFlag(grid, x-1, y)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
if (isNumber(grid, x+1, y)) {
let dd=getValue(grid, x+1, y) / (countClickable(grid, x+1, y) + countFlag(grid, x+1, y));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x+1, y)-countFlag(grid, x+1, y)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
if (isNumber(grid, x-1, y+1)) {
let dd=getValue(grid, x-1, y+1) / (countClickable(grid, x-1, y+1) + countFlag(grid, x-1, y+1));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x-1, y+1)-countFlag(grid, x-1, y+1)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
if (isNumber(grid, x, y+1)) {
let dd=getValue(grid, x, y+1) / (countClickable(grid, x, y+1) + countFlag(grid, x, y+1));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x, y+1)-countFlag(grid, x, y+1)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
if (isNumber(grid, x+1, y+1)) {
let dd=getValue(grid, x+1, y+1) / (countClickable(grid, x+1, y+1) + countFlag(grid, x+1, y+1));
probaMine = Math.max(probaMine, dd);
dd = (getValue(grid, x+1, y+1)-countFlag(grid, x+1, y+1)) == 0 ? 1 : 0;
probaVide = Math.max(probaVide, dd);
}
//if (proba == 1) proba = 99;
}
return { "probaMine": probaMine, "probaVide": probaVide };
}
function getProbaGrid(grid) {
let cases = [];
for (let y=0; y<16; y++) {
cases[y] = Array(16).fill(UNKNOWN);
for (let x=0; x<16; x++) {
let pro = getProba(grid, x, y);
cases[y][x] = { "probaMine": Math.round(pro.probaMine*100)/100 , "probaVide": Math.round(pro.probaVide*100)/100 };
}
}
return cases;
}
function findBestMine(grid, probaGrid) {
let cases = [];
for (let y=0; y<16; y++) {
for (let x=0; x<16; x++) {
cases.push( { "x": x, "y": y, "value": grid[y][x], "probaMine": probaGrid[y][x].probaMine, "probaVide": probaGrid[y][x].probaVide} );
}
}
cases.sort(function(a, b){return b.probaMine - a.probaMine});
return cases;
}
function findBestVide(grid, probaGrid) {
let cases = [];
for (let y=0; y<16; y++) {
for (let x=0; x<16; x++) {
cases.push( { "x": x, "y": y, "value": grid[y][x], "probaMine": probaGrid[y][x].probaMine, "probaVide": probaGrid[y][x].probaVide} );
}
}
cases.sort(function(a, b){return b.probaVide - a.probaVide});
return cases;
}
function getColor(img, x, y) {
var color = img.colorAt(x+8, y+8);
if (color == "c0c0c0") {
color = img.colorAt(x+9, y+9);
}
if (color == "c0c0c0") {
color = img.colorAt(x+5, y+4);
}
return color;
}
function getKind(img, x, y) {
var hex = getColor(img, x, y);
if (hex == "c0c0c0") {
hex = img.colorAt(x+1, y+3);
if (hex == "c0c0c0") {
return CLICKED; //non clickable
}
return 0; //clickable
} else if (hex == "0000ff") {
return 1;
} else if (hex == "008000") {
return 2;
} else if (hex == "ff0000") {
hex = img.colorAt(x+8+1, y+8+3);
if (hex == "000000") {
return FLAG; //flag
}
return 3;
} else if (hex == "000080") {
return 4;
} else if (hex == "800000") {
return 5;
} else if (hex == "008080") {
return 6;
} else if (hex == "000000") {
hex = img.colorAt(x+2, y+2);
if (hex == "ff0000") {
return FAILMINE; //noir et rouge
}
hex = img.colorAt(x+7, y+7);
if (hex == "ffffff") {
return MINE; //noir et blanc
}
return 7;
} else if (hex == "808080") {
return 8;
}
return UNKNOWN;
}
//
function perdu(grid) {
for (let y=0; y<16; y++) {
for (let x=0; x<16; x++) {
if (grid[y][x]==10) {
return true;
}
}
}
return false;
}
let grid = getGrid();
console.log(grid);
//robot.moveMouse(22, 100);
//robot.mouseClick(); //focus
let previousClick = {x: -1, y: -1};
let yyy = 0;
for (let ttt = 0; ttt<16*16; ttt++) {
let grid = getGrid();
if (grid.length==0) {
break;
}
if (perdu(grid)) {
break;
}
console.log();
console.log(grid);
//
let probaGrid = getProbaGrid(grid)
console.log();
console.log(" ");
let where = findBestVide(grid, getProbaGrid(grid));
//console.log(where);
let val = where[0];
if (val.probaVide==1) {
robot.moveMouseSmooth(winpos.TopLeft.X+22+val.x*16, winpos.TopLeft.Y+110+val.y*16);
robot.mouseClick();
} else {
where = findBestMine(grid, getProbaGrid(grid));
val = where[0];
robot.moveMouseSmooth(winpos.TopLeft.X+22+val.x*16, winpos.TopLeft.Y+110+val.y*16);
robot.mouseClick("right");
if (val.x == previousClick.x && val.y == previousClick.y) {
if (yyy == 5) {
console.log("same click 1");
console.log(previousClick);
console.log(val);
break;
} else {
yyy++;
}
}
}
previousClick = {x: val.x, y: val.y};
}
mouse = robot.getMousePos();
hex = robot.getPixelColor(mouse.x, mouse.y);
console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y);
});