[go: up one dir, main page]

0% found this document useful (0 votes)
6 views3 pages

Soduko

This document outlines the structure and functionality of a Sudoku game implemented in HTML, CSS, and JavaScript. It includes a grid layout for the Sudoku board, functions to generate puzzles, solve them using backtracking, and handle user input. The game allows players to interactively fill in numbers while ensuring valid Sudoku rules are maintained.

Uploaded by

milanimahlati.12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Soduko

This document outlines the structure and functionality of a Sudoku game implemented in HTML, CSS, and JavaScript. It includes a grid layout for the Sudoku board, functions to generate puzzles, solve them using backtracking, and handle user input. The game allows players to interactively fill in numbers while ensuring valid Sudoku rules are maintained.

Uploaded by

milanimahlati.12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<!

DOCTYpE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sudoku Game</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
text-align: center;
margin: 50px;
background-color: #f4f4f4;
}

h1 {
color: #333;
}

#sudoku-board {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-gap: 2px;
max-width: 400px;
margin: 0 auto;
}

.cell {
width: 100%;
height: 50px;
font-size: 20px;
box-sizing: border-box;
border: 1px solid #ddd;
background-color: #fff;
display: flex;
align-items: center;
justify-content: center;
}

.cell.input {
cursor: pointer;
}

.cell.input:hover {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>Sudoku Game</h1>

<div id="sudoku-board"></div>

<script>
// Function to generate a random Sudoku puzzle
function generateSudoku() {
const sudokuBoard = [];
for (let i = 0; i < 9; i++) {
const row = [];
for (let j = 0; j < 9; j++) {
row.push(0); // Initialize with zeros
}
sudokuBoard.push(row);
}

// Fill in the Sudoku puzzle with a valid solution


solveSudoku(sudokuBoard);

// Remove some numbers to create the puzzle


removeNumbers(sudokuBoard, 40); // Adjust the difficulty by changing
the number of removed numbers

return sudokuBoard;
}

// Function to solve the Sudoku puzzle using backtracking


function solveSudoku(board) {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (board[i][j] === 0) {
for (let num = 1; num <= 9; num++) {
if (isValid(board, i, j, num)) {
board[i][j] = num;

if (solveSudoku(board)) {
return true;
}

board[i][j] = 0; // If placing num at (i, j)


doesn't lead to a solution, backtrack
}
}
return false; // No valid number found, need to backtrack
}
}
}
return true; // All cells filled, solution found
}

// Function to check if a number can be placed at a specific cell


function isValid(board, row, col, num) {
// Check if the number exists in the row or column
for (let i = 0; i < 9; i++) {
if (board[row][i] === num || board[i][col] === num) {
return false;
}
}

// Check if the number exists in the 3x3 subgrid


const startRow = Math.floor(row / 3) * 3;
const startCol = Math.floor(col / 3) * 3;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[startRow + i][startCol + j] === num) {
return false;
}
}
}
return true;
}

// Function to remove a specified number of numbers from the Sudoku puzzle


function removeNumbers(board, count) {
let removedCount = 0;
while (removedCount < count) {
const i = Math.floor(Math.random() * 9);
const j = Math.floor(Math.random() * 9);

if (board[i][j] !== 0) {
board[i][j] = 0;
removedCount++;
}
}
}

// Function to render the Sudoku board


function renderBoard(board) {
const sudokuBoardElement = document.getElementById('sudoku-board');

for (let i = 0; i < 9; i++) {


for (let j = 0; j < 9; j++) {
const cellElement = document.createElement('div');
cellElement.classList.add('cell');
cellElement.textContent = board[i][j] !== 0 ? board[i][j] : '';

// Make input cells interactive


if (board[i][j] === 0) {
cellElement.classList.add('input');
cellElement.onclick = () => handleCellClick(i, j);
}

sudokuBoardElement.appendChild(cellElement);
}
}
}

// Function to handle clicks on input cells


function handleCellClick(row, col) {
const value = prompt('Enter a number (1-9):');
if (value !== null) {
const num = parseInt(value, 10);
if (!isNaN(num) && num >= 1 && num <= 9) {
// Update the board and re-render
sudokuBoard[row][col] = num;
clearBoard();
renderBoard(sudokuBoard);
} else

You might also like