Skip to content
Snippets Groups Projects
Commit 3d8e6fa3 authored by LeonLicher's avatar LeonLicher
Browse files

better capsulation of html css js js and js in this case,generate new html...

better capsulation of html css js js and js in this case,generate new html based on button click not just display show  like before but no sudoku yet
parent b320a80e
No related branches found
No related tags found
No related merge requests found
Pipeline #172930 passed
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
import { solveSudoku } from "./solver.js"; import { solveSudoku } from "./public/solver.js";
import http from 'node:http'; import http from 'node:http';
import { generateSudoku } from "./generateSudoku.js"; import { generateSudoku } from "./public/generateSudoku.js";
import express from "express" 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 // 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 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 router = express.Router(); const router = express.Router();
const port = 8080; const port = 8080;
let app = express() let app = express();
app.get("/",(req, res) => { app.use(express.static('public'));
app.use('/solution', router);
res.statusCode = 200;
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, () => { app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`); console.log(`Server running at http://localhost:${port}`);
}); });
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment