🚀 benchmark
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import { Grid, Color } from "./grid";
|
||||
import { Grid, Color, setColor, createEmptyGrid } from "./grid";
|
||||
|
||||
const rand = (a: number, b: number) => Math.floor(Math.random() * (b - a)) + a;
|
||||
|
||||
export const generateEmptyGrid = (width: number, height: number) =>
|
||||
generateRandomGrid(width, height, { colors: [], emptyP: 1 });
|
||||
const defaultRand = (a: number, b: number) =>
|
||||
Math.floor(Math.random() * (b - a)) + a;
|
||||
|
||||
export const generateRandomGrid = (
|
||||
width: number,
|
||||
@@ -11,17 +9,17 @@ export const generateRandomGrid = (
|
||||
options: { colors: Color[]; emptyP: number } = {
|
||||
colors: [1, 2, 3],
|
||||
emptyP: 2,
|
||||
}
|
||||
},
|
||||
rand = defaultRand
|
||||
): Grid => {
|
||||
const g = {
|
||||
width,
|
||||
height,
|
||||
data: Array.from({ length: width * height }, () => {
|
||||
const x = rand(-options.emptyP, options.colors.length);
|
||||
const grid = createEmptyGrid(width, height);
|
||||
|
||||
return x < 0 ? null : options.colors[x];
|
||||
}),
|
||||
};
|
||||
for (let x = width; x--; )
|
||||
for (let y = height; y--; ) {
|
||||
const k = rand(-options.emptyP, options.colors.length);
|
||||
|
||||
return g;
|
||||
if (k >= 0) setColor(grid, x, y, options.colors[k]);
|
||||
}
|
||||
|
||||
return grid;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user