Skip to content

Commit 22b4f0a

Browse files
Merge pull request #1 from RahulVadisetty91/RahulVadisetty91-patch-1
Enhance Sudoku Game Script: Refactoring, Error Handling, and AI Integration
2 parents ce2698d + 4c0d856 commit 22b4f0a

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

sudoku_game_refactor_and_fix.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sys
2+
import os
3+
import pygame
4+
5+
# Ensure the correct path is added for imports
6+
sys.path.append(os.path.join("objects"))
7+
8+
# Importing the necessary modules
9+
from SudokuSquare import SudokuSquare
10+
from utils import reconstruct
11+
from GameResources import rows, cols
12+
13+
def play(values, result, history):
14+
"""Main function to play the Sudoku game."""
15+
# Reconstruct the Sudoku assignments from the result and history
16+
assignments = reconstruct(result, history)
17+
pygame.init()
18+
19+
# Set up the screen size and create the display
20+
size = width, height = 700, 700
21+
screen = pygame.display.set_mode(size)
22+
pygame.display.set_caption("Sudoku Game")
23+
24+
# Load the background image
25+
background_image = pygame.image.load("./images/sudoku-board-bare.jpg").convert()
26+
27+
clock = pygame.time.Clock()
28+
29+
while True:
30+
pygame.event.pump()
31+
theSquares = []
32+
33+
# Calculate positions and initialize Sudoku squares
34+
for y in range(9):
35+
for x in range(9):
36+
startX = (x // 3 * 57) + 38 + (x % 3 * 61)
37+
startY = (y // 3 * 57) + 35 + (y % 3 * 65)
38+
39+
string_number = values[rows[y] + cols[x]]
40+
number = int(string_number) if string_number.isdigit() else None
41+
theSquares.append(SudokuSquare(number, startX, startY, editable="N", x=x, y=y))
42+
43+
# Draw the background and squares on the screen
44+
screen.blit(background_image, (0, 0))
45+
for square in theSquares:
46+
square.draw(screen)
47+
48+
pygame.display.flip()
49+
clock.tick(5)
50+
51+
if not assignments:
52+
break
53+
box, value = assignments.pop()
54+
values[box] = value
55+
56+
# Keep the game window open until the user closes it
57+
while True:
58+
for event in pygame.event.get():
59+
if event.type == pygame.QUIT:
60+
pygame.quit()
61+
sys.exit()
62+
63+
if __name__ == "__main__":
64+
# Example values for testing (replace with actual values as needed)
65+
values = {}
66+
result = {}
67+
history = []
68+
play(values, result, history)

0 commit comments

Comments
 (0)