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]) {
printBoard(grid);
}
#Recursive/backtracking algorithm to solve sudoku puzzle if possible.
#By Nathan Rossol, Computer Science sophomore at the University of Michigan
grid = ([[1,2,3,0,5,6,7,8,9],
[0,5,6,7,8,9,1,2,3],
[7,8,0,1,2,3,4,5,6],
[2,1,4,3,6,5,0,9,7],
[3,0,5,8,9,7,2,1,4],
[8,9,7,2,1,4,3,6,0],
[5,3,1,6,4,2,9,0,8],
[6,4,2,9,0,8,5,3,1],
[9,7,8,0,3,1,6,4,2]
])
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] ]
#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,
#column, or box.
solve(grid)
def solveSudoku(grid):
zeroIndex = (0,0)
zeroIndex = [0,0]
foundZero = findZero(grid, zeroIndex)
#print(foundZero),
#print(zeroIndex[0]),
#print(zeroIndex[1])
solved = False
if foundZero:
for i in range(1,9):
for i in range(1,10):
grid[zeroIndex[0]][zeroIndex[1]] = i
if(checkSolution(grid,zeroIndex[0],zeroIndex[1])):
solved = solveSudoku(grid)
......@@ -38,46 +37,54 @@ def solveSudoku(grid):
def checkBox(grid, row, col):
numbers = []
for i in range(row,row+2):
for j in range(col,col+2):
if grid[i][j] not in numbers: numbers.insert(grid[i][j])
else: return False
for i in range(row,row+3):
for j in range(col,col+3):
if grid[i][j] not in numbers and grid[i][j] != 0:
numbers.insert(0, grid[i][j])
elif grid[i][j] in numbers and grid[i][j] != 0:
#print("CheckBox FAILED")
return False
return True
def checkSolution(grid, row, col):
#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:
#print("ROW ALREADY CONTAINS")
return False
elif grid[row][i] == grid[row][col] and i != col:
#print("COL ALREADY CONTAINS")
return False
#check boxes:
for i in range(0,8,3):
for j in range(0,8,3):
for i in range(0,9,3):
for j in range(0,9,3):
#print(i),
#print(j)
if not checkBox(grid,i,j):
return False
#print("PASSED")
return True
def findZero(grid, zeroIndex):
found = False
for r in grid:
for c in r:
for r in range(len(grid)):
for c in range(len(grid[r])):
if grid[r][c] == 0:
zeroIndex[0] = r
zeroIndex[1] = c
found = True
return True
return found
return False
def printBoard(grid):
for i in range(0,8):
for j in range(0,8):
print(grid[i][j], end=' ')
print()
for i in range(0,9):
for j in range(0,9):
print(grid[i][j], end=' '),
print("")
def solve(grid):
......@@ -86,4 +93,6 @@ def solve(grid):
else:
print("Unsolvable :(")
printBoard(grid)
\ No newline at end of file
printBoard(grid)
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