better-sudoku
Version:
Procedural Sudoku Generators
72 lines (71 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Board = void 0;
const small_square_1 = require("../SmallSquare/small_square");
const constants_1 = require("../constants");
class Board {
constructor() {
this.rects = new Map();
this.initBoard();
}
initBoard() {
for (let col = 0; col < constants_1.COLUMNS_SIZE; col++) {
// First loop id for columns
for (let row = 0; row < constants_1.ROWS_SIZE; row++) {
const coords = { row, col };
this.rects.set(this.createCoordsKey(coords), new small_square_1.Square(coords.row, coords.col));
}
}
}
createCoordsKey({ col, row }) {
return `${row}:${col}`;
}
parseCoordKeyToCoords(key) {
const arr = key.split(':');
if (arr.length !== 2) {
throw new Error('Key for coords have to be composed of a number:number string example: 1:2');
}
const maped = arr.map((s) => parseInt(s, 10));
return { row: maped[0], col: maped[1] };
}
findCoordinatesForColumn(col) {
const result = [];
for (let row = 0; row < constants_1.ROWS_SIZE; row++) {
result.push(this.createCoordsKey({ col, row }));
}
return result;
}
findCoordinatesForRow(row) {
const result = [];
for (let col = 0; col < constants_1.COLUMNS_SIZE; col++) {
result.push(this.createCoordsKey({ col, row }));
}
return result;
}
findCoordinatesForBigSquare({ col, row }) {
const colMin = col * 3;
const colMax = colMin + 2;
const rowMin = row * 3;
const rowMax = rowMin + 2;
const result = [];
for (let r = rowMin; r < rowMax; r++) {
for (let c = colMin; c < colMax; c++) {
result.push(this.createCoordsKey({ col: c, row: r }));
}
}
return result;
}
mapCoordsKeysToSquares(keys) {
return keys.map((key) => this.rects.get(key));
}
getColumnSquares(column) {
return this.mapCoordsKeysToSquares(this.findCoordinatesForColumn(column));
}
getRowSquares(row) {
return this.mapCoordsKeysToSquares(this.findCoordinatesForRow(row));
}
getBigSquare(coords) {
return this.mapCoordsKeysToSquares(this.findCoordinatesForBigSquare(coords));
}
}
exports.Board = Board;