history.py :  » Game-2D-3D » Python-Sudoku » pythonsudoku-0.13 » pythonsudoku » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » Python Sudoku 
Python Sudoku » pythonsudoku 0.13 » pythonsudoku » history.py
# -*- coding: utf-8 -*-

"""Module for history management.

This exports the class:
  - History -- Board with history management.

Copyright (C) 2005-2008  Xos Otero <xoseotero@users.sourceforge.net>

"""

__all__ = ["History"]


import copy

from board import Board


class History(Board):
    """Board with history management."""
    def __init__(self, cellsize=3, board=None, filename=None):
        """Form a board with history.

        If board is not None, the numbers are loaded from the board.
        If filename is not None, the numbers are loaded from a filename file.
        If board and filename are None a void board is created, with the size
        specified by cellsize.

        Keyword arguments:
        cellsize -- integer of the cell side lenght, default 3 for a 9x9
                    board (cellsize must be > 1)
                 -- or a tuple/list of 2 integers for a H*W grid on W*H grids
        board -- source board
        filename -- the file name (default None)

        """
        Board.__init__(self, cellsize, board, filename)
        self.forget()

    def remember(self):
        """Add the values of the sudoku to history at the end."""
        if  self.history_position < (len(self.history) - 1):
            self.history = self.history[:self.history_position + 1]

        self.history.append(copy.deepcopy(self.numbers))
        self.history_position += 1

    def forget(self):
        """Clear the history."""
        self.history = []
        self.history_position = -1

        self.remember()

    def is_first_position(self):
        """Return if the actual position is at the beginning of the history."""
        return self.history_position == 0

    def is_last_position(self):
        """Return if the actual position is at the end of the history."""
        return self.history_position == (len(self.history) - 1)

    def undo(self):
        """Go one position back."""
        if self.history_position <= 0:
            return False

        self.history_position -= 1
        self.numbers = copy.deepcopy(self.history[self.history_position])

        return True

    def redo(self):
        """Go one position forward."""
        if self.history_position >= (len(self.history) - 1):
            return False

        self.history_position += 1
        self.numbers = copy.deepcopy(self.history[self.history_position])

        return True
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.