triples sudoku
3 mins read

source code for sudoku game in python

## Python Sudoku Game Source Code

### Overview

In this article, we provide a comprehensive source code for a Sudoku game implemented in Python. Sudoku is a popular puzzle game that involves filling a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid contain all of the digits from 1 to 9. The following Python code snippet allows you to create and solve Sudoku puzzles.

### Installation

Before you can run the Sudoku game, ensure you have Python installed on your system. No additional packages are required as the code is self-contained.

### Source Code

“`python
import random

def create_board():
board = [[0 for _ in range(9)] for _ in range(9)]
for i in range(9):
for j in range(9):
if random.randint(0, 1):
board[i][j] = random.randint(1, 9)
return board

def print_board(board):
for i in range(9):
if i % 3 == 0 and i != 0:
print(“-” * 21)
for j in range(9):
if j % 3 == 0 and j != 0:
print(“|”, end=” “)
print(board[i][j], end=” “)
print()

def is_valid(board, row, col, num):
for x in range(9):
if board[row][x] == num or board[x][col] == num:
return False
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(start_row, start_row + 3):
for j in range(start_col, start_col + 3):
if board[i][j] == num:
return False
return True

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

def main():
board = create_board()
print(“Sudoku Puzzle:”)
print_board(board)
print(“\nSolving the Sudoku…”)
if solve(board):
print(“\nSolution:”)
print_board(board)
else:
print(“No solution exists for the given puzzle.”)

if __name__ == “__main__”:
main()
“`

### Frequently Asked Questions (FAQ)

**Q: Can I modify the difficulty of the Sudoku puzzle?**

A: The current implementation generates a puzzle with random numbers. To modify the difficulty, you can adjust the number of pre-filled cells in the `create_board` function.

**Q: What if the Sudoku puzzle has no solution?**

A: If the Sudoku puzzle has no solution, the `solve` function will return `False`, and the program will indicate that no solution exists.

**Q: Can I save and load puzzles?**

A: The current code does not include save and load functionality. You can extend the code to include these features by using file I/O operations to write to and read from a file.

**Q: How can I check if the solution is correct?**

A: After solving the puzzle, you can compare the solved board with the original board to ensure that all numbers from 1 to 9 are present in each row, column, and 3×3 subgrid without repetition.

**Q: Can I run this Sudoku game in a web browser?**

A: The Python code provided here is a command-line application. To run Sudoku in a web browser, you would need to convert the code into a web application using a framework like Flask or Django, and then deploy it on a web server.