🚀 refactor algorithm

This commit is contained in:
platane
2020-10-29 23:27:08 +01:00
parent 1c6814c2fa
commit d81ecec836
27 changed files with 274 additions and 777 deletions

View File

@@ -0,0 +1,19 @@
import { Color, createEmptyGrid, setColor } from "../grid";
export const createFromAscii = (ascii: string) => {
const a = ascii.split("\n");
if (a[0] === "") a.shift();
const height = a.length;
const width = Math.max(...a.map((r) => r.length));
const grid = createEmptyGrid(width, height);
for (let x = width; x--; )
for (let y = height; y--; ) {
const c = a[y][x];
const color =
(c === "#" && 3) || (c === "@" && 2) || (c === "." && 1) || +c;
if (c) setColor(grid, x, y, color as Color);
}
return grid;
};

View File

@@ -0,0 +1,11 @@
import ParkMiller from "park-miller";
import { Color, createEmptyGrid } from "../grid";
import { randomlyFillGrid } from "../randomlyFillGrid";
export const createFromSeed = (seed: number, width = 5, height = 5) => {
const grid = createEmptyGrid(width, height);
const pm = new ParkMiller(seed);
const random = pm.integerInRange.bind(pm);
randomlyFillGrid(grid, { colors: [1, 2] as Color[], emptyP: 2 }, random);
return grid;
};

View File

@@ -119,7 +119,7 @@ setColor(closedU, 2 + 10, 3 + 10, 1 as Color);
setColor(closedU, 1 + 10, 3 + 10, 1 as Color);
setColor(closedU, 2 + 10, 4 + 10, 1 as Color);
const create = (width: number, height: number, emptyP: number) => {
const createRandom = (width: number, height: number, emptyP: number) => {
const grid = createEmptyGrid(width, height);
const pm = new ParkMiller(10);
const random = pm.integerInRange.bind(pm);
@@ -128,10 +128,10 @@ const create = (width: number, height: number, emptyP: number) => {
};
// small realistic
export const small = create(10, 7, 3);
export const smallPacked = create(10, 7, 1);
export const smallFull = create(10, 7, 0);
export const small = createRandom(10, 7, 3);
export const smallPacked = createRandom(10, 7, 1);
export const smallFull = createRandom(10, 7, 0);
// small realistic
export const realistic = create(52, 7, 3);
export const realisticFull = create(52, 7, 0);
export const realistic = createRandom(52, 7, 3);
export const realisticFull = createRandom(52, 7, 0);