def _do_loadFromFile(self): # DONE: Check errors !!! from tkFileDialog import askopenfilename filename = askopenfilename(filetypes=((_("GPE files"), "*.gpe"), (_("TSudoku files"), "*.tsdk"), (_("All Files"), "*"))) if not isinstance(filename, basestring) or filename == "": return # Exit if Cancel or Closed self.StatusBar.set(_("Opening %s, please wait...") % (filename, )) try: bkgrid = Sudoku.Grid() bkgrid.copy_values_from(self._grid) grid = Sudoku.Grid() grid.load_from_file(filename) self._grid.copy_values_from(grid) if bkgrid != grid: self._history_callback("t", bkgrid, grid) except Sudoku.Contradiction: # Dialog guarantees filename exists from tkMessageBox import showinfo showinfo(message=_("Invalid sudoku file")) except Exception, detail: # Any other issue, is a filesystem, parse, etc, etc, etc... error from tkMessageBox import showerror showerror(message=( _("Malformed, unconsistent, or unexistent file.\n%s") % (detail, )))
def _do_loadFromURL(self): print "open url" from tkSimpleDialog import askstring filename = askstring( _("Enter the URL"), _("Enter the URL"), initialvalue="http://sudoku.udl.es/Problems/Easy-4-1.gpe") if not isinstance(filename, basestring): return self.StatusBar.set(_("Opening %s, please wait...") % (filename, )) import urllib2 as urllib from tkMessageBox import showinfo try: f = urllib.urlopen(filename) bkgrid = Sudoku.Grid() bkgrid.copy_values_from(self._grid) grid = Sudoku.Grid() if filename.lower().endswith(".gpe"): ftype = "gpe" else: ftype = "tsdk" grid.load_from_stream(f, ftype) self._grid.copy_values_from(grid) if bkgrid != grid: self._history_callback("t", bkgrid, grid) f.close() except Sudoku.Contradiction: f.close() showinfo(message=_("Invalid sudoku file"), title=_("Invalid sudoku file")) except urllib.HTTPError, detail: showinfo( message=_("The server said:\n%s\nRequesting the %s resource") % (detail, filename), title=_("Remote server error"))
def OnOpen(self, evt): dlg = wx.FileDialog(self, wildcard="".join( (_("GPE files"), " (*.gpe)|*.gpe|", _("TSudoku files"), " (*.tsdk)|*.tsdk|", _("All Files"), "|*")), style=wx.OPEN) if dlg.ShowModal() == wx.ID_OK: filep = dlg.GetDirectory() + "/" + dlg.GetFilename() self.SetStatusText(_("Opening %s, please wait...") % (filep, )) self.app.Yield() try: bkgrid = Sudoku.Grid(self.SudokuGrid._ModelGrid) bkgrid.copy_values_from(self.SudokuGrid._ModelGrid) grid = Sudoku.Grid( ) #TODO we must scan de file and get the Sudoku Type grid.load_from_file(filep) if grid._type != bkgrid._type: self._addSudoku(grid._type) self.SudokuGrid._ModelGrid.copy_values_from(grid) if bkgrid != grid: self._history_callback("t", bkgrid, grid) except Sudoku.Contradiction: wx.MessageBox(_("Invalid sudoku file"), _("Invalid sudoku file"), style=wx.ICON_INFORMATION, parent=self) except Exception, detail: wx.MessageBox( _("Malformed, unconsistent, or unexistent file.\n%s") % (detail, ), _("Invalid sudoku file"), style=wx.ICON_ERROR, parent=self)
def _do_new(self): bkgrid = Sudoku.Grid() bkgrid.copy_values_from(self._grid) void = Sudoku.Grid() if bkgrid != void: self._history_callback("t", bkgrid, void) self._grid.reset()
def OnNew(self, evt): #self.SudokuGrid.Reset() import wxNewSudokuDialog dlg = wxNewSudokuDialog.NewSudokuDialog(self) dlg.ShowModal() if dlg.OkStatus: bkgrid = Sudoku.Grid(self.SudokuGrid._ModelGrid) bkgrid.copy_values_from(self.SudokuGrid._ModelGrid) type = dlg.type.GetValue() sbr = int(dlg.brows.GetValue()) sbc = int(dlg.bcols.GetValue()) c = sbr r = sbc if type == _("Samurai"): su = Sudoku.SamuraiSudoku(r, c, sbr, sbc) else: su = Sudoku.NormalSudoku(r, c, sbr, sbc) self.SetStatusText(_("Creating Sudoku, please wait....")) self.app.Yield() if su != bkgrid._type: self._addSudoku(su) else: self.SudokuGrid.Reset() emptygrid = Sudoku.Grid(su) if bkgrid != emptygrid: self._history_callback("t", bkgrid, emptygrid) self.SetStatusText("") dlg.Destroy()
def remove_test(): grid = Sudoku.Grid(val) grid.remove_elements_in_neighbors() assert (grid.grid[0][0] == "1") for i in range(1, 9): assert (grid.grid[0][i] == "23456789") assert (grid.grid[i][0] == "23456789") assert (grid.grid[1][1] == "23456789") assert (grid.grid[2][1] == "23456789") assert (grid.grid[1][2] == "23456789") assert (grid.grid[2][2] == "23456789") for i in range(1, 9): for j in range(3, 9): assert (grid.grid[i][j] == Sudoku.ALL_NUMS) for i in range(3, 9): assert (grid.grid[i][1] == Sudoku.ALL_NUMS) assert (grid.grid[i][2] == Sudoku.ALL_NUMS) print("remove_elements_in_neighbors test passed")
def neighbors_test(): grid = Sudoku.Grid(val) grid.find_neighbors_of_elem(2, 1) neighbor_list = grid.neighbor_dict[(2, 1)] for i in range(9): if i == 1: continue assert ((2, i) in neighbor_list) for i in range(9): if i == 2: continue assert ((i, 1) in neighbor_list) assert ((0, 0) in neighbor_list) assert ((1, 0) in neighbor_list) assert ((0, 2) in neighbor_list) assert ((1, 2) in neighbor_list) assert ((2, 1) not in neighbor_list) print("find_neighbors_of_elem test passed")
def setGrid(self, grid): """ Sets the working grid """ self.startup_grid = Sudoku.Grid(grid) self.stack = [ deepcopy(self.startup_grid), ] self.solution = None
def OnOpenURL(self, evt): dlg = wx.TextEntryDialog( self, _("Enter the URL"), _("Enter the URL"), defaultValue="http://sudoku.udl.es/Problems/Easy-4-1.gpe") if dlg.ShowModal() == wx.ID_OK: self.SetStatusText( _("Opening %s, please wait...") % (dlg.GetValue(), )) self.app.Yield() import urllib2 as urllib try: f = urllib.urlopen(dlg.GetValue()) bkgrid = Sudoku.Grid(self.SudokuGrid._ModelGrid) bkgrid.copy_values_from(self.SudokuGrid._ModelGrid) grid = Sudoku.Grid( ) #TODO we must scan de file and get the Sudoku Type if dlg.GetValue().lower().endswith(".gpe"): ftype = "gpe" else: ftype = "tsdk" grid.load_from_stream(f, ftype) if grid._type != bkgrid._type: self._addSudoku(grid._type) self.SudokuGrid._ModelGrid.copy_values_from(grid) if bkgrid != grid: self._history_callback("t", bkgrid, grid) f.close() except Sudoku.Contradiction: f.close() wx.MessageBox(_("Invalid sudoku file"), _("Invalid sudoku file"), style=wx.ICON_INFORMATION, parent=self) except urllib.HTTPError, detail: wx.MessageBox( _("The server said:\n%s\nRequesting the %s resource") % (detail, dlg.GetValue()), _("Remote server error"), style=wx.ICON_INFORMATION, parent=self) except (urllib.URLError, ValueError), detail: wx.MessageBox(_("Cannot open %s, reason %s") % (dlg.GetValue(), detail), parent=self)
def init_test(): grid = Sudoku.Grid(val) assert (grid.grid[0][0] == "1") for i in range(1, 9): assert (grid.grid[0][i] == Sudoku.ALL_NUMS) for j in range(9): assert (grid.grid[i][j] == Sudoku.ALL_NUMS) #grid.display() print("__init__ test passed")
def place_test(): grid = Sudoku.Grid(val) grid.remove_elements_in_neighbors() for i in range(1, len(grid.grid[0]) - 1): grid.grid[0][i] = grid.grid[0][i].replace('2', '') grid.place_element_with_no_other_option() assert (grid.grid[0][-1] == '2') print("place_element_with_no_other_option test passed")
def _do_solve(self): self.StatusBar.set(_("Solving Sudoku, please wait....")) solution = self.Solver.waitForSolution() ## try: ## grid = Sudoku.Grid() ## grid.copy_values_from(self._grid) ## solution = Sudoku.solve(grid) ## except Sudoku.Contradiction: ## solution = None if solution is None: from tkMessageBox import showinfo showinfo(message=_("No solution has been found.")) else: bkgrid = Sudoku.Grid() bkgrid.copy_values_from(self._grid) self._grid.copy_values_from(solution) if bkgrid != solution: self._history_callback("t", bkgrid, solution) self.StatusBar.set("")
def OnSolve(self, evt): self.SetStatusText(_("Solving Sudoku, please wait....")) self.app.Yield() ## try: ## grid = Sudoku.Grid(self.SudokuGrid._ModelGrid) ## grid.copy_values_from(self.SudokuGrid._ModelGrid) ## solution = Sudoku.solve(grid) ## except Sudoku.Contradiction: ## solution = None solution = self.Solver.waitForSolution() if solution is None: wx.MessageBox(_("No solution has been found."), style=wx.ICON_INFORMATION, parent=self) else: bkgrid = Sudoku.Grid(self.SudokuGrid._ModelGrid) bkgrid.copy_values_from(self.SudokuGrid._ModelGrid) self.SudokuGrid._ModelGrid.copy_values_from(solution) if bkgrid != solution: self._history_callback("t", bkgrid, solution) self.SetStatusText("")
def fix_tile_in_grid(grid, num, row, col): grid.tiles[row][col].set_to(num) grid.tiles[row][col].fix() pygame.init() pygame.font.init() pygame.display.init() sudoku_width = 360 display_width = 500 display_height = sudoku_width sudoku_grid = Sudoku.Grid(sudoku_width, colors.SUD_YELLOW, 9) sudoku_grid.set_values() fix_tile_in_grid(sudoku_grid, 9, 0, 0) fix_tile_in_grid(sudoku_grid, 4, 1, 7) fix_tile_in_grid(sudoku_grid, 5, 4, 4) fix_tile_in_grid(sudoku_grid, 3, 2, 2) fix_tile_in_grid(sudoku_grid, 4, 7, 0) fix_tile_in_grid(sudoku_grid, 2, 8, 8) win = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption("Sudoku Solver") draw_list = [] draw_list.append(sudoku_grid)