Generate and Solve Sudokus with CSS

27 May 2023 Balmiki Mandal 0 MERN Full Stack

Generating (and Solving!) Sudokus in CSS

Sudoku is a popular logic-based number puzzle that has been around for centuries. It involves arranging the numbers 1 to 9 in a 9x9 grid, with each number appearing only once in each row, column or 3x3 box. In recent times, it has become even more popular with the introduction of websites and mobile apps that let you play the game on your phone or computer.

But did you know that you can also generate and solve Sudokus using nothing but CSS? That's right - with a few lines of code, you can create and solve puzzles just like you would using any other app or website. In this blog post, we'll go over how to do just that.

Generating Sudokus

The first step in creating a Sudoku puzzle is generating the initial grid. To do this, we'll use the CSS Grid Layout Module. This module provides an easy-to-use syntax for laying out a grid of items. We'll use the following code to create a 9x9 grid with 81 boxes:

grid-template-rows: repeat(9, 1fr); grid-template-columns: repeat(9, 1fr);

Next, we need to assign each box a unique ID. This will allow us to easily refer to specific boxes when solving the puzzle. To do this, we'll use the nth-child selector, which allows us to select elements based on their position in a container. We'll use the following code to assign each box an ID from 1 to 81:

grid-row-start: nth; grid-column-start: nth;

Now, we need to add the numbers to the boxes. We can do this by assigning each box a data-attribute, which we can then use to set the content of the boxes. We'll use the following code to assign each box a data-attribute from 1 to 81:

data-number="nth";

And finally, we need to add some styling to our boxes. We'll use the following code to give each box a black border and a white background:

border: 1px solid #000; background: #fff;

Solving Sudokus

Once we have generated our Sudoku puzzle, we need to be able to solve it. The easiest way to do this is by using a backtracking algorithm. This is a recursive algorithm that works by trying all possible solutions until it finds a valid one. We'll use the following code to implement the algorithm:

def solve(board): for row in range(9): for col in range(9): if board[row][col] == 0: for num in range(1, 10): if is_valid(board, row, col, num): board[row][col] = num if solve(board): return True else: board[row][col] = 0 return False return True

Once we've implemented the algorithm, we can then use it to solve our Sudoku puzzle. To do this, we need to first convert our grid of boxes into a 2D array, which we can then use as input for the algorithm. We'll use the following code to do this:

let board = []; for (let i=0; i<9; i++) { const row = []; for (let j=0; j<9; j++) { row.push(Number(document.querySelectorAll(`[data-number='${(i*9)+j}']`).dataset.number)); } board.push(row); }

And finally, we can call the solve() function with the board array as an argument. Once the function finishes running, it will return a solved 2D array which we can then use to update the boxes on the page. We'll use the following code to do this:

const solvedBoard = solve(board); for (let i=0; i<9; i++) { for (let j=0; j<9; j++) { document.querySelectorAll(`[data-number='${(i*9)+j}']`).innerHTML = solvedBoard[i][j]; } }

And there you have it! With just a few lines of code, you've created your very own Sudoku generator and solver! Now go ahead and try it out for yourself.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.