sudoku-solve
Version:
Konni's Sudoku solving library
73 lines (72 loc) • 2.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SudokuNumber = void 0;
const Changes_1 = require("./Changes");
const CommonFunctions_1 = require("./CommonFunctions");
const NumberArray_1 = require("./NumberArray");
const Position_1 = require("./Position");
const SolvedNumber_1 = require("./SolvedNumber");
const NUMBERS = [1, 2, 3, 4, 5, 6, 7, 8, 9];
class SudokuNumber {
constructor(row, col, value) {
this.row = row;
this.col = col;
this.index = CommonFunctions_1.index(row, col);
this.value = NumberArray_1.NumberArray.contains(NUMBERS, value) ? [value] : NUMBERS.slice();
}
isSolved() {
return this.value.length == 1;
}
isImpossible() {
return this.value.length < 1;
}
isPossible(value) {
return this.isSolved() && this.value[0] === value ||
!this.isSolved() && this.value.includes(value);
}
get() {
if (this.isSolved()) {
return this.value[0];
}
else {
throw new Error("get on a number that is not solved yet");
}
}
remove(numbers, rule) {
if (this.isSolved())
return Changes_1.Change.none();
const possibleNumbers = this.value.filter((n) => !NumberArray_1.NumberArray.contains(numbers, n));
if (possibleNumbers.length === 1) {
return Changes_1.Change.solvedNumber(this.solve(rule, possibleNumbers[0]));
}
if (possibleNumbers.length > 1) {
const change = Changes_1.Change.forRemovedNumbers(this, rule, this.value, possibleNumbers);
this.value = possibleNumbers;
return change;
}
return Changes_1.Change.none();
}
set(number) {
this.value = [number];
}
solve(rule, value) {
this.value = [value];
return new SolvedNumber_1.SolvedNumber(rule, this.row, this.col, value);
}
positionEquals(other) {
return this.row === other.row && this.col === other.col;
}
position() {
return Position_1.Position.fromRowCol(this.row, this.col);
}
toString() {
return `row: ${this.row}, col: ${this.col}, index: ${this.index}, value: ${this.value}`;
}
toSimpleString() {
return this.value.length === 1 ? `${this.value[0]}` : '?';
}
toDetailedString() {
return this.value.length === 1 ? `${this.value[0]}` : `[${this.value}]`;
}
}
exports.SudokuNumber = SudokuNumber;