0

const N = 4;
const fourbyfour = [
  [1, 0, 0, 4],
  [3, 4, 1, 2],
  [2, 1, 4, 3],
  [4, 3, 2, 1],
];

function solve(board) {
  for (let i = 0; i < N; i++) {
    for (let j = 0; j < N; j++) {
      if (board[i][j] === 0) {
        for (let k = 1; k < N + 1; k++) {
          board[i][j] = k;
          if (isValidBoard(board)) {
            if (isSolved(board)) return board;
            else solve(board);
          }
        }
      }
    }
  }
  return;
}

function isSolved(board) {
  let cells = [];
  for (let row of board) {
    cells.push(...row);
  }
  return !cells.includes(0);
}

function isValidBoard(board) {
  // Check rows are valid
  for (let i = 0; i < N; i++) {
    const row = board[i].filter((cell) => cell !== 0);
    if (new Set(row).size !== row.length) {
      return false;
    }
  }

  // Check columns are valid
  for (let i = 0; i < N; i++) {
    let column = [];
    for (let j = 0; j < N; j++) {
      if (board[j][i] !== 0) column.push(board[j][i]);
    }
    if (new Set(column).size !== column.length) {
      return false;
    }
  }

  const root = Math.sqrt(N);
  // Check each grid
  for (let i = 0; i < N; i += root) {
    for (let j = 0; j < N; j += root) {
      const square = [];
      for (let k = i; k < i + root; k++) {
        for (let l = j; l < j + root; l++) {
          if (board[k][l] !== 0) {
            square.push(board[k][l]);
          }
        }
      }
      if (new Set(square).size !== square.length) {
        return false;
      }
    }
  }
  return true;
}
console.table(solve(fourbyfour));

solve() keeps on returning undefined. I'm fairly certain the issue is in the solve function, and not related to isSolved() and isValidBoard(). The solve function is getting the correct board, if I console.log the board instead of returning it, the correct board gets printed, but for some reason it isn't getting returned.

1 Answers1

2

You are never returning the value of your recursion. You need to return from the second time you enter solve.

Also, console.table does not seams to be working in the snippet

const N = 4;
const fourbyfour = [
  [1, 0, 0, 4],
  [3, 4, 1, 2],
  [2, 1, 4, 3],
  [4, 3, 2, 1],
];

function solve(board) {
  for (let i = 0; i < N; i++) {
    for (let j = 0; j < N; j++) {
      if (board[i][j] === 0) {
        for (let k = 1; k < N + 1; k++) {
          board[i][j] = k;
          if (isValidBoard(board)) {
            if (isSolved(board)) {
              return board;
            } else {
              return solve(board);
            }
          }
        }
      }
    }
  }
  return 'test';
}

function isSolved(board) {
  let cells = [];
  for (let row of board) {
    cells.push(...row);
  }
  return !cells.includes(0);
}

function isValidBoard(board) {
  // Check rows are valid
  for (let i = 0; i < N; i++) {
    const row = board[i].filter((cell) => cell !== 0);
    if (new Set(row).size !== row.length) {
      return false;
    }
  }

  // Check columns are valid
  for (let i = 0; i < N; i++) {
    let column = [];
    for (let j = 0; j < N; j++) {
      if (board[j][i] !== 0) column.push(board[j][i]);
    }
    if (new Set(column).size !== column.length) {
      return false;
    }
  }

  const root = Math.sqrt(N);
  // Check each grid
  for (let i = 0; i < N; i += root) {
    for (let j = 0; j < N; j += root) {
      const square = [];
      for (let k = i; k < i + root; k++) {
        for (let l = j; l < j + root; l++) {
          if (board[k][l] !== 0) {
            square.push(board[k][l]);
          }
        }
      }
      if (new Set(square).size !== square.length) {
        return false;
      }
    }
  }
  return true;
}
console.log(solve(fourbyfour));
Nicolas
  • 8,077
  • 4
  • 21
  • 51