0% found this document useful (0 votes)
16 views5 pages

Number Puzzle Game (Sudoku)

The document outlines a Python program for generating and solving Sudoku puzzles. It includes methods for creating a puzzle with varying difficulty levels, validating placements, and displaying the board. The program utilizes backtracking to solve the puzzle and features a class structure for organization.

Uploaded by

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

Number Puzzle Game (Sudoku)

The document outlines a Python program for generating and solving Sudoku puzzles. It includes methods for creating a puzzle with varying difficulty levels, validating placements, and displaying the board. The program utilizes backtracking to solve the puzzle and features a class structure for organization.

Uploaded by

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

NAME: RITAM KUNDU

SECTION: E
ROLL NO: 79
ENROLLMENT NUMBER: 12024052011068
SUBJECT NAME: Introduction to AI and Data Science
using Python
SUBJECT CODE: IVC183
NUMBER PUZZLE GAME(e.g. SUDOKU)

Generate Puzzle:

Fills diagonal grids and solves the board to create a complete Sudoku.

Removes numbers based on difficulty (easy, medium, hard).

Solve Puzzle

Uses backtracking to solve the puzzle.

Display Board

Neatly displays the Sudoku grid

Source Code
import random

class Sudoku:

def _init_(self):

self.board = [[0] * 9 for _ in range(9)]

def is_valid(self, num, row, col):

"""Check if a number can be placed at board[row][col]."""

# Check row

if num in self.board[row]:

return False

# Check column

if num in [self.board[i][col] for i in range(9)]:


return False

# Check 3x3 subgrid

box_row, box_col = row // 3 * 3, col // 3 * 3

for i in range(box_row, box_row + 3):

for j in range(box_col, box_col + 3):

if self.board[i][j] == num:

return False

return True

def solve(self):

"""Solve the Sudoku puzzle using backtracking."""

for row in range(9):

for col in range(9):

if self.board[row][col] == 0:

for num in range(1, 10):

if self.is_valid(num, row, col):

self.board[row][col] = num

if self.solve():

return True

self.board[row][col] = 0

return False

return True

def generate_puzzle(self, difficulty="medium"):

"""Generate a Sudoku puzzle."""

# Fill the diagonal 3x3 grids

for i in range(0, 9, 3):

self.fill_diagonal_grid(i, i)
# Solve the board to ensure it's valid

self.solve()

# Remove numbers based on difficulty

self.remove_numbers(difficulty)

def fill_diagonal_grid(self, row, col):

"""Fill a 3x3 grid with random numbers."""

nums = random.sample(range(1, 10), 9)

for i in range(3):

for j in range(3):

self.board[row + i][col + j] = nums.pop()

def remove_numbers(self, difficulty):

"""Remove numbers from the board based on difficulty."""

levels = {"easy": 30, "medium": 40, "hard": 50}

remove_count = levels.get(difficulty, 40)

for _ in range(remove_count):

row, col = random.randint(0, 8), random.randint(0, 8)

while self.board[row][col] == 0:

row, col = random.randint(0, 8), random.randint(0, 8)

self.board[row][col] = 0

def display(self):

"""Print the Sudoku board."""

for row in self.board:

print(" ".join(str(num) if num != 0 else "." for num in row))

# Create and play Sudoku


game = Sudoku()

game.generate_puzzle(difficulty="medium")

print("Sudoku Puzzle:")

game.display()

print("\nSolved Sudoku:")

game.solve()

game.display()

OUTPUT

You might also like