MultiplayerMinesweeper/scripts/world/generator.mjs

43 lines
1 KiB
JavaScript

import * as util from "../util.mjs"
const searchLoc =
// [
// [0, 2],
// [-1, 1], [0, 1], [1, 1],
// [-2,0], [-1, 0],/*Start*/[1, 0], [2,0],
// [-1,-1], [0,-1], [1,-1],
// [0,-2]
//
// ]
[
[-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;
}