Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: b71adcde8f7bf38b6e44559dced500a863c1c1df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# -*- coding: UTF-8 -*-
# Copyright 2007-2008 One Laptop Per Child
# Copyright 2007 Gerard J. Cerchio <www.circlesoft.com>
# Copyright 2008 Andrés Ambrois <andresambrois@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import logging
import sugar.logger

from gettext import gettext as _

import cPickle
import gtk
from sugar.activity.activity import Activity, ActivityToolbox

from gametoolbar import GameToolbar
from gogame import GoGame
import boardwidget
import infopanel
from collaboration import CollaborationWrapper
from gtp import gnugo


logger = logging.getLogger('PlayGo')

DEFAULT_SIZE = 19
DEFAULT_KOMI = 5.5

class PlayGo(Activity):
    def __init__(self, handle):
        # Initialize the parent
        Activity.__init__(self, handle)
        logger.debug('Initiating PlayGo')
        
        self.size = DEFAULT_SIZE
        self.komi = DEFAULT_KOMI
        
        # Set the activity toolbox
        toolbox = ActivityToolbox(self)
        self.set_toolbox(toolbox)
        self.gameToolbar = GameToolbar(self)
        toolbox.add_toolbar(_('Game'), self.gameToolbar)
        self.gameToolbar.connect('game-restart', self.restart_game)
        self.gameToolbar.connect('game-board-size', self.board_size_change)
        self.gameToolbar.connect('ai-activated', self.ai_activated_cb)
        self.gameToolbar.connect('ai-deactivated', self.ai_deactivated_cb)
        self.gameToolbar.show()  
        
        # Initialize the game
        self.game = GoGame(self.size)
        self.CurrentColor = 'B'
        self.PlayerColor = 'B'
        self.pass_count = 0
        self.ai_activated = False
        self.set_up_ui()
        
        if not handle.object_id:
            self.infopanel.show(_('Welcome to PlayGo!'))
        else:
            self.show_score()
        self.lastX = -1
        self.lastY = -1
            
        #Set up collaboration
        self.collaboration = CollaborationWrapper(self, 
                                                  self.buddy_joined, 
                                                  self.buddy_left, 
                                                  self.Play, 
                                                  self.game.undostack, 
                                                  self.bootstrap)
        
        self.connect('shared', self.collaboration._shared_cb)
        if self._shared_activity:
            # We are joining the activity
            self.connect('joined', self.collaboration._joined_cb)
            if self.get_shared():
                # We've already joined
                self.collaboration._joined_cb()

    def set_up_ui(self):        
        self.board = boardwidget.GoBoardWidget(self.game.get_status(), self.size)
        self.board.connect('motion-notify-event', self.board_motion_cb)
        self.board.connect('insert-requested', self.insert_cb)
        
        self.main_view = gtk.VBox()
        
        self.board_aspect = gtk.AspectFrame(None, .5, .5, 1, False)
        self.board_aspect.add(self.board)
        self.main_view.pack_start(self.board_aspect)
        
        self.buttons_box = gtk.HBox()
        self.buttons_alignment = gtk.Alignment(0.5, 1, 0.5, 1)
        #Pass button
        self.pass_button = gtk.Button(_('Pass'))
        self.pass_button.connect("clicked",  self.pass_cb)
        self.buttons_box.pack_start(self.pass_button,  True,  True, 10)
        
        #Undo button
        self.undo_button = gtk.Button(_('Undo'))
        self.undo_button.connect("clicked",  self.undo_cb)
        self.buttons_box.pack_start(self.undo_button, True, True, 10)
        
        self.buttons_alignment.add(self.buttons_box)
        self.main_view.pack_start(self.buttons_alignment, False, padding=10)
        
        self.infopanel = infopanel.InfoPanel()
        self.main_view.pack_start(self.infopanel, False)

        self.set_canvas(self.main_view)
        self.show_all()

    def insert_cb(self, widget, x, y, announce=True, ai_play=False):
        ''' The insert function. It makes the play and manages turn changing
            stone drawing, etc. 
            
        Parameters x and y are the coordinates of the play ((0,0) is top left), 
        widget points to the widget that emitted the signal connected to this
        function, announce is True when we need to announce this play to 
        other people collaborating, and ai_play is True when this is called 
        by the AI, so we know not to ask for an AI play again '''
        
        # Check if it's our turn only if it's a local play (announce is True)
        # Calls by other players will always be out of turn for us. 
        if announce and self.get_currentcolor() != self.get_playercolor():
            logger.debug('Play at %s x %s was out-of-turn!', x, y)
            self.infopanel.show(_('It\'s not your turn!'))
            return False
        # Make the play only if it wasn't a pass move. 
        if x != -1:
            self.pass_count = 0
            error = self.game.illegal(x, y, self.get_currentcolor())
            if error:
                self.infopanel.show(error)
                return False
            # Make the play
            captures = self.game.play((x, y), self.get_currentcolor())
            if self.ai_activated and not ai_play: 
                self.notify_ai(x, y, self.get_currentcolor())
            self.gameToolbar.grey_out_size_change()
            if captures: self.redraw_captures(captures)
            self.show_score()
            self.board.draw_stone(x, y, self.get_currentcolor(), widget)
        # Player passed
        else:
            self.pass_count += 1
        # Announce the local play
        if self.get_shared() and announce:
            self.collaboration.Play(x, y)
        self.change_turn()
        if x == -1:
            self.infopanel.show(_('Opponent passed'))
        # If this is the second consecutive pass, the game ends
        if self.pass_count == 2:
            self.game_end()
            return
        # If we are playing a local game with AI turned off, change the color
        if not self.get_shared() and not self.ai_activated:
            self.change_player_color()
        # Else, if the AI is on, and this wasn't played by it, request a play by it. 
        elif self.ai_activated: 
            self.change_player_color()
            if not ai_play:
                self.play_ai()

    def undo_cb(self, widget, data=None):
        if self.game.undo():
            self.board.queue_draw()
            # If playing against AI undo twice
            if self.ai_activated: 
                self.ai.undo()
                self.game.undo()
                self.ai.undo()
            else:
                self.change_turn()
            if not self.get_shared() and not self.ai_activated:
                self.change_player_color()
            self.show_score()
        
    def pass_cb(self, widget, data=None):
        if self.get_shared(): 
            if self.get_currentcolor() == self.get_playercolor():
                self.pass_count += 1
                self.collaboration.Play(-1, -1)
            else:
                self.infopanel.show(_('It\'s not your turn!'))
                return
        else:
            self.pass_count += 1
            self.change_player_color()
        self.change_turn()
        if self.pass_count == 2:
            self.game_end()
        if self.ai_activated:
            self.ai.pass_move(self.get_currentcolor())
            self.play_ai()

    def write_file(self, file_path):
        logger.debug('Writing file: %s', file_path)
        # Strip the undostack
        undostack = self.game.undostack[:]
        strippedstack = []
        for pos, color, captures in undostack:
            strippedstack.append(pos)
        f = open(file_path, 'w')
        try:
            cPickle.dump(strippedstack, f, cPickle.HIGHEST_PROTOCOL)
        finally:
            f.close()
        self.metadata['our-color'] = self.get_playercolor()
        self.metadata['shared'] = str(self.get_shared())
        self.metadata['size'] = str(self.size)
        self.metadata['ai'] = str(self.ai_activated)
        
    def read_file(self, file_path):
        logger.debug('Reading file: %s', file_path)
        f = open(file_path, 'r')
        try:
            newstack = cPickle.load(f)
        finally:
            f.close()
        if self.get_shared():
            logger.debug('The game we are loading is shared!')
            self.PlayerColor = self.metadata.get('our-color', 'B')
        if self.size != self.metadata.get('size', DEFAULT_SIZE):
            self.board_size_change(None, int(self.metadata.get('size', DEFAULT_SIZE)))
        self.bootstrap(newstack)
        if self.metadata.get('ai', None) == 'True':
            self.gameToolbar._size_combo.set_sensitive(False)
            self.gameToolbar._ai_button.set_active(True) # This triggers the 'toggled' signal too
        
    def board_motion_cb(self, widget, event):
        x, y = self.board.get_mouse_event_xy(event)
        if x == self.lastX and y == self.lastY:
            return
        self.lastX = x
        self.lastY = y
        if not self.game.is_occupied(x, y) and self.game.legal((x, y), self.get_playercolor()):
            self.board.draw_ghost_stone(x, y, self.get_playercolor())
    
    def invert_color(self, color):
        if color == 'B': return 'W'
        return 'B'
    
    def get_currentcolor(self):
        return self.CurrentColor
    
    def change_turn(self):
        # It's the other guy's turn now
        if self.CurrentColor == 'B':
            self.infopanel.show(_('White\'s turn'))
        else:
            self.infopanel.show(_('Black\'s turn'))
        self.CurrentColor = self.invert_color(self.get_currentcolor())

    def get_playercolor(self):
        return self.PlayerColor

    def change_player_color(self):
        self.PlayerColor = self.invert_color(self.get_playercolor())
        
    def set_player_color(self, color):
        self.PlayerColor = color

    def redraw_captures(self, captures):
        for x in captures:
            self.board.redraw_area(x[0], x[1])
            
    def bootstrap(self, plays):
        ''' Take our game to the state it would have if @plays were manually played'''
        logger.debug('Bootstraping...')
        self.board.do_expose_event() # HACK: Looks like read_file is called before the board is exposed
        for pos in plays:
            logger.debug('Playing at %s with color %s', pos, self.get_currentcolor())
            captures = self.game.play((pos[0], pos[1]), self.get_currentcolor())
            if captures: self.redraw_captures(captures)
            self.change_turn()
            self.change_player_color()
        logger.debug('Color after bootstraping is %s', self.get_currentcolor())
        self.show_score()
        self.board.do_expose_event()
        
    def restart_game(self, widget=None):
        logger.debug('Received restart signal!')
        self.CurrentColor = 'B'
        self.PlayerColor = 'B'
        self.pass_count = 0
        self.game.clear()
        self.board.status = self.game.status
        self.board.do_expose_event()
        self.show_score()
        self.board.set_sensitive(True)
        self.buttons_box.set_sensitive(True)
        if self.ai_activated:
            self.ai.clear()
        
    def game_end(self):
        # TODO: Mark captured territories with pretty symbols
        self.board.set_sensitive(False)
        self.buttons_box.set_sensitive(False)
        territories = self.game.get_territories()
        final_score = {'B':(len(territories['B']) - self.game.get_score()['W']), 
                                'W':(len(territories['W']) - self.game.get_score()['B'] + self.komi)}
        if final_score['B'] > final_score['W']:
            winner_string = _('Black wins!')
        elif final_score['W'] > final_score['B']:
            winner_string = _('White wins!')
        else:
            winner_string = _('There was a tie!')
        self.infopanel.show(_('Game ended! %s' % winner_string))
        self.infopanel.show_score(_('Final score: White %(W)d - Black %(B)d' % final_score))
        
        
    def board_size_change(self, widget, size):
        if size == self.size:
            return
        self.size = size
        del self.game
        self.game = GoGame(size)
        self.board_aspect.remove(self.board)
        del self.board
        self.board = boardwidget.GoBoardWidget(self.game.get_status(), int(size))
        self.board_aspect.add(self.board)
        self.board.connect('motion-notify-event', self.board_motion_cb)
        self.board.connect('insert-requested', self.insert_cb)
        self.board.show()
        if self.ai_activated:
            del self.ai
            self.ai = gnugo(boardsize=self.size)
        
    def ai_activated_cb(self, widget=None):
        self.ai_activated = True
        self.ai = gnugo(boardsize=self.size)
        for pos, color, captures in self.game.undostack:
            self.notify_ai(pos[0], pos[1], color)
        self._alert(_('AI'), _('PlayGo AI Activated'))
        
    def ai_deactivated_cb(self, widget):
        self.ai_activated = False
        del self.ai
        self._alert(_('AI'), _('PlayGo AI Deactivated'))
        
    def notify_ai(self, x, y, color):
            logger.debug('Notifying AI of play by %s at %s x %s', color, x, y)
            self.ai.make_play(color, x, y)
            
    def play_ai(self):
        if self.get_currentcolor() == self.get_playercolor():
            x, y = self.ai.get_move(self.get_currentcolor())
            logger.debug('Got play %s x %s from AI', x, y)
            self.insert_cb(None, x, y, ai_play=True)
            #logger.debug('Dumping board: %s', self.ai.dump_board())
        
    def show_score(self):
        self.infopanel.show_score(_("Score is: White %(W)d - Black %(B)d" % self.game.get_score()))
    
    def _alert(self, title, text=None):
        from sugar.graphics.alert import NotifyAlert
        alert = NotifyAlert(timeout=5)
        alert.props.title = title
        alert.props.msg = text
        self.add_alert(alert)
        alert.connect('response', self._alert_cancel_cb)
        alert.show()

    def _alert_cancel_cb(self, alert, response_id):
        self.remove_alert(alert)
    
    # ------- Callbacks for Collaboration -------- #
    def buddy_joined(self, buddy):
        self._alert(_('Buddy joined'), _('%s joined' % buddy.props.nick))
        
    def buddy_left(self, buddy):
        self._alert(_('Buddy left'), _('%s left' % buddy.props.nick))
    
    def Play(self, x, y, sender=None):
        ''' Called when a stone was placed at x,y by sender'''
        # Discard a pass move received in our turn. Do it here for extra security
        if x == -1 and self.get_currentcolor() == self.get_playercolor():
            return
        self.insert_cb(None, x, y, False)