Skip to content

Commit

Permalink
Merge pull request #1 from RahulVadisetty91/RahulVadisetty91-patch-1
Browse files Browse the repository at this point in the history
Enhance Sudoku Game Script: Refactoring, Error Handling, and AI Integration
  • Loading branch information
RahulVadisetty91 authored Aug 21, 2024
2 parents ce2698d + 4c0d856 commit 22b4f0a
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions sudoku_game_refactor_and_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import sys
import os
import pygame

# Ensure the correct path is added for imports
sys.path.append(os.path.join("objects"))

# Importing the necessary modules
from SudokuSquare import SudokuSquare
from utils import reconstruct
from GameResources import rows, cols

def play(values, result, history):
"""Main function to play the Sudoku game."""
# Reconstruct the Sudoku assignments from the result and history
assignments = reconstruct(result, history)
pygame.init()

# Set up the screen size and create the display
size = width, height = 700, 700
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Sudoku Game")

# Load the background image
background_image = pygame.image.load("./images/sudoku-board-bare.jpg").convert()

clock = pygame.time.Clock()

while True:
pygame.event.pump()
theSquares = []

# Calculate positions and initialize Sudoku squares
for y in range(9):
for x in range(9):
startX = (x // 3 * 57) + 38 + (x % 3 * 61)
startY = (y // 3 * 57) + 35 + (y % 3 * 65)

string_number = values[rows[y] + cols[x]]
number = int(string_number) if string_number.isdigit() else None
theSquares.append(SudokuSquare(number, startX, startY, editable="N", x=x, y=y))

# Draw the background and squares on the screen
screen.blit(background_image, (0, 0))
for square in theSquares:
square.draw(screen)

pygame.display.flip()
clock.tick(5)

if not assignments:
break
box, value = assignments.pop()
values[box] = value

# Keep the game window open until the user closes it
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

if __name__ == "__main__":
# Example values for testing (replace with actual values as needed)
values = {}
result = {}
history = []
play(values, result, history)

0 comments on commit 22b4f0a

Please sign in to comment.