def test_incorrect_grid_3(self): input_grid = 70004300053 solver = sudoku_solver(input_grid) self.assertEqual( solver, "invalid grid", "Input Validation: Should return `invalid grid` when the grid is not a list of list" )
def test_valid_grid(self): solved_grid = [ [7, 1, 3, 2, 4, 5, 8, 9, 6], [5, 8, 9, 6, 1, 3, 2, 4, 7], [4, 6, 2, 7, 9, 8, 1, 3, 5], [1, 2, 8, 3, 5, 4, 6, 7, 9], [9, 5, 4, 8, 7, 6, 3, 2, 1], [3, 7, 6, 9, 2, 1, 4, 5, 8], [6, 9, 7, 4, 8, 2, 5, 1, 3], [8, 4, 1, 5, 3, 9, 7, 6, 2], [2, 3, 5, 1, 6, 7, 9, 8, 4] ] input_grid = [ [7,0,0, 0,0,0, 0,0,6], [0,0,0, 6,0,0, 0,4,0], [0,0,2, 0,0,8, 0,0,0], [0,0,8, 0,0,0, 0,0,0], [0,5,0, 8,0,6, 0,0,0], [0,0,0, 0,2,0, 0,0,0], [0,0,0, 0,0,0, 0,1,0], [0,4,0, 5,0,0, 0,0,0], [0,0,5, 0,0,7, 0,0,4] ] solver = sudoku_solver(input_grid) self.assertListEqual(solver, solved_grid)
def __solve(self): # print("we need to figure this out") # self.game.puzzle = sud.sudoku_solver(self.game.puzzle) sol = sud.sudoku_solver(self.game.puzzle) if sol != None: self.game.puzzle = sol self.__draw_puzzle() if self.game.check_win(): self.__draw_victory()
def test_incorrect_grid_2(self): input_grid = [[7, 0, 0, 0, 0, 0, 0, 0, 6], [0, 0, 0, 6, 0, 0, 0, 4, 0], [0, 0, 2, 0, 0, 8, 0, 0, 0], [0, 0, 8, 0, 0, 0, 0, 0, 0], [0, 5, 0, 8, 0, 6, 0, 0, 0], [0, 0, 0, 0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 5, 0, 0, 7, 0, 0, 0]] solver = sudoku_solver(input_grid) self.assertEqual( solver, "invalid grid", "Input Validation: Should return `invalid grid` when grid is missing a column" )
x = xcopy # En nuestra aplicación cada 3 filas se presenta una línea más # gruesa, entonces: ## y += dy + (11 if j % 3 == 0 else 6) mat.append(grid_row) touch_mat.append(t_row) # Resolver e imprimir en pantalla el sudoku si es que este se puede resolver, # caso contrario se imprime en pantalla el mensaje que indica que el sudoku # no es solucionable y cambia el valor de "impr" a 1 para que ninguna acción # sea ralizada en pantalla: print_board(mat) print("_________________________") orig_grid = copy.deepcopy(mat) if sudoku_solver(mat): print("Sudoku Resuelto: ") print_board(mat) else: print("No existe solución") impr = 1 # TO MOD # Función que permite al programa detectar las coordenadas de cada casilla y # y realizar un "click" sobre las mismas: def click(i, j): device.shell(f'input touchscreen tap {touch_mat[i][j][0]} {touch_mat[i][j][1]}') # TO MOD
def run(self): cap = cv2.VideoCapture(self.url) while self._run_flag: if not self.done: if self.src_frame is not None: if self.row < 9: for _ in range(3): img = self.src_frame[36 * self.row + 4:36 * self.row + 32, 36 * self.col + 4:36 * self.col + 32] img = cv2.resize(img, (28, 28)) image = img.reshape(1, 28, 28, 1) if image.sum() > 15000: val, prob = dc.classify_image(image) save_img = cv2.resize(img, (52, 52)) self.matrix[self.row][self.col] = val self.pred[self.row][self.col] = [val, prob] cv2.imwrite( './cells/' + str(self.row) + str(self.col) + '.jpg', cv2.bitwise_not(save_img)) self.col += 1 if self.col == 9: self.col = 0 self.row += 1 self.process_percent += 3.35 if self.process_percent < 100: self.send_process_percent.emit( self.process_percent) else: res = sol.sudoku_solver(self.matrix) self.process_percent = 1000 self.sol_list = res self.done = True if len(self.sol_list) > 0: self.sol = self.sol_list[globals()['index']] self.send_process_percent.emit(self.process_percent) self.send_solution.emit(self.sol_list, self.pred) ret, cv_img = cap.read() if ret: gray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (7, 7), 0) thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 4) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) max_area = 0 contour_grille = None for c in contours: area = cv2.contourArea(c) if area > 25000: peri = cv2.arcLength(c, True) polygone = cv2.approxPolyDP(c, 0.05 * peri, True) if area > max_area and len(polygone) == 4: contour_grille = polygone max_area = area if contour_grille is not None: if self.con_start is None: self.con_start = time.time() if self.done: self.discon_start = None if time.time() - self.con_start >= 2 or self.done: if self.src_frame is None: self.src_frame = ip.getSudoku( thresh, contour_grille) cv2.imwrite('cc.jpg', self.src_frame) if not (self.currentIndex == globals()['index']): self.currentIndex = globals()['index'] if (self.currentIndex < len(self.sol_list)): self.sol = self.sol_list[self.currentIndex] final = ip.img_proc(cv_img, contour_grille, self.sol) self.change_pixmap_signal.emit(final) continue else: self.con_start = None if self.done: if self.discon_start is None: self.discon_start = time.time() if time.time() - self.discon_start >= 3: globals()['index'] = 0 self.row = 0 self.col = 0 self.done = False self.sol_list = [] self.src_frame = None self.discon_start = None self.process_percent = 0 self.sol = [[0 for _ in range(9)] for _ in range(9)] self.matrix = [[0 for _ in range(9)] for _ in range(9)] self.pred = [[[0, 0] for i in range(9)] for j in range(9)] self.change_pixmap_signal.emit(cv_img) cap.release()