当前位置: 首页>>代码示例>>Python>>正文


Python AI.get_move方法代码示例

本文整理汇总了Python中ai.AI.get_move方法的典型用法代码示例。如果您正苦于以下问题:Python AI.get_move方法的具体用法?Python AI.get_move怎么用?Python AI.get_move使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ai.AI的用法示例。


在下文中一共展示了AI.get_move方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: start_game

# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import get_move [as 别名]
def start_game(game_type):
    game = Game()
    ai = AI()
    
    while (game.game_finished is False):
        
        #clear board so output is not too messy
        os.system('cls' if os.name == "nt" else 'clear')
        
        #print the current board state for the players
        print_board_state(game.board)
        
        #if the game has an AI player get AI to move if it's their turn
        if (game_type == AI_PLAYS_BLUE or game_type == AI_PLAYS_RED):
            
            #if it's the AI's turn call the AI to make a move, else player is prompted for a move
            if (game.current_player_turn == RED_PLAYER and game_type == AI_PLAYS_RED):
                game.player_move(ai.get_move(game))
            elif (game.current_player_turn == BLUE_PLAYER and game_type == AI_PLAYS_BLUE):
                game.player_move(ai.get_move(game))
            else:
                game.player_move(get_user_column_input())
        
        #get player move if no AI player or it's still player turn
        else:
            game.player_move(get_user_column_input())
            
        #if the game is finished print the board state and let the player know who has won
        if (game.game_finished):
            os.system('cls' if os.name == "nt" else 'clear')
            print_board_state(game.board)
            print
            print str(game.winner) + " has won the game!"
开发者ID:zbendef,项目名称:connect_four,代码行数:35,代码来源:run.py

示例2: __init__

# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import get_move [as 别名]
class Game:
    def __init__(self):
        self.browser = Browser()
        self.app = QApplication(sys.argv)
        self.timer = QTimer()
        self.timer.timeout.connect(self.updateEvent)

        self.ai = AI()
        self.last_score = 1

    def run(self, delay):
        self.timer.start(delay)
        self.browser.run()
        return self.app.exec_()

    def updateEvent(self):
        self.timer.stop()

        (score, board) = self.browser.extractData()

        if self.last_score > score:
            self.ai.restart_game()
        self.last_score = score

        self.browser.sendMove(self.ai.get_move(score, board))

        self.timer.start() # restart the event loop
开发者ID:JINGHSU,项目名称:reinforcement-2048,代码行数:29,代码来源:2048.py

示例3: run_game

# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import get_move [as 别名]
def run_game():
    """
    runs the game :D
    """
    game_board = Board()

    player1 = Human("1")
    player2 = AI("2")
    print(player1, "vs", player2)
    # players_turn = 1

    while not game_board.has_winner:
        col = player1.get_move(game_board)
        game_board.add_piece(col, player1.piece)
        col = player2.get_move(game_board)
        game_board.add_piece(col, player2.piece)
        game_board.print_board()
开发者ID:MasterOdin,项目名称:Connect4-AI,代码行数:19,代码来源:main.py

示例4: __init__

# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import get_move [as 别名]

