MultiplayerMinesweeper/dist/world/revealer.js

36 lines
1.3 KiB
JavaScript
Raw Normal View History

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) {
var toSearch = [];
var searchedLocations = [];
toSearch.push([x, y]);
if (data[x][y].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;
}
}