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 {}
go_game = GoGame( size ) while go_game.consecutive_pass_count < 2: try: go_game.Print() if go_game.whose_turn == GoBoard.WHITE: whose_turn = 'WHITE' else: whose_turn = 'BLACK' command = input( whose_turn + ': ' ) if command == 'resign': break elif command == 'pass': go_game.PlaceStone( -1, -1 ) elif command == 'analyze': go_game.PrintGroupAnalysis() elif command == 'score': go_game.PrintScoreCalculation() else: match = re.match( r'\(([0-9]+),([0-9]+)\)', command ) if match: i = int( match.group(1) ) j = int( match.group(2) ) go_game.PlaceStone( i, j ) else: raise Exception( 'Failed to parse command: ' + command ) except Exception as ex: print( 'Exception: ' + str(ex) )