#.........这里部分代码省略.........
        # if enhancements are enabled initialize and setup variables for the GUI display
        if enhancements:
            pygame.init()
            self._screen1 = pygame.display.set_mode(((size * 22) + 1,(size * 11) + 50))
            self._screen1.fill((0,0,0))
            self._font = pygame.font.Font(None, 30)
            self._ship_font = pygame.font.Font(None, 20)
        
    def start(self):
        # continue playing the game as long as someone has not lost
        turn_count = 0
        draw_ships = False
        while(not self._player1.is_defeated() and not self._player2.is_defeated()):
            # if enhancements are enabled generate the images to display for the GUI and display them
            if enhancements:
                for event in pygame.event.get(pygame.QUIT):
                    pygame.quit()
                    sys.exit()
                self._screen1.fill((0,0,0))
                image, imagerect = self._player1.get_board().draw_board(draw_ships)
                imagerect.top += 50
                image1, imagerect1 = self._player2.get_board().draw_board(draw_ships)
                imagerect1.top += 50
                imagerect1.left = imagerect.width + 1
                self._screen1.blit(image, imagerect)
                self._screen1.blit(image1, imagerect1)
                # create the 'scoreboard' for the GUI with the player name highlighted for
                # whoever's turn it currently is
                if turn_count % 2 == 0:
                    self._font.set_bold(True)
                else:
                    self._font.set_bold(False)
                font_image = self._font.render("Player 1", False, (255,255,255))
                self._screen1.blit(font_image, pygame.Rect(0,0,self._font.size("Player 1")[0], self._font.size("Player 1")[1]))
                ship_image = self._ship_font.render("ships left = " + str(self._player1.get_ships_left()), False, (255,255,255))
                self._screen1.blit(ship_image, pygame.Rect(0,30,self._ship_font.size("ships left = " + str(self._player1.get_ships_left()))[0], self._ship_font.size("ships left = " + str(self._player1.get_ships_left()))[1]))
                if turn_count % 2 != 0:
                    self._font.set_bold(True)
                else:
                    self._font.set_bold(False)
                font_image = self._font.render("Player 2", False, (255,255,255))
                self._screen1.blit(font_image, pygame.Rect(self._screen1.get_width() - self._font.size("Player 2")[0],0,self._font.size("Player 2")[0], self._font.size("Player 2")[1]))
                ship_image = self._ship_font.render("ships left = " + str(self._player2.get_ships_left()), False, (255,255,255))
                self._screen1.blit(ship_image, pygame.Rect(self._screen1.get_width() - self._ship_font.size("ships left = " + str(self._player2.get_ships_left()))[0],30,self._ship_font.size("ships left = " + str(self._player2.get_ships_left()))[0], self._ship_font.size("ships left = " + str(self._player2.get_ships_left()))[1]))
                pygame.display.update()
            # get player 1 or player 2 move and check for hit and update board
            if turn_count % 2 == 0:
                if not enhancements:
                    print self._player1.get_board()
                value = self._player2.is_hit(Point(0, self._player1.get_move()))
                if value:
                    self._player1.set_prev_was_hit(True)
                elif self._player1.get_prev_was_hit() and not value:
                    self._player1.next_direction()
            else:
                if not enhancements:
                    print self._player2.get_board()
                value = self._player1.is_hit(Point(0, self._player2.get_move()))
                if value:
                    self._player2.set_prev_was_hit(True)
                elif self._player2.get_prev_was_hit() and not value:
                    self._player2.next_direction()
            # change players turn
            turn_count += 1
        # if enhancements are enabled display the final display after a player has won
        if enhancements:
            self._screen1.fill((0,0,0))
            self._font.set_bold(False)
            font_image = self._font.render("Player 1", False, (255,255,255))
            self._screen1.blit(font_image, pygame.Rect(0,0,self._font.size("Player 1")[0], self._font.size("Player 1")[1]))
            font_image = self._font.render("Player 2", False, (255,255,255))
            self._screen1.blit(font_image, pygame.Rect(self._screen1.get_width() - self._font.size("Player 2")[0],0,self._font.size("Player 2")[0], self._font.size("Player 2")[1]))
        # display who was the winner of the game
        if self._player1.is_defeated():
            if not enhancements:
                print "Player 2 Wins."
                print self._player1.get_board()
            else:
                end_image = self._ship_font.render("Player 2 Wins.", False, (255,255,255))
                self._screen1.blit(end_image, pygame.Rect(0,30, self._ship_font.size("Player 2 Wins.")[0], self._ship_font.size("Player 2 Wins.")[1]))
        elif self._player2.is_defeated():
            if not enhancements:
                print "Player 1 Wins."
                print self._player2.get_board()
            else:
                end_image = self._ship_font.render("Player 1 Wins.", False, (255,255,255))
                self._screen1.blit(end_image, pygame.Rect(0,30, self._ship_font.size("Player 1 Wins.")[0], self._ship_font.size("Player 1 Wins.")[1]))
        # if enhancements are enabled display the actual boards of the players after one has won
        if enhancements:
            image, imagerect = self._player1.get_board().draw_board(draw_ships)
            imagerect.top += 50
            image1, imagerect1 = self._player2.get_board().draw_board(draw_ships)
            imagerect1.top += 50
            imagerect1.left = imagerect.width + 1
            self._screen1.blit(image, imagerect)
            self._screen1.blit(image1, imagerect1)        
            pygame.display.update()
            # wait for a mouse click to allow the users to see the outcome of the game
            while len(pygame.event.get(pygame.MOUSEBUTTONUP)) == 0:
                continue
开发者ID:cprimera,项目名称:projects,代码行数:104,代码来源:game.py

示例5: AbstractBoard

# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import get_move [as 别名]

#.........这里部分代码省略.........

    def remove_man(self, coords):
        coords = tuple(coords)
        if coords not in self.man_coords:
            return None
        self.man_coords.remove(coords)
        return {'remove': [coords]}

    def toggle_man(self, coords):
        coords = tuple(coords)
        if coords in self.man_coords:
            instructions = self.remove_man(coords)
        else:
            instructions = self.add_man(coords)
        self.update_legal_moves()
        return instructions

    def play_man_at(self, coords):
        '''Method for attempting to play a man piece. Adds the man, and
        updates internal move state if necessary.
        '''
        instructions = self.add_man(coords)
        if instructions:
            self.update_legal_moves()
            self.reset_speculation()
        return instructions

    def do_ai_move(self):
        if not self.ai:
            self.initialise_ai()

        self.reset_speculation()

        move_type, coords = self.ai.get_move()
        print('ai wants to move at', coords, move_type)
        if move_type == 'move':
            self.speculative_move_ball_to(coords)
        elif move_type == 'play':
            self.speculative_play_man_at(coords)
#        legal_moves[current_pos] = 

    def update_legal_moves(self):
        moves = get_legal_moves(self.ball_coords, self.man_coords,
                                self.shape)
        self.legal_moves = moves
        return self.legal_moves

    def as_ascii(self, speculative=False, *args):
        '''Returns an ascii representation of the board.'''
        string_elements = []
        if not speculative:
            ball_coords = self.ball_coords
            man_coords = self.man_coords
            legal_moves = self.legal_moves
        else:
            ball_coords = self.speculative_ball_coords
            man_coords = self.speculative_man_coords
            legal_moves = self.speculative_legal_moves
        for y in range(self.shape[1])[::-1]:
            for x in range(self.shape[0]):
                coords = (x, y)
                if (coords[0] == ball_coords[0] and
                        coords[1] == ball_coords[1]):
                    string_elements.append('O')
                elif coords in man_coords:
                    string_elements.append('X')
开发者ID:htoma,项目名称:phutball,代码行数:70,代码来源:abstractboard.py


注:本文中的ai.AI.get_move方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。