This commit is contained in:
ZennDev1337 2024-04-02 11:44:12 +02:00
parent 8521834c15
commit c38f48b727
4 changed files with 185 additions and 0 deletions

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sudoku</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<div class="sudoku-container">
<table id="sudoku-board">
<!-- You can dynamically generate the sudoku board using JavaScript -->
</table>
<button class="validButton" onclick="isValidSudoku(getBoardFromInputs())">IsValid?</button>
<p id="result">Result: </p>
</div>
<script src="./script.js"></script>
<script src="./yourScript.js"></script>
</body>
</html>

View file

@ -0,0 +1,38 @@
function getBoardFromInputs() {
let sudokuInputs = document.querySelectorAll("#sudoku-board input");
let sudokuBoard = [];
sudokuInputs.forEach((input, index) => {
let rowIndex = Math.floor(index / 9);
let colIndex = index % 9;
if (!sudokuBoard[rowIndex]) {
sudokuBoard[rowIndex] = [];
}
sudokuBoard[rowIndex][colIndex] = parseInt(input.value) || 0;
});
return sudokuBoard;
}
function initializeBoard() {
let table = document.getElementById("sudoku-board");
for (let i = 0; i < 9; i++) {
let row = table.insertRow();
for (let j = 0; j < 9; j++) {
let cell = row.insertCell();
let value = board[i][j];
let input = document.createElement("input");
input.type = "text";
input.maxLength = 1;
input.value = value === "." ? "" : value;
input.addEventListener("input", function () {
// Update the value in the board1 array when user changes input
board[i][j] = this.value === "" ? "." : this.value;
});
cell.appendChild(input);
}
}
}
window.onload = initializeBoard;

View file

@ -0,0 +1,49 @@
.sudoku-container {
margin: 50px auto;
text-align: center;
}
#sudoku-board {
border-collapse: collapse;
margin: 0 auto;
}
#sudoku-board td {
border: 1px solid #000;
width: 50px;
height: 50px;
}
#sudoku-board input {
height: 100%;
max-width: 50px;
text-align: center;
font-size: 1.2em;
}
@import url("https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Koulen&family=Lato&family=Nunito&family=Playfair+Display:ital@1&family=Prata&family=Raleway:ital,wght@1,100&family=Roboto&family=Roboto+Condensed&family=Teko&display=swap");
.validButton {
margin-top: 20px;
font-family: Roboto, sans-serif;
font-weight: 0;
font-size: 14px;
color: #fff;
background: linear-gradient(90deg, #0066cc 0%, #c500cc 100%);
padding: 10px 30px;
border: 2px solid #0066cc;
box-shadow: rgb(0, 0, 0) 0px 0px 0px 0px;
border-radius: 50px;
transition: 1000ms;
transform: translateY(0);
align-items: center;
cursor: pointer;
}
.validButton:hover {
transition: 1000ms;
padding: 10px 50px;
transform: translateY(-0px);
background: linear-gradient(90deg, #0066cc 0%, #c500cc 100%);
color: #0066cc;
border: solid 2px #0066cc;
}

View file

@ -0,0 +1,73 @@
let board = [
["5", "3", ".", ".", "7", ".", ".", ".", "."],
["6", ".", ".", "1", "9", "5", ".", ".", "."],
[".", "9", "8", ".", ".", ".", ".", "6", "."],
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
[".", "6", ".", ".", ".", ".", "2", "8", "."],
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
[".", ".", ".", ".", "8", ".", ".", "7", "9"],
];
function isValidRow(board, row) {
let seen = new Set();
for (let i = 0; i < 9; i++) {
let num = board[row][i];
if (num !== 0 && seen.has(num)) {
return false;
}
seen.add(num);
}
return true;
}
function isValidColumn(board, col) {
let seen = new Set();
for (let i = 0; i < 9; i++) {
let num = board[i][col];
if (num !== 0 && seen.has(num)) {
return false;
}
seen.add(num);
}
return true;
}
function isValidSubgrid(board, startRow, startCol) {
let seen = new Set();
for (let row = startRow; row < startRow + 3; row++) {
for (let col = startCol; col < startCol + 3; col++) {
let num = board[row][col];
if (num !== 0 && seen.has(num)) {
return false;
}
seen.add(num);
}
}
return true;
}
function isValidSudoku(board) {
result: HTMLElement = document.getElementById("result");
for (let i = 0; i < 9; i++) {
if (!isValidRow(board, i) || !isValidColumn(board, i)) {
console.log("Board is Unvalid");
result.innerHTML = "Result: Unvalid";
return false;
}
}
for (let i = 0; i < 9; i += 3) {
for (let j = 0; j < 9; j += 3) {
if (!isValidSubgrid(board, i, j)) {
console.log("Board is Unvalid");
result.innerHTML = "Result: Unvalid";
return false;
}
}
}
console.log("Board is Valid");
result.innerHTML = "Result: Valid";
return true;
}