-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
38 lines (31 loc) · 863 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Grid from "./src/grid";
export function generateSokobanLevel(parameters = {}) {
let {
width = 9,
height = 9,
boxes = 3,
minWalls = 13,
attempts = 5000,
seed = Date.now(),
initialPosition,
type = "string",
} = parameters;
let grid = new Grid(width, height, boxes, seed, minWalls, initialPosition);
while (--attempts > 0) {
if (!grid.applyTemplates()
|| !grid.isGoodCandidate()
|| !grid.redeployGoals()
|| !grid.generateFarthestBoxes()) {
continue;
}
if (type === "string") {
return grid.toReadableString();
}
if (type === "class") {
return grid;
}
console.warn(`sokoban-generator/generateSokobanLevel: Unrecognized value for key "string": ${type}. It should be either "string" or "class`);
return grid.toReadableString();
}
return null;
}