Commit 77dcb603 authored by nrossol's avatar nrossol
Browse files

Implemented clicking feature to reveal a square's solution.

parent 3578d573
#GUI for sudoku solver by Nathan Rossol, Computer Science sophomore at the University of Michigan #GUI for sudoku solver by Nathan Rossol, Computer Science sophomore at the University of Michigan
from sudokuSolverPython import solveSudoku from sudokuSolverPython import solveSudoku, solve
import pygame import pygame
import os import os
grid = [ [ 3, 2, 1, 0, 5, 0, 9, 4, 7 ], tempGrid = [ [ 3, 2, 1, 0, 5, 0, 9, 4, 7 ],
[7, 8, 0, 0, 1, 0, 0, 6, 5], [7, 8, 0, 0, 1, 0, 0, 6, 5],
[0, 0, 6, 7, 0, 4, 1, 0, 0], [0, 0, 6, 7, 0, 4, 1, 0, 0],
[5, 4, 9, 0, 0, 0, 7, 8 ,6], [5, 4, 9, 0, 0, 0, 7, 8 ,6],
...@@ -13,6 +13,19 @@ grid = [ [ 3, 2, 1, 0, 5, 0, 9, 4, 7 ], ...@@ -13,6 +13,19 @@ grid = [ [ 3, 2, 1, 0, 5, 0, 9, 4, 7 ],
[0 ,3 ,0, 2, 0, 7, 0, 5, 0], [0 ,3 ,0, 2, 0, 7, 0, 5, 0],
[2, 0, 7, 0, 4, 0, 8, 0, 3] ] [2, 0, 7, 0, 4, 0, 8, 0, 3] ]
solvedGrid = [ [ 3, 2, 1, 0, 5, 0, 9, 4, 7 ],
[7, 8, 0, 0, 1, 0, 0, 6, 5],
[0, 0, 6, 7, 0, 4, 1, 0, 0],
[5, 4, 9, 0, 0, 0, 7, 8 ,6],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 9, 0, 6, 0, 0, 0],
[1, 0, 5, 0, 6, 0, 4, 0, 2],
[0 ,3 ,0, 2, 0, 7, 0, 5, 0],
[2, 0, 7, 0, 4, 0, 8, 0, 3] ]
solve(solvedGrid)
#Basic Colors: #Basic Colors:
BLACK = (0,0,0) BLACK = (0,0,0)
WHITE = (255,255,255) WHITE = (255,255,255)
...@@ -33,9 +46,14 @@ clock = pygame.time.Clock() ...@@ -33,9 +46,14 @@ clock = pygame.time.Clock()
#Main Loop: #Main Loop:
done = False done = False
while not done: while not done:
#event detection:
clicked = False
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
done = True done = True
if event.type == pygame.MOUSEBUTTONUP:
clicked = True
pos = pygame.mouse.get_pos()
screen.fill(BLACK) screen.fill(BLACK)
...@@ -45,10 +63,18 @@ while not done: ...@@ -45,10 +63,18 @@ while not done:
pygame.draw.rect(screen, WHITE, (i,j,box_size,box_size), 0) pygame.draw.rect(screen, WHITE, (i,j,box_size,box_size), 0)
pygame.draw.rect(screen, WHITE, (0,495,490,100), 0) pygame.draw.rect(screen, WHITE, (0,495,490,100), 0)
#mouse click:
if clicked:
print("CLICKED")
if pos[1] < screen_height:
row = int(pos[0]/(box_size+margin))
col = int(pos[1]/(box_size+margin))
tempGrid[row][col] = solvedGrid[row][col]
#fill in grid: #fill in grid:
for i in range(0,9): for i in range(0,9):
for j in range(0,9): for j in range(0,9):
number_surface = font.render(str(grid[i][j]), False, (0,0,0)) number_surface = font.render(str(tempGrid[i][j]), False, (0,0,0))
screen.blit(number_surface, (7 + i*(box_size + margin), 7 + j*(box_size + margin))) screen.blit(number_surface, (7 + i*(box_size + margin), 7 + j*(box_size + margin)))
#update display #update display
......
...@@ -93,6 +93,6 @@ def solve(grid): ...@@ -93,6 +93,6 @@ def solve(grid):
else: else:
print("Unsolvable :(") print("Unsolvable :(")
printBoard(grid) #printBoard(grid)
#return grid;
solve(grid)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment