STANAANDREY

random sudoku board

Jul 30th, 2021 (edited)
787
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const getRandomSudokuBoard = () => {
  2.     //#region other functions
  3.     const randShuffleArr = arr => arr.sort(() => Math.random() - .5);
  4.     const cyclicPermuteArr = arr => {
  5.         const last = arr.pop();
  6.         arr.splice(0, 0, last);
  7.     };
  8.     //#endregion
  9.     let randPattern = [];
  10.     for (let i = 1; i <= 9; i++) {
  11.         randPattern.push(i);
  12.     }
  13.     randShuffleArr(randPattern);
  14.     let board = new Array(9).fill().map(() => new Array(9).fill(0));
  15.     for (let j = 0; j < 3; j++) {
  16.         for (let i = 0; i < 3; i++) {
  17.             const startI = i * 3, endI = startI + 3;
  18.             const startJ = j * 3, endJ = startJ + 3;
  19.             let index = 0;//index in randPattern
  20.             for (let i2 = startI; i2 < endI; i2++) {
  21.                 for (let j2 = startJ; j2 < endJ; j2++) {
  22.                     board[i2][j2] = randPattern[index++];
  23.                 }
  24.             }
  25.             cyclicPermuteArr(randPattern);
  26.         }
  27.     }
  28.     return board;
  29. };
  30.  
  31. let r = getRandomSudokuBoard();
  32. console.log(r);
RAW Paste Data