├── README.md └── sudoku_solver.py /README.md: -------------------------------------------------------------------------------- 1 | # Sudoku-Solver-GUI 2 | A Graphical User Interface based Sudoku Solver, that works using advanced AI algorithms 3 | 4 | ![s1](https://user-images.githubusercontent.com/64016811/155738465-f5eb8e91-baec-4763-bb5a-85ecaab4597c.jpg) 5 | 6 | -------------------------------------------------------------------------------- /sudoku_solver.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import requests 3 | 4 | WIDTH = 550 5 | background_color = (251,247,245) 6 | original_grid_element_color = (52, 31, 151) 7 | buffer = 5 8 | 9 | response = requests.get("https://sugoku.herokuapp.com/board?difficulty=easy") 10 | grid = response.json()['board'] 11 | grid_original = [[grid[x][y] for y in range(len(grid[0]))] for x in range(len(grid))] 12 | 13 | 14 | def isEmpty(num): 15 | if num == 0: 16 | return True 17 | return False 18 | 19 | def isValid(position, num): 20 | #Check for Column, row and sub-grid 21 | 22 | #Checking row 23 | for i in range(0, len(grid[0])): 24 | if(grid[position[0]][i] == num): 25 | return False 26 | 27 | #Checking column 28 | for i in range(0, len(grid[0])): 29 | if(grid[i][position[1]] == num): 30 | return False 31 | 32 | #Check sub-grid 33 | x = position[0]//3*3 34 | y = position[1]//3*3 35 | #Gives us the box number 36 | 37 | for i in range(0,3): 38 | for j in range(0,3): 39 | if(grid[x+i][y+j]== num): 40 | return False 41 | return True 42 | 43 | 44 | solved = 0 45 | def sudoku_solver(win): 46 | myfont = pygame.font.SysFont('Comic Sans MS', 35) 47 | for i in range(0,len(grid[0])): 48 | for j in range(0, len(grid[0])): 49 | if(isEmpty(grid[i][j])): 50 | for k in range(1,10): 51 | if isValid((i,j), k): 52 | grid[i][j] = k 53 | pygame.draw.rect(win, background_color, ((j+1)*50 + buffer, (i+1)*50+ buffer,50 -2*buffer , 50 - 2*buffer)) 54 | value = myfont.render(str(k), True, (0,0,0)) 55 | win.blit(value, ((j+1)*50 +15,(i+1)*50)) 56 | pygame.display.update() 57 | pygame.time.delay(25) 58 | 59 | sudoku_solver(win) 60 | 61 | #Exit condition 62 | global solved 63 | if(solved == 1): 64 | return 65 | 66 | #if sudoku_solver returns, there's a mismatch 67 | grid[i][j] = 0 68 | pygame.draw.rect(win, background_color, ((j+1)*50 + buffer, (i+1)*50+ buffer,50 -2*buffer , 50 - 2*buffer)) 69 | pygame.display.update() 70 | #pygame.time.delay(50) 71 | return 72 | solved = 1 73 | 74 | 75 | 76 | def main(): 77 | pygame.init() 78 | win = pygame.display.set_mode((WIDTH, WIDTH)) 79 | pygame.display.set_caption("Sudoku") 80 | win.fill(background_color) 81 | myfont = pygame.font.SysFont('Comic Sans MS', 35) 82 | 83 | for i in range(0,10): 84 | if(i%3 == 0): 85 | pygame.draw.line(win, (0,0,0), (50 + 50*i, 50), (50 + 50*i ,500 ), 4 ) 86 | pygame.draw.line(win, (0,0,0), (50, 50 + 50*i), (500, 50 + 50*i), 4 ) 87 | 88 | pygame.draw.line(win, (0,0,0), (50 + 50*i, 50), (50 + 50*i ,500 ), 2 ) 89 | pygame.draw.line(win, (0,0,0), (50, 50 + 50*i), (500, 50 + 50*i), 2 ) 90 | pygame.display.update() 91 | 92 | for i in range(0, len(grid[0])): 93 | for j in range(0, len(grid[0])): 94 | if(0