def genere_grille(self): """ Génère une nouvelle grille """ if self.chrono_on: self.play_pause() rep = _("Yes") if self.debut: rep = two_button_box(self, _("Confirmation"), _("Do you want to abandon the current puzzle?"), _("Yes"), _("No"), self.im_question) if rep == _("Yes"): self.configure(cursor="watch") self.update() rep2 = _("Retry") while rep2 == _("Retry"): grille = genere_grille() diff = difficulte_grille(grille) nb = grille.nb_cases_remplies() self.configure(cursor="") rep2 = two_button_box(self, _("Information"), _("The generated puzzle contains %(nb)i numbers and its level is %(difficulty)s.") % ({"nb": nb, "difficulty": _(diff.capitalize())}), _("Play"), _("Retry"), image=self.im_info) if rep2 == _("Play"): self.level = diff self.affiche_grille(grille.get_sudoku())
def resolution(self): if self.chrono_on: self.play_pause() rep = two_button_box(self, _("Confirmation"), _("Do you really want to get the solution?"), _("Yes"), _("No"), image=self.im_question) if rep == _("Yes"): self.frame_pause.place_forget() grille = Grille() for i in range(9): for j in range(9): val = self.blocs[i, j].get_val() if val: grille.ajoute_init(i, j, val) self.configure(cursor="watch") self.update() sol = grille.solve() self.configure(cursor="") if type(sol) == np.ndarray: for i in range(9): for j in range(9): val = self.blocs[i, j].get_val() if not val: self.blocs[i, j].edit_chiffre(sol[i, j]) self.blocs[i, j].affiche_solution() self.restart() self.b_restart.configure(state="normal") self.nb_cases_remplies = 81 elif sol[1]: i, j = sol[1] if self.blocs[i, j].get_val(): self.blocs[i, j].affiche_erreur() i, j = 0, 0 while i < 9 and self.blocs[i, j].is_modifiable(): j += 1 if j == 9: i += 1 j = 0 if i < 9: # il y a au moins une case de type "initial" rep = two_button_box(self, _("Error"), _("The grid is wrong. It cannot be solved. Do you want the solution of the initial grid?"), _("Yes"), _("No"), image=self.im_erreur) if rep == _("Yes"): self.resolution_init() else: one_button_box(self, _("Error"), _("The grid is wrong. It cannot be solved."), image=self.im_erreur) else: one_button_box(self, _("Error"), _("Resolution failed."), image=self.im_erreur)
def import_grille(self, fichier=None): """ importe une grille stockée dans un fichier txt sous forme de chiffres séparés par des espaces (0 = case vide) """ if self.chrono_on: self.play_pause() rep = _("Yes") if self.debut: rep = two_button_box(self, _("Confirmation"), _("Do you want to abandon the current puzzle?"), _("Yes"), _("No"), self.im_question) if rep == _("Yes"): if not fichier: fichier = askopenfilename(initialdir=cst.INITIALDIR, defaultextension='.txt', filetypes=[('Text', '*.txt'), ('Tous les fichiers', "*")]) if fichier: try: self.load_grille(fichier) except (ValueError, UnicodeDecodeError): one_button_box(self, _("Error"), _("The file does not have the right format. It should be a .txt file with cell values separated by one space. 0 means empty cell."), image=self.im_erreur) except FileNotFoundError: one_button_box(self, _("Error"), _("The file %(file)r does not exist.") % fichier, image=self.im_erreur) elif self.debut: self.play_pause()
def import_partie(self): """ importe une partie stockée dans un fichier .sudoku """ if self.chrono_on: self.play_pause() rep = _("Yes") if self.debut: rep = two_button_box(self, _("Confirmation"), _("Do you want to abandon the current puzzle?"), _("Yes"), _("No"), self.im_question) if rep == _("Yes"): fichier = askopenfilename(initialdir=cst.INITIALDIR, defaultextension='.sudoku', filetypes=[('Sudoku', '*.sudoku')]) if fichier: try: self.load_sudoku(fichier) except FileNotFoundError: one_button_box(self, _("Error"), _("The file %(file)r does not exist.") % fichier, image=self.im_erreur) except (KeyError, EOFError, UnpicklingError): one_button_box(self, _("Error"), _("This file is not a valid sudoku file."), image=self.im_erreur) elif self.debut: self.play_pause()
def quitter(self): rep = _("Yes") if self.debut: rep = two_button_box(self, _("Confirmation"), _("Do you want to interrupt the current puzzle?"), _("Yes"), _("No"), image=self.im_question) if rep == _("Yes"): if self.debut: self.save(cst.PATH_SAVE) self.destroy()
def grille_vide(self): rep = _("Yes") if self.debut: rep = two_button_box(self, _("Confirmation"), _("Do you want to abandon the current puzzle?"), _("Yes"), _("No"), self.im_question) if rep == _("Yes"): self.nb_cases_remplies = 0 self.restart() self.level = "unknown" self.reset_nbs() for i in range(9): for j in range(9): self.blocs[i, j].set_modifiable(True) self.blocs[i, j].efface_case()
def recommence(self): if self.chrono_on: self.play_pause() rep = _("Yes") if self.debut: rep = two_button_box(self, _("Confirmation"), _("Do you really want to start again?"), _("Yes"), _("No"), self.im_question) if rep == _("Yes"): self.reset_nbs() for i in range(9): for j in range(9): if self.blocs[i, j].is_modifiable(): if self.blocs[i, j].get_val(): self.nb_cases_remplies -= 1 self.blocs[i, j].efface_case() else: self.update_nbs(self.blocs[i, j].get_val(), 1) self.restart() elif self.debut: self.play_pause()