MultiplayerMinesweeper/dist/world/generator.js

34 lines
1 KiB
JavaScript

import * as util from "../util.js";
const searchLoc = [
[-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;
}
//# sourceMappingURL=generator.js.map