-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
70 lines (65 loc) · 1.67 KB
/
bot.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
//Bot system for it, not done yet
import { iterateNeighbors, log, open, putFlag } from "./script.js";
const flag = putFlag;
/**
* @param {{r:Boolean,f:Boolean,c:Number,f:Boolean,p:String,h:Boolean}[][]} map Data of the field, without unrevealed tiles.
*/
function bot(map) {
try {
map.forEach((column, x) =>
column.forEach((tile, y) => {
if (tile.h) return;
if (tile.c === 0) return;
let count = 0,
remaining = 0;
iterateNeighbors(
x,
y,
(x, y) =>
((count += map[x][y].f) || 1) &&
(remaining += !map[x][y].f && !map[x][y].r)
);
const score = tile.c - count;
//console.log(x, y, count, score, remaining);
map[x][y].fc = count;
if (score === 0) {
iterateNeighbors(x, y, (x, y) => open(x, y));
} else {
if (score === remaining) {
iterateNeighbors(x, y, (x, y) => map[x][y].f || flag(x, y));
}
}
})
);
} catch (e) {
log(e.message);
}
//console.log(map);
}
/**
* @param {{r:Boolean,f:Boolean,c:Number,f:Boolean,p:String,h:Boolean}[][]} map Data of the field, without unrevealed tiles.
*/
function guess(map) {
map.every((column, x) =>
column.every((tile, y) => {
if (tile.h) return true;
if (tile.c === 0) return true;
let remaining = 0;
iterateNeighbors(
x,
y,
(x, y) => (remaining += !map[x][y].f && !map[x][y].r)
);
if (remaining > 0 && Math.random() > 0.8) {
log(`guess ${x}, ${y}, ${remaining}`);
var found = false;
iterateNeighbors(x, y, (x, y) => {
found || (found = flag(x, y));
});
return false;
}
return true;
})
);
}
export { bot, guess };