2022-06-24 13:37:03 +00:00
|
|
|
import * as util from "../util.js";
|
2022-06-24 14:34:25 +00:00
|
|
|
const searchLoc = [
|
2022-06-24 13:37:03 +00:00
|
|
|
[-1, 1], [0, 1], [1, 1],
|
|
|
|
[-1, 0], [1, 0],
|
|
|
|
[-1, -1], [0, -1], [1, -1]
|
|
|
|
];
|
|
|
|
export function generate(avoidX, avoidY, world) {
|
|
|
|
var minesPlanted = 0;
|
|
|
|
while (minesPlanted < world.mines || !(minesPlanted <= world.width * world.height - 15)) {
|
|
|
|
const x = util.randomNumber(0, world.width);
|
|
|
|
const y = util.randomNumber(0, world.height);
|
|
|
|
var suitable = true;
|
|
|
|
searchLoc.forEach(loc => {
|
|
|
|
const tempx = x + loc[0];
|
|
|
|
const tempy = y + loc[1];
|
|
|
|
if (tempx === avoidX && tempy === avoidX) {
|
|
|
|
suitable = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (x == avoidX && y == avoidY) {
|
|
|
|
suitable = false;
|
|
|
|
}
|
|
|
|
if (world.data[x][y].type == 5) {
|
|
|
|
suitable = false;
|
|
|
|
}
|
|
|
|
if (suitable) {
|
|
|
|
world.data[x][y].type = 5;
|
|
|
|
minesPlanted++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
world.isGenerated = true;
|
|
|
|
return world;
|
|
|
|
}
|
2022-06-24 16:29:05 +00:00
|
|
|
//# sourceMappingURL=generator.js.map
|