diff --git a/main.js b/main.js new file mode 100644 index 0000000000000000000000000000000000000000..eb42045c2646094e47d0de537364222b5f22e258 --- /dev/null +++ b/main.js @@ -0,0 +1,80 @@ +import { solveSudoku } from "./solver.js"; +import http from 'node:http'; + + + +const sudokuBoard = [ + [5, 3, 0, 0, 7, 0, 0, 0, 0], + [6, 0, 0, 1, 9, 5, 0, 0, 0], + [0, 9, 8, 0, 0, 0, 0, 6, 0], + [8, 0, 0, 0, 6, 0, 0, 0, 3], + [4, 0, 0, 8, 0, 3, 0, 0, 1], + [7, 0, 0, 0, 2, 0, 0, 0, 6], + [0, 6, 0, 0, 0, 0, 2, 8, 0], + [0, 0, 0, 4, 1, 9, 0, 0, 5], + [0, 0, 0, 0, 8, 0, 0, 7, 9], +]; +const rawString = sudokuBoard.map(row => row.map(num => `<td>${num}</td>`).join('')).map(row => `<tr>${row}</tr>`).join(''); + + + +const sol = solveSudoku(sudokuBoard); +// Format the solved Sudoku board vertically +const solString = sol.map(row => row.map(num => `<td>${num}</td>`).join('')).map(row => `<tr>${row}</tr>`).join(''); + +const htmlResponse = ` + <html> + <head> + <style> + table { + border-collapse: collapse; + } + td { + border: 1px solid black; + width: 30px; /* Adjust the width as needed */ + height: 30px; /* Adjust the height as needed */ + text-align: center; + } + </style> + </head> + <body> + <table>${rawString}</table> + <p><p> + <button onclick="displaySolution()">Show Solution</button> + <p><p> + + <table id="solutionTable" style="display: none;">${solString}</table> + <script> + function displaySolution() { + const solutionTable = document.getElementById('solutionTable'); + if(solutionTable.style.display ==="none"){ + solutionTable.style.display = 'table'; // Show the solution table + } else { + solutionTable.style.display = "none" // unshow + } + } + </script> + + </body> + </html> +`; + +const port = 3000; + +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/html'); + res.end(htmlResponse); +}); + +server.listen(port, () => { + console.log(`Server running at http://localhost:${port}`); +}); + + + + + + + + diff --git a/package.json b/package.json index c4f831b606dd664f3d6490fd2973ea6c14ade01f..33cce32b8ba58ae050910dcbf7bfa1a75daa9185 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,11 @@ "description": "To make it easy for you to get started with GitLab, here's a list of recommended next steps.", "main": "solver.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "nodemon main.js" }, "keywords": [], "type": "module", "author": "", - "license": "ISC" + "license": "ISC" } diff --git a/solver.js b/solver.js index 232468dedab7b16b57cc2242e8408b3862fd1288..0652859729964740b2e7bdfc51f96116b08fc19a 100644 --- a/solver.js +++ b/solver.js @@ -45,22 +45,6 @@ function solveSudoku(board) { } } - // Example Sudoku board (0 represents empty cells) - const sudokuBoard = [ - [5, 3, 0, 0, 7, 0, 0, 0, 0], - [6, 0, 0, 1, 9, 5, 0, 0, 0], - [0, 9, 8, 0, 0, 0, 0, 6, 0], - [8, 0, 0, 0, 6, 0, 0, 0, 3], - [4, 0, 0, 8, 0, 3, 0, 0, 1], - [7, 0, 0, 0, 2, 0, 0, 0, 6], - [0, 6, 0, 0, 0, 0, 2, 8, 0], - [0, 0, 0, 4, 1, 9, 0, 0, 5], - [0, 0, 0, 0, 8, 0, 0, 7, 9], - ]; - - const solvedSudoku = solveSudoku(BoardArrayofArraysForLine); - console.log(solvedSudoku); - export {solveSudoku } \ No newline at end of file