Commit e8adde56 authored by nrossol's avatar nrossol
Browse files

Implemented solve button.

parent 77dcb603
......@@ -25,20 +25,23 @@ solvedGrid = [ [ 3, 2, 1, 0, 5, 0, 9, 4, 7 ],
solve(solvedGrid)
#Basic Colors:
BLACK = (0,0,0)
WHITE = (255,255,255)
BLUE = (0,0,255)
#Initialize Pygame and variables::
pygame.init()
pygame.display.init()
screen_width = 490
screen_height = 490
buttonArea = 100 #height of area where buttons will be below the grid
box_size = 50
margin = 5
font = pygame.font.SysFont("Comic Sans MS",30)
font2 = pygame.font.SysFont("Arial", 28)
screen = pygame.display.set_mode([screen_width,screen_height+buttonArea])
pygame.display.set_caption("Sudoku")
clock = pygame.time.Clock()
......@@ -62,14 +65,20 @@ while not done:
for j in range(0,screen_height,box_size+margin):
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, BLUE, (170, 510, 150, 60), 0)
word_surface = font2.render('Solve Puzzle', False, (0,0,0))
screen.blit(word_surface, (180, 520))
#mouse click:
if clicked:
print("CLICKED")
if pos[1] < screen_height:
if pos[1] < screen_height: #grid clicked
row = int(pos[0]/(box_size+margin))
col = int(pos[1]/(box_size+margin))
tempGrid[row][col] = solvedGrid[row][col]
print("Grid Clicked")
elif (pos[1] >= 510 and pos[1] <= 570) and (pos[0] >= 170 and pos[0] <= 320):
tempGrid = solvedGrid
print("Solve Button Clicked")
#fill in grid:
for i in range(0,9):
......
#Recursive/backtracking algorithm to solve sudoku puzzle if possible.
#By Nathan Rossol, Computer Science sophomore at the University of Michigan
grid = [ [ 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] ]
#TO RUN THIS PROGRAM: Instal pygame and use terminal to run gui.py. Make sure this file and gui.py
#are in the same directory.
#Game rules: The grid has 81 squares: 9 boxes of 9 squares. Each box must contain
#all numbers 1-9 in its squares, and each number can appear only once in any row,
......@@ -89,7 +83,7 @@ def printBoard(grid):
def solve(grid):
if solveSudoku(grid):
print("SOLVED!")
print("Solveable :)!")
else:
print("Unsolvable :(")
......
Markdown is supported
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