As a newcomer to Angular with a basic understanding of JavaScript, I am attempting to create a program that can solve Sudoku puzzles. In a 9x9 grid, there are a total of 81 points or squares. To efficiently check for violations of Sudoku rules - no repeating numbers within a row, column, or 3x3 sector - I thought of organizing the points into three identical lists of 81, but in three different ways: by column, row, and sector.
Therefore, I need an Array that consists of three Arrays, each containing nine Arrays of nine points.
Even though I know that Array<Array<Point>>
has a length of 9, and I have declared it as such, when I log it to the console, it appears to have a length of 9. My assumption was that there are nine null Array<Point>
within. I attempted to push points into any of those 9 Arrays, but I faced difficulties as I kept receiving an error stating that grid[0][x]
is undefined. Any suggestions or ideas would be greatly appreciated. Thank you.
gridByX:Array<Array<Point>> = new Array<Array<Point>>(9);
gridByY:Array<Array<Point>> = new Array<Array<Point>>(9);
gridByS:Array<Array<Point>> = new Array<Array<Point>>(9);
grid:Array<Array<Array<Point>>> = [this.gridByX, this.gridByY, this.gridByS];
constructor() {}
ngOnInit() {
this.populateGrid();
}
populateGrid(){
let counter = 0;
for (let x = 0; x < 9; x++){
for (let y = 0; y < 9; y++){
let point = new Point(counter, x, y);
this.grid[0][x].push(point);
this.grid[1][y].push(point);
this.grid[2][point.sector].push(point);
}
}
}