- Notifications
You must be signed in to change notification settings - Fork0
sudoku-solver#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Binary file added.DS_Store
Binary file not shown.
11 changes: 10 additions & 1 deletionREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
# Sudoku Game In Python | ||
### run: | ||
* python gui.py | ||
### Mac | ||
* python3 gui.py |
72 changes: 72 additions & 0 deletionschooseLevel.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import pygame | ||
BLACK = (0, 0, 0) | ||
WHITE = (255, 255, 255) | ||
GREEN = (0, 255, 0) | ||
L_GREEN = (150, 255, 150) | ||
RED = (255, 0, 0) | ||
L_RED = (255, 204, 203) | ||
GRAY = (80, 80, 80) | ||
YELLOW = (255, 255, 0) | ||
# | ||
pygame.init() | ||
X = 300 | ||
Y = 200 | ||
size = (X, Y) | ||
window = pygame.display.set_mode(size) | ||
font = pygame.font.Font('freesansbold.ttf', 25) | ||
def drawButton(left, top, color, textInButton): | ||
rectSize = pygame.Rect(left, top, 60, 30) | ||
pygame.draw.rect(window, color, rectSize) # left, top, width, height | ||
pygame.draw.rect(window, BLACK, rectSize, 3) | ||
fontButton = pygame.font.Font('freesansbold.ttf', 20) | ||
textButton = fontButton.render(textInButton, True, BLACK, ) | ||
textRectButton = textButton.get_rect() | ||
textRectButton.center = (left + 30, top + 15) | ||
window.blit(textButton, textRectButton) | ||
def chooseLevel(): | ||
level = 0 | ||
text = font.render('choose difficulty level', True, BLACK, WHITE) | ||
textRect = text.get_rect() | ||
textRect.center = (X // 2, Y // 2 - 40) | ||
pygame.display.set_caption("Sudoku King") | ||
done = True | ||
while done: | ||
window.fill(WHITE) | ||
window.blit(text, textRect) | ||
drawButton(40, 100, GRAY, "1") | ||
drawButton(120, 100, GRAY, "2") | ||
drawButton(200, 100, GRAY, "3") | ||
pos = pygame.mouse.get_pos() | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
# deactivates the pygame library | ||
pygame.quit() | ||
# quit the program. | ||
quit() | ||
if event.type == pygame.MOUSEBUTTONDOWN: | ||
# print("Click ", pos) | ||
if (40 <= pos[0] <= 100) and (100 <= pos[1] <= 130): | ||
level = 1 | ||
if (120 <= pos[0] <= 180) and (100 <= pos[1] <= 130): | ||
level = 2 | ||
if (200 <= pos[0] <= 260) and (100 <= pos[1] <= 130): | ||
level = 3 | ||
if level != 0: | ||
# print(level) | ||
pygame.quit() | ||
return level | ||
# Draws the surface object to the screen. | ||
pygame.display.update() | ||
# chooseLevel() |
100 changes: 100 additions & 0 deletionsgenerator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import random | ||
import copy | ||
# firstBoard = [ | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
# [0, 0, 0, 0, 0, 0, 0, 0, 0] | ||
# ] | ||
def printBoard(board): | ||
for i in range(len(board)): | ||
if i % 3 == 0 and i != 0: | ||
print("- - - - - - - - - - - -") | ||
for j in range(len(board[0])): | ||
if j % 3 == 0 and j != 0: | ||
print(" | ", end="") | ||
if j == 8: # end of the row | ||
print(board[i][j]) | ||
else: | ||
print(str(board[i][j]) + " ", end="") | ||
def findEmpty(board): | ||
for y in range(len(board)): | ||
for x in range(len(board[0])): | ||
if board[y][x] == 0: | ||
return y, x # y = row , x = column | ||
# if we got here it mean that we finish the sudoku, so return none | ||
return None | ||
def validCheck(board, number, coordinates): | ||
# checking row | ||
for x in range(len(board[0])): | ||
if number == board[coordinates[0]][x] and coordinates[1] != x: # coordinates[0]= row | ||
return False | ||
# checking column | ||
for y in range(len(board)): | ||
if number == board[y][coordinates[1]] and coordinates[0] != y: | ||
return False | ||
# checking the box | ||
box_x = coordinates[1] // 3 | ||
box_y = coordinates[0] // 3 | ||
for y in range(box_y * 3, box_y * 3 + 3): | ||
for x in range(box_x * 3, box_x * 3 + 3): | ||
if number == board[y][x] and (y, x) != coordinates: | ||
return False | ||
return True | ||
def generateRandomBoard(board): | ||
# end condition:- getting to the end of the board - the function findEmpty return NONE | ||
find = findEmpty(board) | ||
if find is None: # if find != False | ||
return True | ||
else: | ||
row, col = find | ||
for number in range(1, 10): | ||
randomNumber = random.randint(1, 9) # TODO: need to work on the algorithm a bit more - | ||
# TODO: to not rand the same number over and over again | ||
if validCheck(board, randomNumber, (row, col)): | ||
board[row][col] = randomNumber | ||
if generateRandomBoard(board): | ||
return True | ||
board[row][col] = 0 | ||
return False | ||
def deleteCells(firstBoard,number): | ||
while number: | ||
row = random.randint(0, 8) | ||
col = random.randint(0, 8) | ||
if firstBoard[row][col] != 0: | ||
firstBoard[row][col] = 0 | ||
number = number - 1 | ||
def sudokuGenerate(firstBoard, level): | ||
# printBoard(firstBoard) | ||
generateRandomBoard(firstBoard) | ||
# printBoard(firstBoard) | ||
if level == 1: | ||
deleteCells(firstBoard,30) | ||
if level == 2: | ||
deleteCells(firstBoard,40) | ||
if level == 3: | ||
deleteCells(firstBoard,50) |
Binary file addedgifs/playing the game.gif
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedgifs/start the game.gif
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
199 changes: 199 additions & 0 deletionsgui.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import pygame | ||
from sudokuSolver import * | ||
from chooseLevel import * | ||
import time | ||
# Define some colors | ||
BLACK = (0, 0, 0) | ||
WHITE = (255, 255, 255) | ||
GREEN = (0, 255, 0) | ||
L_GREEN = (150, 255, 150) | ||
RED = (255, 0, 0) | ||
L_RED = (255, 204, 203) | ||
GRAY = (60, 60, 60) | ||
L_GRAY = (220, 220, 220) | ||
YELLOW = (255, 255, 0) | ||
# This sets the WIDTH and HEIGHT of each grid location | ||
WIDTH = HEIGHT = 50 | ||
# This sets the margin between each cell | ||
MARGIN = 5 | ||
numbers_1to9 = [pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5, pygame.K_6, pygame.K_7, pygame.K_8, | ||
pygame.K_9] | ||
# Set the width and height of the screen [width, height] | ||
size = (500, 500) | ||
# screen = pygame.display.set_mode(size) | ||
pygame.init() | ||
font = pygame.font.Font('freesansbold.ttf', 32) | ||
# pygame.display.set_caption("Sudoku King") | ||
# Loop until the user clicks the close button. | ||
done = False | ||
def cheatingAllTheWay(): | ||
for row in range(len(Board)): | ||
for column in range(len(Board[row])): | ||
Board[row][column] = solvedBoard[row][column] | ||
addNumToBoard(Board[row][column], row, column, L_GREEN) | ||
time.sleep(0.05) | ||
pygame.display.flip() | ||
finish() | ||
def addNumToBoard(number, row, column, color): | ||
addNewRect(row, column, WHITE, 5) | ||
addNewRect(row, column, color, None) | ||
font = pygame.font.Font('freesansbold.ttf', 32) | ||
text = font.render(str(number), True, BLACK, ) | ||
textRect = text.get_rect() # get_rect() -> Returns a new rectangle covering the entire surface. | ||
textRect.center = ((MARGIN + WIDTH) * column + MARGIN + WIDTH / 2, (MARGIN + HEIGHT) * row + MARGIN + WIDTH / 2) | ||
screen.blit(text, textRect) | ||
drawTheBorder() | ||
def finish(): | ||
if solvedBoard == Board: | ||
print("good") | ||
else: | ||
print("not good") | ||
def addNewRect(row, col, color, width): | ||
rectSize = pygame.Rect((MARGIN + WIDTH) * col + MARGIN, (MARGIN + HEIGHT) * row + MARGIN, WIDTH, | ||
HEIGHT) | ||
if width is not None: | ||
pygame.draw.rect(screen, color, rectSize, width) # coloring only the border | ||
else: | ||
pygame.draw.rect(screen, color, rectSize) # coloring the whole rectangle | ||
def flickering(timeFlickering, color): # flickering with color on-off | ||
addNewRect(row, column, color, 5) | ||
pygame.display.flip() | ||
time.sleep(timeFlickering) | ||
addNewRect(row, column, WHITE, 5) | ||
pygame.display.flip() | ||
time.sleep(timeFlickering) | ||
addNewRect(row, column, color, 5) | ||
pygame.display.flip() | ||
time.sleep(timeFlickering) | ||
addNewRect(row, column, WHITE, 5) | ||
pygame.display.flip() | ||
def drawTheBorder(): | ||
dif = 500 // 9 | ||
for i in range(10): | ||
thick = 5 | ||
pygame.draw.line(screen, GRAY, (0, i * dif + 2), (500, i * dif + 2), thick) | ||
pygame.draw.line(screen, GRAY, (i * dif + 2, 0), (i * dif + 2, 500), thick) | ||
for i in range(10): | ||
if i % 3 == 0: | ||
thick = 8 | ||
pygame.draw.line(screen, BLACK, (0, i * dif), (500, i * dif), thick) | ||
pygame.draw.line(screen, BLACK, (i * dif, 0), (i * dif, 500), thick) | ||
def drawInitBoard(): | ||
# printBoard(solvedBoard) | ||
for row in range(len(Board)): | ||
for column in range(len(Board[row])): | ||
color = L_GRAY | ||
if Board[row][column] == 0: # if we want to change to background of the empty cells | ||
color = WHITE | ||
# ----- drawing the rect ------ | ||
pygame.draw.rect(screen, color, | ||
[(MARGIN + WIDTH) * column + MARGIN, (MARGIN + HEIGHT) * row + MARGIN, WIDTH, HEIGHT]) | ||
# show nothing if the number is 0 | ||
font = pygame.font.Font('freesansbold.ttf', 32) | ||
if Board[row][column] == 0: | ||
text = font.render(" ", True, BLACK, ) # render(text, anti-alias[True], color, background=None) | ||
else: | ||
text = font.render(str(Board[row][column]), True, BLACK, ) | ||
textRect = text.get_rect() # get_rect() -> Returns a new rectangle covering the entire surface. | ||
textRect.center = ( | ||
(MARGIN + WIDTH) * column + MARGIN + WIDTH / 2, (MARGIN + HEIGHT) * row + MARGIN + WIDTH / 2) | ||
screen.blit(text, textRect) | ||
drawTheBorder() | ||
# -------- Main Program Loop ----------- | ||
if __name__ == "__main__": | ||
flag1 = True | ||
while flag1: | ||
level = chooseLevel() | ||
if level == 1 or level == 2 or level == 3: | ||
print(level) | ||
flag1 = False | ||
pygame.display.set_caption("Sudoku King1") | ||
screen = pygame.display.set_mode(size) | ||
sol = mainSolver(level) # first at all the script solve the sudoku by itself | ||
print("solveBoard") | ||
printBoard(sol) | ||
# ------ draw the board ------ | ||
pygame.init() | ||
screen.fill(BLACK) | ||
drawInitBoard() | ||
readyForInput = False | ||
key = None | ||
while not done: | ||
# --- Main event loop | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
done = True | ||
if event.type == pygame.KEYDOWN: | ||
if event.key in numbers_1to9: | ||
key = chr(event.key) | ||
if event.key == pygame.K_RETURN: | ||
finish() | ||
if event.key == pygame.K_c: | ||
cheatingAllTheWay() | ||
if event.type == pygame.MOUSEBUTTONDOWN: | ||
# ------ if clicked on a cell get his row and column ------ | ||
if readyForInput is True: | ||
addNewRect(row, column, WHITE, None) | ||
drawTheBorder() | ||
readyForInput = False | ||
pos = pygame.mouse.get_pos() | ||
column = pos[0] // (WIDTH + MARGIN) | ||
row = pos[1] // (WIDTH + MARGIN) | ||
# ------ checking if it is a empty (0 inside) ------ | ||
if Board[row][column] == 0: | ||
# ------ coloring the border of the clicked cell ----- #TODO YELLOW | ||
addNewRect(row, column, YELLOW, 5) | ||
readyForInput = True | ||
# ------ now only wait for input from the user ----- | ||
if readyForInput and key is not None: | ||
# ------ checking if the key is good at it's place ------ | ||
if int(key) == sol[row][column]: | ||
Board[row][column] = key | ||
flickering(0.1, GREEN) # flickering at a 0.2 seconds with the color green | ||
addNumToBoard(key, row, column, L_GREEN) | ||
else: | ||
flickering(0.1, RED) # flickering at a 0.2 seconds with the color red | ||
addNumToBoard(key, row, column, L_RED) | ||
# ----------------------------------------------- | ||
drawTheBorder() | ||
readyForInput = False | ||
key = None | ||
pygame.display.flip() | ||
pygame.display.update() | ||
# Close the window and quit. | ||
pygame.quit() |
Binary file addedimages/choosing difficulty level.PNG
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedimages/gaming.PNG
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file addedimages/playing the game.PNG
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.