From dec37fffee6cee68a20c6bb6250feafb4737558b Mon Sep 17 00:00:00 2001 From: saurabh gupta Date: Wed, 31 Jul 2024 01:37:24 +0530 Subject: [PATCH] presenting Sudoku Solver --- ALGO/Graph/sudokoSolver/index.html | 41 +++++ ALGO/Graph/sudokoSolver/script.js | 232 +++++++++++++++++++++++++++++ ALGO/Graph/sudokoSolver/style.css | 66 ++++++++ Assets/Testing/test.js | 35 +++++ 4 files changed, 374 insertions(+) create mode 100644 ALGO/Graph/sudokoSolver/index.html create mode 100644 ALGO/Graph/sudokoSolver/script.js create mode 100644 ALGO/Graph/sudokoSolver/style.css create mode 100644 Assets/Testing/test.js diff --git a/ALGO/Graph/sudokoSolver/index.html b/ALGO/Graph/sudokoSolver/index.html new file mode 100644 index 0000000..ac04af3 --- /dev/null +++ b/ALGO/Graph/sudokoSolver/index.html @@ -0,0 +1,41 @@ + + + + + + Document + + + + + + + + +
+ +
+
+
+ +
+ + + +
+ +
Speed
+
+ +
+
+ + + + + + \ No newline at end of file diff --git a/ALGO/Graph/sudokoSolver/script.js b/ALGO/Graph/sudokoSolver/script.js new file mode 100644 index 0000000..40d732a --- /dev/null +++ b/ALGO/Graph/sudokoSolver/script.js @@ -0,0 +1,232 @@ + +const table = document.getElementById("suduko"); +let tables = Array.from({ length: 9 }, () => Array(9).fill(0)); + + + +function getRandomValue(range) +{ + return Math.floor(range * Math.random()); +} + +function createTable() +{ + table.style.borderSpacing ="0px" + for(let i=0;i<9;i++) + { + let row = document.createElement('tr'); + for(let j=0;j<9;j++) + { + //creating new cell & input element + let cell = document.createElement('td'); + let input = document.createElement('input'); + + //indexing cell & input element + let uniqueId = `${i}_${j}`; + cell.setAttribute('id',uniqueId); + + //styling cell + cell.style.border = "4px solid black"; + cell.style.padding = "0px"; + cell.style.fontSize="50px"; + + //style input + input.style.width ="100px"; + input.style.height ="100px"; + input.style.fontSize ="50px"; + input.style.textAlign ="center"; + //input.placeholder=i; + input.style.backgroundColor="transparent"; + input.style.border = "none"; + + //appending input textBox to each cell + cell.appendChild(input); + //appending cell to row tr + row.appendChild(cell); + } + //appending row to table + table.appendChild(row); + } +} + +function updateTable() +{ + for(let i=0;i<9;i++) + { + for(let j=0;j<9;j++) + { + let data = document.getElementById(`${i}_${j}`).getElementsByTagName('input')[0].value; + if(data==="")continue; + tables[i][j] = data; + console.log(data); + } + } +} + +function checkValidity(i,j,n) +{ + let x=i,y=j; + i=Math.floor(i/3) * 3,j=Math.floor(j/3) * 3; + //console.log(i + " " + j); + for(let row=i;row { + function tryNext(k) { + if (k > 9) { + tables[i][j] = 0; + cellInput.value = ""; + cellInput.style.backgroundColor = "red";//caf + resolve(false); + return; + } + if (checkValidity(i, j, k)) { + tables[i][j] = k; + cellInput.value = k; + cellInput.style.backgroundColor = "green"; + setTimeout(() => { + nextCell(i, j).then(result => { + if (result) { + resolve(true); + } else { + tables[i][j] = 0; + cellInput.value = ""; + cellInput.style.backgroundColor = "red";//caf + setTimeout(() => tryNext(k + 1), 0.001); + } + }); + }, 0.001); + } else { + setTimeout(() => tryNext(k + 1), 0.001); + } + } + + tryNext(1); + }); + } + + function nextCell(i, j) { + if (j < 8) return animateSolve(i, j + 1); + return animateSolve(i + 1, 0); + } + + return animateSolve(0, 0); +} + +function driverFun() +{ + createTable(); + + let randomFillBut = document.getElementById('randomFill'); + let clearTableBut = document.getElementById('clearBox'); + let solveBut = document.getElementById('solveBut'); + + randomFillBut.addEventListener('click', randomFillTable); + clearTableBut.addEventListener('click',clearTable); + solveBut.addEventListener('click',() => { + updateTable(); + if(solveSudoku()) + { + console.log("solved"); + } + else + { + console.log("not possible"); + } + }); +} +driverFun(); \ No newline at end of file diff --git a/ALGO/Graph/sudokoSolver/style.css b/ALGO/Graph/sudokoSolver/style.css new file mode 100644 index 0000000..5d522b1 --- /dev/null +++ b/ALGO/Graph/sudokoSolver/style.css @@ -0,0 +1,66 @@ +* +{ + margin:0 auto; + box-sizing: border-box; +} + +body +{ + background-color: beige; +} + +#header +{ + display: flex; + font-size: 2em; + justify-content: center; +} +#mainFrame +{ + display: flex; + flex-direction: column; + justify-content: space-around; + align-items: center; + height: 90vh; +} + +#board +{ + height: 1000px; + width: 1000px; +} + +#suduko +{ + height: 1000px; + width: 1000px; +} + +#operator +{ + display: flex; + width: 100vw; + justify-content: space-around; +} +.but +{ + height:100px; + width:150px; + font-size: 1.3em; + font-weight: bolder; +} + + +.footer +{ + display: flex; + justify-content: space-around; + font-size: 30px; +} +#footer +{ + position: fixed; + bottom: 0; + width: 100%; + text-align: center; /* Ensure content is centered */ +} \ No newline at end of file diff --git a/Assets/Testing/test.js b/Assets/Testing/test.js new file mode 100644 index 0000000..f5fe988 --- /dev/null +++ b/Assets/Testing/test.js @@ -0,0 +1,35 @@ +// function fetchData(callback) { +// setTimeout(() => { +// console.log("Data fetched"); +// callback(); +// }, 2000); +// } +// console.log("Dloading "); +// function processData() { +// console.log("Processing data"); +// } + +// fetchData(processData); + +//await asynchronous +function fetchData() { + return new Promise((resolve) => { + setTimeout(() => { + resolve("Data fetched"); + }, 2000); + }); + } + + async function processData() { + try { + const data = await fetchData(); + console.log(data); + console.log("Processing data"); + } catch (error) { + console.log(error); + } + } + + processData(); + + \ No newline at end of file