def main(): pygame.init() global DISPLAY, FONT_SIZE, FONT, SELECTED DISPLAY = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) FONT_SIZE = 32 FONT = pygame.font.Font('roboto.ttf', FONT_SIZE) # which cell is currently selected SELECTED = [None, None] pygame.display.set_caption("Sudoku Solver") draw() # main game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # select a cell when mouse clicked on it elif event.type == pygame.MOUSEBUTTONUP: draw() pos = pygame.mouse.get_pos() selectCell(pos[0], pos[1]) elif event.type == pygame.KEYUP: # solve the board when space pressed if event.key == pygame.K_SPACE: sudoku.solver(BOARD) draw() # quite game when escape pressed elif event.key == pygame.K_ESCAPE: sys.exit() # ensure that a cell is selected elif SELECTED != [None, None]: # change the number in cell if a number key pressed keys = [ pygame.K_0, 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 ] num = 0 if event.key in keys: num = pygame.key.name(event.key) x = SELECTED[0] y = SELECTED[1] pos = getIndex(x, y) BOARD[pos[0]][pos[1]] = int(num) draw() SELECTED = [None, None] pygame.display.update()
def play_game(board, command): print board while not board.check_win(): print_dirs() rcv_tuple = get_coords() if rcv_tuple == None: #board.fix_board_state() if sudoku.solver(board): print "Congratulatory message" else: print "This board is unsolveable. Try again!" # unsolveable at current state. elif rcv_tuple == '0': board.undo() print board elif rcv_tuple == '1': # TODO: fix haaaaackyness break else: if not board.make_move(*rcv_tuple): print board.error print board return board # having been changed in fxn
def generate_board(): """ generate a soduku board """ while True: row_1 = np.random.choice(np.arange(1, 10), 9, False) row_2 = np.random.choice(np.arange(1, 10), 9, False) if 0 not in row_1 - row_2: break while True: row_num_1, row_num_2 = np.random.choice(np.arange(0, 9), 2, False) if np.abs(row_num_1 - row_num_2) >= 3: break template = np.zeros((9, 9)).astype(int) template[row_num_1, :] = row_1 template[row_num_2, :] = row_2 solved = solver(template, False) return solved
from eyes import detector import sudoku as s import cv2 import time import numpy as np import matplotlib.pyplot as plt img = cv2.imread("2.jpg") d = detector() a, tru = d.detect(img) p = d.pred(a, tru) sol = s.solver(p.reshape((9, 9)).tolist()) sol = ((1 - tru).reshape(9, 9) * (np.asarray(sol))).tolist() scale = 0.09 fontScale = min(img.shape[1], img.shape[0]) / (25 / scale) err = int((img.shape[0] // 9) * 0.17) for j in range(9): for i in range(9): if sol[j][i] != 0: cv2.putText(img, str(sol[j][i]), ((i) * (img.shape[0] // 9) + err, (j + 1) * (img.shape[1] // 9) - err), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (0, 0, 0), 2) plt.imshow(img) plt.show()
"7 - 2 3 - 9 8 - 1", "- - - 8 - - - - -", "- 1 9 - - - 4 7 -"] M = [M[i].split() for i in range(9)] Mats.append(M) display_width = 359 display_height = 400 display = pygame.display.set_mode((display_width,display_height)) pygame.display.set_caption('Sudoku') i_m = 0 clock = pygame.time.Clock() run = True B = board("assets") s = sudoku.solver() s2 = sudoku.solver() solve = False reset = False editable = True see_solution = False B.set_from(display,Mats[i_m]) def a_reset(): global reset,editable,solve,B,s,ss B.set_from(display,B.initial_state) s.set_from(B.initial_state) ss = s.solve(is_pausable=True) editable = True solve = False
corners = filtering(coordinates) print(corners) for i in range(len(corners)): cv2.circle(im1, (corners[i][0], corners[i][1]), 2, red, 2) img = transform(im, corners) ## Sagnik Code img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.medianBlur(img, 5) img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,11,2) a, tru = d.detect(img) p = d.pred(a, tru) sol = sudoku.solver(p.reshape((9, 9)).tolist()) sol = ((1 - tru).reshape(9, 9) * (np.asarray(sol))).tolist() scale = 0.09 fontScale = min(img.shape[1], img.shape[0]) / (25 / scale) err = int((img.shape[0] // 9) * 0.17) for j in range(9): for i in range(9): if sol[j][i] != 0: cv2.putText(img, str(sol[j][i]), ((i) * (img.shape[0] // 9) + err, (j + 1) * (img.shape[1] // 9) - err), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (0, 0, 0), 2) d.disp(a) ## Sagnik Code
def solve(self,canvas): if solver(self.board): for i in self.zeros: canvas.create_text(i[2], i[3], text = str(self.board[i[0]][i[1]]), font=("Arial", 18)) else: print("No solution")