diff --git a/generateSudoku.js b/generateSudoku.js deleted file mode 100644 index d308f16930e1d4c6c978730a6c11b0eb2c2d6b4c..0000000000000000000000000000000000000000 --- a/generateSudoku.js +++ /dev/null @@ -1,69 +0,0 @@ -function generateSudoku(numberOfZ) { - const grid = Array.from({ length: 9 }, () => Array(9).fill(0)); - - function isValid(num, row, col) { - for (let i = 0; i < 9; i++) { - if ( - grid[row][i] === num || - grid[i][col] === num || - grid[row - (row % 3) + Math.floor(i / 3)][col - (col % 3) + (i % 3)] === num - ) { - return false; - } - } - return true; - } - - function solve() { - const numbers = Array.from({ length: 9 }, (_, i) => i + 1); - shuffle(numbers); - - for (let row = 0; row < 9; row++) { - for (let col = 0; col < 9; col++) { - if (grid[row][col] === 0) { - for (const num of numbers) { - if (isValid(num, row, col)) { - grid[row][col] = num; - - if (solve()) { - return true; - } - - grid[row][col] = 0; - } - } - return false; - } - } - } - return true; - } - - function shuffle(array) { - for (let i = array.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [array[i], array[j]] = [array[j], array[i]]; - } - } - - // Generate a complete Sudoku puzzle - solve(); - - // Create a copy of the grid before removing numbers - const puzzle = grid.map(row => row.slice()); - - // Randomly remove some numbers to create an unfinished Sudoku puzzle - if(numberOfZ>=82){throw new Error("Number greater than number of fields")} - - for (let i = 0; i < numberOfZ; i++) { - const row = Math.floor(Math.random() * 9); - const col = Math.floor(Math.random() * 9); - if(puzzle[row][col] ===0 ){i--} - puzzle[row][col] = 0; - } - - return puzzle; - } - - export { generateSudoku }; - \ No newline at end of file diff --git a/main.js b/main.js index c6d6ca615d36e14521596b3eea66982c68d4a8ee..6d328bc74559df9a6b65a6c80e177860c5250c68 100644 --- a/main.js +++ b/main.js @@ -1,110 +1,30 @@ -import { solveSudoku } from "./solver.js"; +import { solveSudoku } from "./public/solver.js"; import http from 'node:http'; -import { generateSudoku } from "./generateSudoku.js"; -import express from "express" +import { generateSudoku } from "./public/generateSudoku.js"; +import express, { raw } from "express"; +import path from 'path'; // Add this line to import the path module +// const sudokuBoard = generateSudoku(50); +// const rawString = sudokuBoard.map(row => row.map(num => `<td>${num}</td>`).join('')).map(row => `<tr>${row}</tr>`).join(''); +// console.log(rawString) -const sudokuBoard = generateSudoku(50) -console.log(sudokuBoard) -const rawString = sudokuBoard.map(row => row.map(num => `<td>${num}</td>`).join('')).map(row => `<tr>${row}</tr>`).join(''); - - -const sol = solveSudoku(sudokuBoard); +// 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> - * { - margin-bottom: 20px; - } - - #difficulty { - display: block; - } - 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> - <h1>Leon baut sein JS</h1> - - <label for="difficulty">Select a Difficulty (Number of empty fields you still have to figure out!)</label> - <select id="difficulty" display="block"> - <option value="easy">easy</option> - <option value="mid">mid</option> - <option value="hard">hard</option> - </select> - - <table>${rawString}</table> - - <button onclick="displaySolution()">Toggle Solution</button> - - - <table id="solutionTable" style="display: none;">${solString}</table> - - <script> - function generateSudoku() { - const sudokuBoard = document.getElementById('solutionTable'); - if (solutionTable.style.display === "none") { - solutionTable.style.display = 'table'; // Show the solution table - } else { - solutionTable.style.display = "none"; // Hide the solution table - } - } - - - - 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"; // Hide the solution table - } - } - </script> - - </body> - </html> -`; +// const solString = sol.map(row => row.map(num => `<td>${num}</td>`).join('')).map(row => `<tr>${row}</tr>`).join(''); const router = express.Router(); const port = 8080; -let app = express() +let app = express(); -app.get("/",(req, res) => { - app.use('/solution', router); - res.statusCode = 200; +app.use(express.static('public')); - res.end(htmlResponse); +app.get('/', (req, res) => { + res.statusCode(200) + res.sendFile(path.join(__dirname, 'public', 'index.html')); }); -router.get("/solution", (req, res) => { - - res.end(solString) -}) - -app.use(router) app.listen(port, () => { - console.log(`Server running at http://localhost:${port}`); + console.log(`Server running at http://localhost:${port}`); }); - - - - - - - - diff --git a/solver.js b/solver.js deleted file mode 100644 index 0652859729964740b2e7bdfc51f96116b08fc19a..0000000000000000000000000000000000000000 --- a/solver.js +++ /dev/null @@ -1,50 +0,0 @@ -function solveSudoku(board) { - const size = 9; - - function isValid(num, row, col) { - // Check if 'num' is not present in the current row, column, and 3x3 grid - for (let i = 0; i < size; i++) { - if ( - board[row][i] === num || - board[i][col] === num || - board[row - (row % 3) + Math.floor(i / 3)][col - (col % 3) + (i % 3)] === num - ) { - return false; - } - } - return true; - } - - function solve() { - for (let row = 0; row < size; row++) { - for (let col = 0; col < size; col++) { - if (board[row][col] === 0) { - for (let num = 1; num <= size; num++) { - if (isValid(num, row, col)) { - board[row][col] = num; - - if (solve()) { - return true; // If the current configuration leads to a solution - } - - // If the current configuration doesn't lead to a solution, backtrack - board[row][col] = 0; - } - } - return false; // If no number can be placed at this cell - } - } - } - return true; // If the entire board is filled - } - - if (solve()) { - return board; // Return the solved Sudoku board - } else { - return "No solution exists."; // Return a message if no solution exists - } - } - - - export {solveSudoku } - \ No newline at end of file