Commit 6dfe86fe authored by nrossol's avatar nrossol
Browse files

Began implimenting gui.py

parents 306b5bd5 7294059e
{
"files.associations": {
"ostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"optional": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
},
"python.pythonPath": "/usr/bin/python3"
}
\ No newline at end of file
#GUI for sudoku solver by Nathan Rossol, Computer Science sophomore at the University of Michigan
from sudokuSolverPython import solveSudoku
import pygame
import os
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] ]
#Basic Colors:
BLACK = (0,0,0)
WHITE = (255,255,255)
#Initialize Pygame:
#os.environ["SDL_VIDEODRIVER"] = "dummy"
os.environ['SDL_VIDEODRIVER'] = 'windib'
pygame.display.init()
screen_width = 500
screen_height = 500
margin = 5
screen = pygame.display.set_mode([screen_width,screen_height])
pygame.display.set_caption("Main")
clock = pygame.time.Clock()
#Main Loop:
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
\ No newline at end of file
...@@ -169,3 +169,4 @@ void solve(int (&grid)[9][9]) { ...@@ -169,3 +169,4 @@ void solve(int (&grid)[9][9]) {
printBoard(grid); printBoard(grid);
} }
#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 ],
grid = ([[1,2,3,0,5,6,7,8,9], [7, 8, 0, 0, 1, 0, 0, 6, 5],
[0,5,6,7,8,9,1,2,3], [0, 0, 6, 7, 0, 4, 1, 0, 0],
[7,8,0,1,2,3,4,5,6], [5, 4, 9, 0, 0, 0, 7, 8 ,6],
[2,1,4,3,6,5,0,9,7], [0, 0, 0, 0, 0, 0, 0, 0, 0],
[3,0,5,8,9,7,2,1,4], [0, 0, 0, 9, 0, 6, 0, 0, 0],
[8,9,7,2,1,4,3,6,0], [1, 0, 5, 0, 6, 0, 4, 0, 2],
[5,3,1,6,4,2,9,0,8], [0 ,3 ,0, 2, 0, 7, 0, 5, 0],
[6,4,2,9,0,8,5,3,1], [2, 0, 7, 0, 4, 0, 8, 0, 3] ]
[9,7,8,0,3,1,6,4,2]
])
#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,
#column, or box. #column, or box.
solve(grid)
def solveSudoku(grid): def solveSudoku(grid):
zeroIndex = (0,0) zeroIndex = [0,0]
foundZero = findZero(grid, zeroIndex) foundZero = findZero(grid, zeroIndex)
#print(foundZero),
#print(zeroIndex[0]),
#print(zeroIndex[1])
solved = False solved = False
if foundZero: if foundZero:
for i in range(1,9): for i in range(1,10):
grid[zeroIndex[0]][zeroIndex[1]] = i grid[zeroIndex[0]][zeroIndex[1]] = i
if(checkSolution(grid,zeroIndex[0],zeroIndex[1])): if(checkSolution(grid,zeroIndex[0],zeroIndex[1])):
solved = solveSudoku(grid) solved = solveSudoku(grid)
...@@ -38,46 +37,54 @@ def solveSudoku(grid): ...@@ -38,46 +37,54 @@ def solveSudoku(grid):
def checkBox(grid, row, col): def checkBox(grid, row, col):
numbers = [] numbers = []
for i in range(row,row+2): for i in range(row,row+3):
for j in range(col,col+2): for j in range(col,col+3):
if grid[i][j] not in numbers: numbers.insert(grid[i][j]) if grid[i][j] not in numbers and grid[i][j] != 0:
else: return False numbers.insert(0, grid[i][j])
elif grid[i][j] in numbers and grid[i][j] != 0:
#print("CheckBox FAILED")
return False
return True return True
def checkSolution(grid, row, col): def checkSolution(grid, row, col):
#check cols and rows: #check cols and rows:
for i in range(0,8): for i in range(0,9):
if grid[i][col] == grid[row][col] and i != row: if grid[i][col] == grid[row][col] and i != row:
#print("ROW ALREADY CONTAINS")
return False return False
elif grid[row][i] == grid[row][col] and i != col: elif grid[row][i] == grid[row][col] and i != col:
#print("COL ALREADY CONTAINS")
return False return False
#check boxes: #check boxes:
for i in range(0,8,3): for i in range(0,9,3):
for j in range(0,8,3): for j in range(0,9,3):
#print(i),
#print(j)
if not checkBox(grid,i,j): if not checkBox(grid,i,j):
return False return False
#print("PASSED")
return True return True
def findZero(grid, zeroIndex): def findZero(grid, zeroIndex):
found = False for r in range(len(grid)):
for r in grid: for c in range(len(grid[r])):
for c in r:
if grid[r][c] == 0: if grid[r][c] == 0:
zeroIndex[0] = r zeroIndex[0] = r
zeroIndex[1] = c zeroIndex[1] = c
found = True return True
return found return False
def printBoard(grid): def printBoard(grid):
for i in range(0,8): for i in range(0,9):
for j in range(0,8): for j in range(0,9):
print(grid[i][j], end=' ') print(grid[i][j], end=' '),
print() print("")
def solve(grid): def solve(grid):
...@@ -86,4 +93,6 @@ def solve(grid): ...@@ -86,4 +93,6 @@ def solve(grid):
else: else:
print("Unsolvable :(") print("Unsolvable :(")
printBoard(grid) printBoard(grid)
\ No newline at end of file
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