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