2022-06-22 01:51:09 +00:00
|
|
|
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]
|
|
|
|
]
|
|
|
|
|
2022-06-23 05:44:57 +00:00
|
|
|
export function generate(avoidX, avoidY, world){
|
2022-06-22 01:51:09 +00:00
|
|
|
var minesPlanted = 0;
|
2022-06-23 05:44:57 +00:00
|
|
|
while (minesPlanted < world.mines || !(minesPlanted <= world.width*world.height-15)) {
|
|
|
|
const x = util.randomNumber(0,world.width)
|
|
|
|
const y = util.randomNumber(0,world.height)
|
2022-06-22 01:51:09 +00:00
|
|
|
var suitable = true;
|
|
|
|
searchLoc.forEach(loc => {
|
|
|
|
const tempx = x + loc[0]
|
|
|
|
const tempy = y + loc[1]
|
2022-06-23 05:44:57 +00:00
|
|
|
if (tempx === avoidX && tempy === avoidX) {
|
2022-06-22 01:51:09 +00:00
|
|
|
suitable = false;
|
|
|
|
}
|
|
|
|
})
|
2022-06-23 05:44:57 +00:00
|
|
|
|
|
|
|
if (x == avoidX && y == avoidY) { suitable = false }
|
|
|
|
if (world.data[x][y].type == 5) { suitable = false }
|
2022-06-22 01:51:09 +00:00
|
|
|
|
|
|
|
if (suitable) {
|
|
|
|
world.data[x][y].type = 5
|
|
|
|
minesPlanted++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
world.isGenerated = true;
|
|
|
|
return world;
|
|
|
|
}
|