Use 0 instead of null to represent no value
This commit is contained in:
parent
09f740f86a
commit
c16a68e796
@ -3,7 +3,7 @@ import { StaticPool, isTimeoutError } from "node-worker-threads-pool";
|
|||||||
import WORKERS from "physical-cpu-count";
|
import WORKERS from "physical-cpu-count";
|
||||||
const TIMEOUT = 20000;
|
const TIMEOUT = 20000;
|
||||||
|
|
||||||
export type Cell = { value: number | null };
|
export type Cell = { value: number };
|
||||||
|
|
||||||
export type GenerateArguments = {
|
export type GenerateArguments = {
|
||||||
regionWidth: number;
|
regionWidth: number;
|
||||||
|
@ -20,14 +20,20 @@ type NodeMeta = {
|
|||||||
type BoardInfo = [number, number, number, number, number];
|
type BoardInfo = [number, number, number, number, number];
|
||||||
|
|
||||||
export class SudokuMath {
|
export class SudokuMath {
|
||||||
|
/** The width of each region */
|
||||||
regionWidth: number;
|
regionWidth: number;
|
||||||
|
/** The height of each region */
|
||||||
regionHeight: number;
|
regionHeight: number;
|
||||||
|
|
||||||
|
/** The number of unqiue values in this sudoku */
|
||||||
values: number;
|
values: number;
|
||||||
|
/** The squared number of unique values - also the total number of cells */
|
||||||
values2: number;
|
values2: number;
|
||||||
|
|
||||||
|
/** An array of board indexes (a range from 0 to `values2 - 1`) */
|
||||||
indexes: number[];
|
indexes: number[];
|
||||||
|
|
||||||
|
/** A cache of the possible cadidate rows of this sudoku. */
|
||||||
candidates: BoardInfo[];
|
candidates: BoardInfo[];
|
||||||
|
|
||||||
constructor(regionWidth: number, regionHeight: number) {
|
constructor(regionWidth: number, regionHeight: number) {
|
||||||
@ -130,8 +136,7 @@ export class SudokuMath {
|
|||||||
_baseBoard(): Cell[][] {
|
_baseBoard(): Cell[][] {
|
||||||
// return a sudoku board with a random set of values in the first row
|
// return a sudoku board with a random set of values in the first row
|
||||||
// used in generateComplete for small speedup
|
// used in generateComplete for small speedup
|
||||||
const firstRow = range(1, this.values + 1);
|
const firstRow = shuffle(range(1, this.values + 1));
|
||||||
shuffle(firstRow);
|
|
||||||
return [
|
return [
|
||||||
firstRow.map((value) => ({ value })),
|
firstRow.map((value) => ({ value })),
|
||||||
...Array(this.values - 1)
|
...Array(this.values - 1)
|
||||||
@ -139,7 +144,7 @@ export class SudokuMath {
|
|||||||
.map(() =>
|
.map(() =>
|
||||||
Array(this.values)
|
Array(this.values)
|
||||||
.fill(0)
|
.fill(0)
|
||||||
.map((val) => ({ value: val > 0 ? val : null }))
|
.map((value) => ({ value }))
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -179,8 +184,8 @@ export class SudokuMath {
|
|||||||
candidates[meta.row][meta.col][meta.value - 1] = node;
|
candidates[meta.row][meta.col][meta.value - 1] = node;
|
||||||
});
|
});
|
||||||
|
|
||||||
// board positions which have been removed, in the order they've been removed
|
// board positions which have been removed
|
||||||
const removed: Set<number> = new Set();
|
const removed = new Set<number>();
|
||||||
const masked: DNode[] = [];
|
const masked: DNode[] = [];
|
||||||
|
|
||||||
const hasOneSolution = () => {
|
const hasOneSolution = () => {
|
||||||
@ -199,12 +204,8 @@ export class SudokuMath {
|
|||||||
const col = n % this.values;
|
const col = n % this.values;
|
||||||
const existValue = completed[row][col].value;
|
const existValue = completed[row][col].value;
|
||||||
const nodes = candidates[row][col];
|
const nodes = candidates[row][col];
|
||||||
// console.log(row, col);
|
|
||||||
// console.log(existValue);
|
|
||||||
nodes.forEach((node) => {
|
nodes.forEach((node) => {
|
||||||
if (node.meta.value !== existValue) {
|
if (node.meta.value !== existValue) {
|
||||||
// console.log(node.meta);
|
|
||||||
// console.log("masking node");
|
|
||||||
masked.push(node);
|
masked.push(node);
|
||||||
maskRow(node);
|
maskRow(node);
|
||||||
}
|
}
|
||||||
@ -256,9 +257,7 @@ export class SudokuMath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
removed.forEach((index) => {
|
removed.forEach((index) => {
|
||||||
completed[Math.floor(index / this.values)][
|
completed[Math.floor(index / this.values)][index % this.values].value = 0;
|
||||||
index % this.values
|
|
||||||
].value = null;
|
|
||||||
});
|
});
|
||||||
return completed;
|
return completed;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user