我在学校有一个制作数独游戏的项目,当尝试从数组中应用随机数独出现在html中时遇到问题 当我使用一个非随机数独时,它工作正常,但是当我使用randomSudoku作为矩阵时,它全部弄乱了,并在每个输入中给了我整个数组
//this is the 9x9 matrix and there are 3 more
let sudoku1 = [
[8, 2, 7, 1, 5, 4, 3, 9, 6],
[9, 6, 5, 3, 2, 7, 1, 4, 8],
[3, 4, 1, 6, 8, 9, 7, 5, 2],
[5, 9, 3, 4, 6, 8, 2, 7, 1],
[4, 7, 2, 5, 1, 3, 6, 8, 9],
[6, 1, 8, 9, 7, 2, 4, 3, 5],
[7, 8, 6, 2, 3, 5, 9, 1, 4],
[1, 5, 4, 7, 9, 6, 8, 2, 3],
[2, 3, 9, 8, 4, 1, 5, 6, 7]];
//these are the array of sudokus(each sudoku is a 9x9 matrix)
let allSudokus = [[sudoku1], [sudoku2], [sudoku3], [sudoku4]]
let randomSudoku = allSudokus[Math.floor(Math.random() * allSudokus.length)]
//this is the function i did in order to fill "solve" the board
const solveBoard = () => {
for (let r = 0; r < 9; r++) {
for (let c = 0; c < 9; c++) {
let cellid = document.querySelector("#r" + r + "c" + c); //
cellid.innerHTML = randomSudoku[r][c]
}
}
}
答案 0 :(得分:0)
您正在使用allsudokus
(不存在)而不是allSudokus
。这样做:
let randomSudoku = allSudokus[Math.floor(Math.random() * allSudokus.length)]
,然后在这里相同:
cellid.innerHTML = randomSudoku[r][c]
编辑:确定,您现在已解决该问题