I am new to angular and my JavaScript is basic. I am trying to write a program the solves Sudoku puzzles. A 9x9 grid will have 81 points or squares. I figured that I could check for violations of Sudoku rules (no repeats of a number within a single row, column, or 3x3 by sector) faster if I organized the points into three identical lists of 81, but organized three different ways. Again, by column, row, and sector.
So: I would like an Array that contains three Arrays that contains nine Arrays that contains nine points.
I know that the Array<Array<Point>> has a length of 9. I declared it as such and when I double check with a console log, it seems it has a length of 9. So I was thinking that there is 9 nulls Array<Point> within. I figured I could just push points to any of those 9 Arrays, but I have no luck. I keep getting told that grid[0][x] is undefined. Any ideas? Thanks.
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);
}
}
}
new Array(n), it creates an array withnundefined elements. Then, you can assign elements using bracket notation (eg:array[x] = new Point()). Also, you can only call.pushon arrays. In this case, your syntax should look like:grid[0][x] = new Point(counter, x, y)