~ubuntu-branches/ubuntu/intrepid/gnome-games/intrepid-updates

« back to all changes in this revision

Viewing changes to glchess/src/lib/scene/human.py

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2007-11-21 13:43:08 UTC
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: package-import@ubuntu.com-20071121134308-3m2zeh0776quevvk
Tags: upstream-2.21.2
ImportĀ upstreamĀ versionĀ 2.21.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
    __startSquare = None
18
18
    
19
19
    __showHints       = True
20
 
    __boardHighlights = None
21
20
    
22
21
    def __init__(self):
23
22
        """Constructor for a scene with human selectable components"""
70
69
        """
71
70
        pass
72
71
    
73
 
    def setBoardHighlight(self, coords):
74
 
        """Called when a human player changes the highlighted squares.
75
 
        
76
 
        'coords' is a list or tuple of co-ordinates to highlight.
77
 
                 The co-ordinates are in LAN format (string).
78
 
                 If None the highlight should be cleared.
79
 
        """
80
 
        pass
81
 
    
82
72
    # Public methods
83
73
    
84
74
    def enableHumanInput(self, inputEnabled):
87
77
        'inputEnabled' is a flag to show if human input is enabled (True) or disabled (False).
88
78
        """
89
79
        if inputEnabled is False:
90
 
            self.__startSquare = None
91
 
            self.setBoardHighlight(None)
 
80
            self.__selectSquare(None)
92
81
        self.__inputEnabled = inputEnabled
 
82
        self.selectSquare(None)
93
83
        
94
 
    def showMoveHints(self, showHints):
95
 
        """
96
 
        """
97
 
        self.__showHints = showHints
98
 
        if showHints:
99
 
            self.setBoardHighlight(self.__boardHighlights)
100
 
        else:
101
 
            if self.__startSquare is not None:
102
 
                self.setBoardHighlight({self.__startSquare: glchess.scene.HIGHLIGHT_SELECTED})
103
 
            else:
104
 
                self.setBoardHighlight(None)
105
 
 
106
84
    def select(self, x, y):
107
85
        """
108
86
        """
120
98
        
121
99
        # Deselect when clicking on piece a second time
122
100
        if self.__startSquare == coord:
123
 
            self.__boardHighlights = None
124
 
            self.setBoardHighlight(None)
125
 
            self.__startSquare = None
 
101
            self.__selectSquare(None)
126
102
            return
127
103
 
128
104
        # If this is a friendly piece then select it
129
105
        if self.squareIsFriendly(coord):
130
 
            self.__startSquare = coord
131
 
            
132
 
            # Highlight the squares that can be moved to
133
 
            self.__boardHighlights = {}
134
 
            for file in '12345678':
135
 
                for rank in 'abcdefgh':
136
 
                    if self.canMove(coord, rank + file):
137
 
                        self.__boardHighlights[rank + file] = glchess.scene.HIGHLIGHT_CAN_MOVE
138
 
            self.__boardHighlights[coord] = glchess.scene.HIGHLIGHT_SELECTED
139
 
            self.showMoveHints(self.__showHints)
 
106
            self.__selectSquare(coord)
140
107
 
141
108
        else:
142
109
            # If we have already selected a start move try
175
142
    
176
143
    # Private methods
177
144
    
 
145
    def __selectSquare(self, coord):
 
146
        if self.__startSquare == coord:
 
147
            return
 
148
        self.__startSquare = coord
 
149
        self.selectSquare(coord)
 
150
    
178
151
    def __move(self, start, end):
179
152
        """Attempt to make a move.
180
153
        
182
155
        """
183
156
        if self.canMove(start, end) is False:
184
157
            return
185
 
        self.__startSquare = None
186
 
        self.__boardHighlights = None
187
 
        self.setBoardHighlight(None)
 
158
        self.__selectSquare(None)
188
159
        self.moveHuman(start, end)