MultiplayerMinesweeper/scripts/world/revealer.mjs

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-06-22 01:51:09 +00:00
const searchLoc =
[
[-1,1], [0,1], [1,1],
[-1,0], [1,0],
[-1,-1],[0,-1],[1,-1]
]
export function reveal(x, y, data) {
if (data[x][y].type !== 5) {
2022-06-22 01:51:09 +00:00
var toSearch = [];
var searchedLocations = [];
toSearch.push([x, y])
2022-06-22 01:51:09 +00:00
if (data[x][y].type === 1) {
2022-06-22 01:51:09 +00:00
while (toSearch.length > 0) {
const x = toSearch[0][0]
const y = toSearch[0][1]
searchedLocations.push(toSearch[0])
toSearch.shift()
searchLoc.forEach(loc => {
const tempx = x + loc[0]
const tempy = y + loc[1]
if (tempx >= 0 && tempy >= 0 && tempx < data.length && tempy < data[0].length) {
if (data[tempx][tempy].type === 1 && data[tempx][tempy].mask === true) {
if (!toSearch.includes([tempx,tempy]) && !searchedLocations.includes([tempx,tempy])) {
toSearch.push([tempx,tempy])
}
}
if (data[tempx][tempy].type !== 5) {
data[tempx][tempy].mask = false;
}
}
})
}
}
return data
}
}