Changeset 9535

Show
Ignore:
Timestamp:
04/26/08 04:06:08 (5 months ago)
Author:
bct
Message:

tictactoe: notify user of when won/lost

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/session_centric/src/tictactoe.py

    r9465 r9535  
    1616 
    1717class TicTacToeSession(stanza_session.StanzaSession): 
     18        # initiate a session 
    1819        def begin(self, rows = 3, cols = 3, role_s = 'x'): 
    1920                self.rows = rows 
     
    2122 
    2223                self.role_s = role_s 
     24 
     25                self.strike = 3 
    2326 
    2427                if self.role_s == 'x': 
     
    6972                        self.cols = 3 
    7073 
     74                # number in a row needed to win 
    7175                if form.getField('strike'): 
    7276                        self.strike = int(form.getField('strike').getValues()[0]) 
     
    104108 
    105109                        self.our_turn() 
    106  
    107         def is_my_turn(self): 
    108                 # XXX not great semantics 
    109                 return self.received == self.ignore 
    110110 
    111111        # just sent an invitation, expecting a reply 
     
    120120 
    121121                elif msg.getTag('decline', namespace=games_ns): 
    122                         self.XXX() 
     122                        # XXX notify the user 
     123 
     124                        # XXX end session 
     125                        pass 
    123126 
    124127        # silently ignores any received messages 
     
    126129                pass 
    127130 
     131        def game_over(self, msg): 
     132                invite = msg.getTag('invite', namespace=games_ns) 
     133 
     134                # ignore messages unless they're renewing the game 
     135                if invite and invite.getAttr('type') == 'renew': 
     136                        self.invited(msg) 
     137 
    128138        def wait_for_move(self, msg): 
    129139                turn = msg.getTag('turn', namespace=games_ns) 
     
    136146                if id != self.next_move_id: 
    137147                        print 'unexpected move id, lost a move somewhere?' 
    138                         raise 
     148                        return 
    139149 
    140150                try: 
    141151                        self.board.mark(row, col, self.role_o) 
    142152                except InvalidMove, e: 
    143                         print 'received invalid move' 
     153                        # received an invalid move, end the game. 
     154 
     155                        # XXX notify the user 
     156                        self.terminate('cheating') 
    144157                        return 
    145158 
     
    154167                        self.our_turn() 
    155168 
     169        def is_my_turn(self): 
     170                # XXX not great semantics 
     171                return self.received == self.ignore 
     172 
    156173        def our_turn(self): 
    157174                # ignore messages until we've made our move 
    158175                self.received = self.ignore 
    159                 self.board.win.set_title(self.board.title + ': your turn') 
     176                self.board.set_title('your turn') 
    160177 
    161178        def their_turn(self): 
    162179                self.received = self.wait_for_move 
    163                 self.board.win.set_title(self.board.title + ': their turn') 
     180                self.board.set_title('their turn') 
    164181 
    165182        # called when the board receives input 
     
    174191 
    175192                # check win conditions 
    176                 if self.board.check_for_strike(self.role_s, row, col,self.strike): 
     193                if self.board.check_for_strike(self.role_s, row, col, self.strike): 
    177194                        self.won() 
    178195                elif self.board.full(): 
     
    183200                        self.their_turn() 
    184201 
     202        # sends a move message 
    185203        def send_move(self, row, column): 
    186204                msg = xmpp.Message() 
     
    199217                self.send(msg) 
    200218 
     219        # sends a termination message and ends the game 
     220        def terminate(self, reason): 
     221                msg = xmpp.Message() 
     222 
     223                terminate = msg.NT.terminate 
     224                terminate.setNamespace(games_ns) 
     225                terminate.setAttr('reason', reason) 
     226 
     227                self.send(msg) 
     228 
     229                self.received = self.game_over 
     230 
     231        def won(self): 
     232                self.terminate('won') 
     233                self.board.won() 
     234 
     235        def lost(self): 
     236                self.terminate('lost') 
     237                self.board.lost() 
     238 
     239        def drawn(self): 
     240                self.terminate('draw') 
     241                self.board.drawn() 
     242 
    201243class TicTacToeBoard: 
     244        def __init__(self, session, rows, cols): 
     245                self.session = session 
     246 
     247                self.state = 'None' 
     248 
     249                self.rows = rows 
     250                self.cols = cols 
     251 
     252                self.board = [ [None] * self.cols for r in xrange(self.rows) ] 
     253 
     254                self.setup_window() 
     255 
    202256        def check_for_strike(self, p, r, c, strike): 
    203                 # up and down, left and right 
     257                # number in a row: up and down, left and right 
    204258                tallyI = 0 
    205259                tally_ = 0 
    206260 
    207                 # right triangles: L\ , F/ 
     261                # number in a row: diagonal 
     262                # (imagine L or F as two sides of a right triangle: L\ or F/) 
    208263                tallyL = 0 
    209264                tallyF = 0 
     
    242297                return False 
    243298 
    244         def __init__(self, session, rows, cols): 
    245                 self.session = session 
    246  
    247                 self.rows = rows 
    248                 self.cols = cols 
    249  
    250                 self.board = [ [None] * self.cols for r in xrange(self.rows) ] 
    251  
    252                 self.setup_window() 
    253  
    254299        # is the board full? 
    255300        def full(self): 
     
    264309                self.win = gtk.Window() 
    265310 
    266                 self.title = 'tic-tac-toe with %s' % self.session.jid 
    267  
    268                 self.win.set_title(self.title) 
     311                self.title_prefix = 'tic-tac-toe with %s' % self.session.jid 
     312                self.set_title() 
     313 
    269314                self.win.set_app_paintable(True) 
    270315 
     
    314359                                        self.draw_o(cr, i, j, row_height, col_width) 
    315360 
     361                # XXX draw 'won', 'lost', 'draw' 
     362 
    316363        def draw_x(self, cr, row, col, row_height, col_width): 
    317364                cr.set_source_rgb(0, 0, 0) 
     
    352399 
    353400                self.win.queue_draw() 
     401 
     402        def set_title(self, suffix = None): 
     403                str = self.title_prefix 
     404 
     405                if suffix: 
     406                        str += ': ' + suffix 
     407 
     408                self.win.set_title(str) 
     409 
     410        def won(self): 
     411                self.state = 'won' 
     412                self.set_title('you won!') 
     413                self.win.queue_draw() 
     414 
     415        def lost(self): 
     416                self.state = 'lost' 
     417                self.set_title('you lost.') 
     418                self.win.queue_draw() 
     419 
     420        def drawn(self): 
     421                self.state = 'drawn' 
     422                self.win.set_title(self.title_prefix + ': a draw.') 
     423                self.win.queue_draw()