0

I have an array, which is generated randomly every time, and I want to create a matrix using it. The matrix is for a sudoku game so it should be generated by a pattern. The first line is always the array that I am using. The pattern (from here: https://gamedev.stackexchange.com/a/138228) goes like this:

line 0: 8 9 3  2 7 6  4 5 1
line 1: 2 7 6  4 5 1  8 9 3 (shift 3)
line 2: 4 5 1  8 9 3  2 7 6 (shift 3)

line 3: 5 1 8  9 3 2  7 6 4 (shift 1)
line 4: 9 3 2  7 6 4  5 1 8 (shift 3)
line 5: 7 6 4  5 1 8  9 3 2 (shift 3)

line 6: 6 4 5  1 8 9  3 2 7 (shift 1)
line 7: 1 8 9  3 2 7  6 4 5 (shift 3)
line 8: 3 2 7  6 4 5  1 8 9 (shift 3)

How do I assign the array to the first line and then for the rest of the lines the shifted line from before? It doesn't work how I tried...

function generateArray() {
  var min = 1;
  var max = 9;
  var stop = 9;
  var numbers = [];
  for (let i = 0; i < stop; i++) {
    var n = Math.floor(Math.random() * max) + min;
    var check = numbers.includes(n);
    if (check === false) {
      numbers.push(n);
    } else {
      while (check === true) {
        n = Math.floor(Math.random() * max) + min;
        check = numbers.includes(n);
        if (check === false) {
          numbers.push(n);
        }
      }
    }
  }
  return numbers;
}

function shiftArray(array, nr) {
  array = array.concat(array.splice(0, nr)); // shift left by nr
  return array;
}

function generateMatrix() {
  var v = generateArray();
  var m = [];
  m[0] = v;
  m[1] = shiftArray(m[0], 3);
  m[2] = shiftArray(m[1], 3);
  m[3] = shiftArray(m[2], 1);
  m[4] = shiftArray(m[3], 3);
  m[5] = shiftArray(m[4], 3);
  m[6] = shiftArray(m[5], 1);
  m[7] = shiftArray(m[6], 3);
  m[8] = shiftArray(m[7], 3);
  console.log("matrix: ", m);
  return m;
}
generateMatrix();
Ale.X.L
  • 31
  • 6

0 Answers0