Skip to content

Commit

Permalink
Added hints
Browse files Browse the repository at this point in the history
  • Loading branch information
ymercad0 committed Jul 26, 2020
1 parent 1c1606a commit 28f92c3
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions sudoku_gui.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sudoku_alg import valid, solve, find_empty
from sys import exit
from copy import deepcopy
from sys import exit
import pygame
import time
import random
Expand All @@ -12,7 +12,6 @@ def generate():
if event.type == pygame.QUIT:
exit()
board = [[0 for i in range(9)] for j in range (9)]

# puts one random number, then solves the board to generate a board
for i in range(9):
for j in range(9):
Expand All @@ -22,7 +21,6 @@ def generate():
continue
else:
board[i][j] = 0

partialBoard = deepcopy(board) #copies board without being modified after solve is called
if solve(board):
return partialBoard
Expand Down Expand Up @@ -120,6 +118,20 @@ def visualSolve(self, wrong, time):
pygame.time.delay(63)
self.redraw({}, wrong, time)

def hint(self, keys):
'''Shows a random empty tile's solved value as a hint'''
while True: #keeps generating i,j coords until it finds a valid random spot
i = random.randint(0, 8)
j = random.randint(0, 8)
if self.board[i][j] == 0: #hint spot has to be empty
if (j,i) in keys:
del keys[(j,i)]
self.board[i][j] = self.solvedBoard[i][j]
self.tiles[i][j].value = self.solvedBoard[i][j]
return True

elif self.board == self.solvedBoard:
return False
class Tile:
'''Represents each white tile/box on the grid'''
def __init__(self, value, window, x1, y1):
Expand Down Expand Up @@ -148,7 +160,6 @@ def clicked(self, mousePos):

def main():
'''Runs the main Sudoku GUI/Game'''
wrong = 0
screen = pygame.display.set_mode((540, 590))
screen.fill((255, 255, 255))
pygame.display.set_caption("Sudoku")
Expand All @@ -166,10 +177,10 @@ def main():
pygame.display.flip()

#initiliaze values and variables
wrong = 0
board = Board(screen)
selected = -1,-1 #NoneType error when selected = None, easier to just format as a tuple whose value will never be used
keyDict = {}

running = True
startTime = time.time()
while running:
Expand Down Expand Up @@ -240,6 +251,9 @@ def main():
board.board[selected[1]][selected[0]] = keyDict[selected] #assigns to actual board so that the correct value can't be modified
del keyDict[selected]

if event.key == pygame.K_h:
board.hint(keyDict)

if event.key == pygame.K_SPACE:
for i in range(9):
for j in range(9):
Expand All @@ -253,7 +267,6 @@ def main():
running = False

board.redraw(keyDict, wrong, passedTime)

while True: #another running loop so that the program ONLY closes when user closes program
for event in pygame.event.get():
if event.type == pygame.QUIT:
Expand Down

0 comments on commit 28f92c3

Please sign in to comment.