Advertisement
Old_But_Gold

Untitled

Sep 3rd, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. const gridSize = 9;
  2.  
  3. document.addEventListener('DOMContentLoaded', () => {
  4. const solveButton = document.getElementById('solve-btn');
  5. solveButton.addEventListener('click', solveSudoku);
  6.  
  7. const clearButton = document.getElementById('clear-btn');
  8. clearButton.addEventListener('click', clearGrid);
  9.  
  10. const sudokuGrid = document.getElementById('sudoku-grid');
  11.  
  12. for (let row = 0; row < gridSize; row += 1) {
  13. const newRow = document.createElement('tr');
  14. for (let col = 0; col < gridSize; col += 1) {
  15. const cell = document.createElement('td');
  16. const input = document.createElement('input');
  17. input.type = 'number';
  18. input.className = 'cell';
  19. input.id = `cell-${row}-${col}`;
  20. cell.appendChild(input);
  21. newRow.appendChild(cell);
  22. }
  23. sudokuGrid?.appendChild(newRow);
  24. }
  25. });
  26.  
  27. async function solveSudoku() {
  28. const clearButton = document.getElementById('clear-btn') as HTMLButtonElement;
  29. clearButton.disabled = true;
  30. const sudokuArray : number[][] = [];
  31.  
  32. for (let row = 0; row < gridSize; row += 1) {
  33. sudokuArray[row] = [];
  34. for (let col = 0; col < gridSize; col += 1) {
  35. const cellId = `cell-${row}-${col}`;
  36. const cell = document.getElementById(cellId) as HTMLInputElement;
  37. const cellValue = cell.value;
  38. sudokuArray[row][col] = cellValue !== '' ? parseInt(cellValue) : 0;
  39.  
  40. if (sudokuArray[row][col] !== 0) {
  41. cell.classList.add('user-input');
  42. }
  43. }
  44. }
  45.  
  46. if (solveSudokuHelper(sudokuArray, 0, 0)) {
  47. alert('Solution exists for the given Sudoku.');
  48. } else {
  49. alert('No solution exists for the given Sudoku.');
  50. }
  51. clearButton.disabled = false;
  52. }
  53.  
  54. function solveSudokuHelper(board : number[][], row : number, col : number) : boolean {
  55. if (col === 9) {
  56. col = 0;
  57. row += 1;
  58. if (row === 9) {
  59. return true;
  60. }
  61. }
  62.  
  63. if (board[row][col] !== 0) {
  64. return solveSudokuHelper(board, row, col + 1);
  65. }
  66.  
  67. for (let c = 1; c <= 9; c += 1) {
  68. if (isValid(board, row, col, c)) {
  69. board[row][col] = c;
  70.  
  71. if (solveSudokuHelper(board, row, col + 1)) {
  72. return true;
  73. }
  74.  
  75. board[row][col] = 0;
  76. }
  77. }
  78.  
  79. return false;
  80.  
  81. function isValid(board : number[][], row : number, col : number, c : number) : boolean {
  82.  
  83. for (let i = 0; i < 9; i += 1) {
  84. if (board[row][i] === c) {
  85. return false;
  86. }
  87. if (board[i][col] === c) {
  88. return false;
  89. }
  90. }
  91.  
  92. let startRow = Math.floor(row / 3) * 3;
  93. let startCol = Math.floor(col / 3) * 3;
  94. for (let i = startRow; i < startRow + 3; i += 1) {
  95. for (let j = startCol; j < startCol + 3; j += 1) {
  96. if (board[i][j] === c) {
  97. return false;
  98. }
  99. }
  100. }
  101.  
  102. return true;
  103. }
  104. }
  105.  
  106. function clearGrid() {
  107. for (let row = 0; row < gridSize; row += 1) {
  108. for (let col = 0; col < gridSize; col += 1) {
  109. const cellId = `cell-${row}-${col}`;
  110. const cell = document.getElementById(cellId) as HTMLInputElement;
  111. cell.value = '';
  112. cell.classList.remove('user-input', 'solved');
  113. }
  114. }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement