MultiplayerMinesweeper/scripts/world/revealer.mjs

44 lines
1.1 KiB
JavaScript

const searchLoc =
[
[-1,1], [0,1], [1,1],
[-1,0], [1,0],
[-1,-1],[0,-1],[1,-1]
]
export function reveal(data, location) {
if (data[location[0]][location[1]].type !== 5) {
var toSearch = [];
var searchedLocations = [];
toSearch.push(location)
if (data[location[0]][location[1]].type === 1) {
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
}
}