-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsweeper.js
130 lines (119 loc) · 2.45 KB
/
sweeper.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
//sweeper2.0
var sweeper = window.appController.minesweeper
var pId = sweeper.localPlayerId
var me = sweeper.getPlayer(pId)
var BLANK = 9,
BOMB = 0,
FLAG = 16,
BOMBHIT = 0,
HIDDEN = 10,
PROX1 = 1,
PROX2 = 2,
PROX3 = 3,
PROX4 = 4,
PROX5 = 5,
PROX6 = 6,
PROX7 = 7,
PROX8 = 8,
EDGE = 11
//returns the neigbors of the tile
getNeighbors = function(x,y)
{
return sweeper.playGrid.getNeighbors(x,y)
}
//returns the id of the tile
getTileId = function(x,y)
{
return sweeper.playGrid.get(x,y)
}
//returns if the tile is a flag or a hitbomb
isPosDangerous = function(x,y)
{
var id = getTileId(x,y)
return id == BOMBHIT || id > 11
}
//returns if the tile is a flag or a hitbomb
isDangerous = function(obj)
{
var id = getTileId(obj.x,obj.y)
//console.log("DANGER ID:",id)
//console.log(id == BOMBHIT || id > 11)
return id == BOMBHIT || id > 11
}
//returns true if the tile is a proxy
isProxy = function(x,y)
{
var id = getTileId(x,y)
return id > 0 && id < 9
}
//returns true if the tile is an outdated proxy
isOutdated = function(x,y)
{
if(isProxy(x,y))
{
var id = getTileId(x,y)
//console.log("(",x,y,")is:",id)
neighbors = getNeighbors(x,y)
nearbyDangers = neighbors.filter(isDangerous)
//console.log("neighbors_ filtered", nearbyDangers)
//console.log("length: ", nearbyDangers.length)
return id == nearbyDangers.length
}
else
{
//console.log("not proxy")
}
}
getHiddenNeighbors = function(x,y)
{
return getNeighbors(x,y).filter(function(obj){
return HIDDEN == getTileId(obj.x,obj.y)
})
}
snipe = function()
{
var w = sweeper.playGrid.width
var h = sweeper.playGrid.height
for(i = 0; i < w; i++)
{
for(j = 0; j < h; j++)
{
if(isProxy(i,j))
{
//console.log("proxy")
if(isOutdated(i,j))
{
gold = getHiddenNeighbors(i,j)
if(gold.length > 0)
{
oldscore = me.points
gold.forEach(function(obj)
{
sweeper.revealCell(obj.x,obj.y,sweeper.localPlayerId)
//for debugging
if(oldscore > me.points)
{
//console.log("lost score at: ", obj, getTileId(obj.x, obj.y))
}
else if(oldscore == me.points)
{
//console.log("no points gained:", obj, getTileId(obj.x, obj.y))
}
else
{
//console.log("", obj, getTileId(obj.x, obj.y))
}
//end of debugging
})
return true
}
}
}
}
}
return false
}
sweep = function(seconds=1)
{
window.setInterval(snipe, seconds*1000);
}