def show_info_cb (self, *args): if not self.gsd.grid: dialog_extras.show_message_dialog(parent = self.w, title = _("Puzzle Information"), label = _("There is no current puzzle.") ) return puzzle = self.gsd.grid.virgin.to_string() diff = self.sudoku_maker.get_difficulty(puzzle) information = _("Calculated difficulty: ") try: information += {'easy': _('Easy'), 'medium': _('Medium'), 'hard': _('Hard'), 'very hard': _('Very Hard')}[diff.value_category()] except KeyError: information += diff.value_category() information += " (%1.2f)" % diff.value information += "\n" information += _("Number of moves instantly fillable by elimination: ") information += str(int(diff.instant_elimination_fillable)) information += "\n" information += _("Number of moves instantly fillable by filling: ") information += str(int(diff.instant_fill_fillable)) information += "\n" information += _("Amount of trial-and-error required to solve: ") information += str(len(diff.guesses)) dialog_extras.show_message_dialog(parent = self.w, title = _("Puzzle Statistics"), label = _("Puzzle Statistics"), sublabel = information)
def you_win_callback(self, grid): if hasattr(self, "dancer"): return self.won = True # increase difficulty for next time. self.gconf["difficulty"] = self.gconf["difficulty"] + 0.1 self.timer.finish_timing() self.sudoku_tracker.finish_game(self) if self.timer.active_time < 60: seconds = int(self.timer.active_time) sublabel = ( ngettext("You completed the puzzle in %d second", "You completed the puzzle in %d seconds", seconds) % seconds ) elif self.timer.active_time < 3600: minutes = int(self.timer.active_time / 60) seconds = int(self.timer.active_time - minutes * 60) minute_string = ngettext("%d minute", "%d minutes", minutes) % minutes second_string = ngettext("%d second", "%d seconds", seconds) % seconds sublabel = _("You completed the puzzle in %(minute)s and %(second)s") % { "minute": minute_string, "second": second_string, } else: hours = int(self.timer.active_time / 3600) minutes = int((self.timer.active_time - hours * 3600) / 60) seconds = int(self.timer.active_time - hours * 3600 - minutes * 60) hour_string = ngettext("%d hour", "%d hours", hours) % hours minute_string = ngettext("%d minute", "%d minutes", minutes) % minutes second_string = ngettext("%d second", "%d seconds", seconds) % seconds sublabel = _("You completed the puzzle in %(hour)s, %(minute)s and %(second)s") % { "hour": hour_string, "minute": minute_string, "second": second_string, } sublabel += "\n" sublabel += ngettext("You got %(n)s hint.", "You got %(n)s hints.", self.gsd.hints) % {"n": self.gsd.hints} sublabel += "\n" if self.gsd.impossible_hints: sublabel += ngettext( "You had %(n)s impossibility pointed out.", "You had %(n)s impossibilities pointed out.", self.gsd.impossible_hints, ) % {"n": self.gsd.impossible_hints} sublabel += "\n" self.start_dancer() dialog_extras.show_message_dialog(_("You win!"), label=_("You win!"), sublabel=sublabel)
def finish_jar (self, jar): self.remove_from_saved_games(jar) try: filename = os.path.join(self.finished_path, self.get_filename(jar['game'])) outfi = file(filename, 'w') pickle.dump(jar, outfi) outfi.close() except (OSError, IOError), e: show_message_dialog( title = _('Unable to mark game as finished.'), label = _('Unable to mark game as finished.'), message_type = Gtk.MessageType.ERROR, sublabel = (_('Unable to save file %(filename)s.') % locals() + '\n' + _('Error %(errno)s: %(error)s') % { 'errno':e.errno, 'error':e.strerror }) )
def save_game (self, ui): jar = jar_game(ui) filename = os.path.join(self.save_path, self.get_filename(jar['game'])) try: outfi = file(filename, 'w') pickle.dump(jar, outfi) outfi.close() except (OSError, IOError), e: show_message_dialog( title = _('Unable to save game.'), label = _('Unable to save game.'), message_type = Gtk.MessageType.ERROR, sublabel = (_('Unable to save file %(filename)s.') % locals() + '\n' + _('Error %(errno)s: %(error)s') % { 'errno':e.errno, 'error':e.strerror }) )
def create_dir_safely (self, path): if not os.path.exists(path): try: os.makedirs(path) except OSError, e: if e.errno == errno.ENOSPC: show_message_dialog( title = _('No Space'), label = _('No space left on disk'), message_type = Gtk.MessageType.ERROR, sublabel = _('Unable to create data folder %(path)s.') % locals() + '\n' + \ _('There is no disk space left!') ) else: show_message_dialog( title = 'Error creating directory', label = 'Error creating directory', sublabel = (_('Unable to create data folder %(path)s.') % locals() + '\n' + _('Error %(errno)s: %(error)s') % { 'errno': e.errno, 'error': e.strerror}) )
'error':e.strerror }) ) try: filename = list_of_finished_games = os.path.join( os.path.join(defaults.DATA_DIR, 'puzzles'), 'finished' ) ofi = open(list_of_finished_games, 'a') ofi.write(jar['game'].split('\n')[0]+'\n') ofi.close() except (OSError, IOError), e: show_message_dialog( title = _('Sudoku unable to mark game as finished.'), label = _('Sudoku unable to mark game as finished.'), message_type = Gtk.MessageType.ERROR, sublabel = (_('Unable to save file %(filename)s.') % locals() + '\n' + _('Error %(errno)s: %(error)s') % { 'errno':e.errno, 'error':e.strerror }) ) def remove_from_saved_games (self, jar): previously_saved_game = os.path.join( self.save_path, self.get_filename(jar['game']) ) if os.path.exists(previously_saved_game): os.remove(os.path.join(previously_saved_game)) def abandon_game (self, ui): jar = jar_game(ui) self.remove_from_saved_games(jar)