示例#1
0
 def take_turn( self, **kwargs ):
     name = kwargs[ 'name' ]
     game_doc = self.game_collection.find_one( { 'name' : name } )
     if not game_doc:
         return { 'error' : 'A game by the name "%s" was not found.' % name }
     color = kwargs[ 'color' ]
     if color == 'white':
         color = GoBoard.WHITE
     elif color == 'black':
         color = GoBoard.BLACK
     else:
         return { 'error' : 'Bogus color given.' }
     row = int( kwargs[ 'row' ] )
     col = int( kwargs[ 'col' ] )
     go_game = GoGame()
     go_game.Deserialize( game_doc[ 'data' ] )
     if go_game.whose_turn != color:
         return { 'error' : 'It is not yet your turn.' }
     move = None
     if row < 0 or col < 0 or go_game.CurrentBoard().GetState( ( row, col ) ) == GoBoard.EMPTY:
         try:
             go_game.PlaceStone( row, col )
         except Exception as ex:
             return { 'error' : str(ex) }
         move = { 'row' : row, 'col' : col }
         if 'respond' in kwargs and kwargs[ 'respond' ] == 'true':
             move = go_game.CalculateReasonableMove()
             go_game.PlaceStone( move[0], move[1] )
             move = { 'row' : move[0], 'col' : move[1] }
     elif go_game.CurrentBoard().GetState( ( row, col ) ) == color:
         try:
             go_game.RelinquishStone( row, col )
         except Exception as ex:
             return { 'error' : str(ex) }
     data = go_game.Serialize()
     update = { 'data' : data }
     if move:
         update[ 'most_recent_move' ] = move
     result = self.game_collection.update_one( { 'name' : name }, { '$set' : update } )
     if result.modified_count != 1:
         return { 'error' : 'Failed to update game in database.' }
     return {}
示例#2
0
 def game( self, **kwargs ):
     name = kwargs[ 'name' ]
     color = kwargs[ 'color' ]
     game_doc = self.game_collection.find_one( { 'name' : name } )
     if not game_doc:
         return self.MakeErrorPage( 'Failed to find game: %s', name )
     go_game = GoGame()
     go_game.Deserialize( game_doc[ 'data' ] )
     whose_turn = 'white' if go_game.whose_turn == GoBoard.WHITE else 'black'
     color_id = GoBoard.WHITE if color == 'white' else GoBoard.BLACK
     move = { 'row' : -1, 'col' : -1 }
     if 'most_recent_move' in game_doc:
         move = game_doc[ 'most_recent_move' ]
     board = go_game.CurrentBoard()
     group_list = {
         GoBoard.WHITE : board.AnalyzeGroups( GoBoard.WHITE ),
         GoBoard.BLACK : board.AnalyzeGroups( GoBoard.BLACK )
     }
     html_board_table = '<table cellspacing="0" cellpadding="0">\n'
     for i in range( board.size ):
         html_board_table += '<tr>'
         for j in range( board.size ):
             html_board_table += '<td class="cell" style="height: 64px; width: 64px;">\n'
             board_back_image = self.DetermineBoardImage( i, j, board.size )
             state = board.GetState( ( i, j ) )
             if state == GoBoard.EMPTY:
                 html_board_table += '<img src="images/%s" onclick="OnPlaceStoneClicked( \'%s\', \'%s\', %d, %d )">\n' % ( board_back_image, name, color, i, j )
                 if any( [ board.GetState( adj_location ) != GoBoard.EMPTY for adj_location in board.AdjacentLocations( ( i, j ) ) ] ):
                     html_board_table += '<img class="lib_img" id="liberty_%d_%d" src="images/liberty.png" style="visibility:hidden"/>\n' % ( i, j )
             else:
                 if state == GoBoard.WHITE:
                     board_fore_image = 'white_stone.png'
                 elif state == GoBoard.BLACK:
                     board_fore_image = 'black_stone.png'
                 hover_calls = self.FormulateLibertyHoverJSCalls( group_list[ state ], i, j )
                 click_calls = 'onclick="OnGiveUpStoneClicked( \'%s\', \'%s\', %d, %d )"' % ( name, color, i, j ) if state == color_id else ''
                 html_board_table += '<img class="back_img" src="images/%s"/>\n' % board_back_image
                 html_board_table += '<img class="fore_img" src="images/%s" %s %s/>\n' % ( board_fore_image, hover_calls, click_calls )
                 if move[ 'row' ] == i and move[ 'col' ] == j:
                     html_board_table += '<img class="high_img" src="images/highlight.png" %s/>\n' % hover_calls
             html_board_table += '</td>\n'
         html_board_table += '</tr>\n'
     html_board_table += '</table>\n'
     html_message = '<p>It is %s\'s turn.  You are %s.</p>' % ( whose_turn, color )
     html_white_info = self.GenerateInfoForColor( go_game, 'white' )
     html_black_info = self.GenerateInfoForColor( go_game, 'black' )
     scores = go_game.CalculateScores()
     html_score_info = '<center><table border="2">\n'
     html_score_info += '<tr><th></th><th>white</th><th>black</th></tr>\n'
     html_score_info += '<tr><td>score</td><td>%d</td><td>%d</td></tr>\n' % ( scores[ GoBoard.WHITE ][ 'score' ], scores[ GoBoard.BLACK ][ 'score' ] )
     html_score_info += '<tr><td>captures</td><td>%d</td><td>%d</td></tr>\n' % ( scores[ GoBoard.WHITE ][ 'captures' ], scores[ GoBoard.BLACK ][ 'captures' ] )
     html_score_info += '<tr><td>territory</td><td>%d</td><td>%d</td></tr>\n' % ( scores[ GoBoard.WHITE ][ 'territory' ], scores[ GoBoard.BLACK ][ 'territory' ] )
     html_score_info += '</table></center>\n'
     html_pass_button = '<p><center><button type="button" onclick="OnPlaceStoneClicked( \'%s\', \'%s\', -1, -1 )">forfeit turn</button>' % ( name, color )
     return '''
     <html lang="en-US">
         <head>
             <title>Go Game: %s</title>
             <link rel="stylesheet" href="css/go.css">
             <script src="https://code.jquery.com/jquery.js"></script>
             <script src="scripts/go.js"></script>
         </head>
         <body onload="OnPageLoad(%s, '%s', '%s')">
             <div>
                 <p><center>%s</center></p>
                 <p><center>%s</center></p>
                 <!--<center><input type="checkbox" id="respond">Have computer respond.</input></center>-->
                 <center>%s</center>
                 %s
                 <p><center>Click an empty board intersection to place a stone.  Click on your own stone to give it up as a prisoner (at end of game.)</center></p>
             </div>
             <div>
                 %s
                 %s
             </div>
         </body>
     </html>
     ''' % ( name, ('true' if whose_turn == color else 'false'), color, name, html_message, html_score_info, html_board_table, html_pass_button, html_white_info, html_black_info )