Exemple #1
0
def run_ai_mode():
    """ Run Connect 3 against AI on a 3x3 board """
    player = player_piece()

    rows = 3
    cols = 3

    game = Connect3Board(rows, cols)
    print(game)

    game_tree = GameTree(game)
    position = game_tree.get_root_position()

    while game.get_winner() is None:
        if game.get_whose_turn() == player:
            move = get_int(
                "Your turn. Choose column (0 to {}): ".format(cols - 1), 0,
                cols - 1)

            if game.can_add_token_to_column(move) is True:
                game.add_token(move)
                position = position.get_child(move)
                print(game)
            else:
                print("ERROR: Invalid move, please try again")
        else:
            children_scores = position.get_children_scores()
            child_index = None
            max_score = -2
            min_score = 2

            for i, child in enumerate(children_scores):
                if game.get_whose_turn() == game.TOKENS[0]:
                    if child is not None and child > max_score:
                        max_score = child
                        child_index = i
                else:
                    if child is not None and child < min_score:
                        min_score = child
                        child_index = i

            game.add_token(child_index)
            position = position.get_child(child_index)

            print("AI's turn")
            print(game)

    if game.get_winner() == Connect3Board.DRAW:
        print("This game has ended in a draw!")
    else:
        print("Player {} wins!".format(game.get_winner()))
Exemple #2
0
def run_ai_mode():
    board = Connect3Board(3, 3)
    player_token = select_player_token()
    game_tree = GameTree(board)
    print(game_tree.count)
    position_tree = game_tree.get_root_position()

    while board.get_winner() == None:
        if board.get_whose_turn() == player_token:
            print(board)
            column_choice = get_int(
                "Player {}'s turn. Choose column ({} to {}):".format(
                    board.get_whose_turn(), 0,
                    board.get_columns() - 1))
            if board.can_add_token_to_column(column_choice):
                board.add_token(column_choice)
                # find the children from the tree based on the users selection
                position_tree = position_tree.get_child(column_choice)
            else:
                print("You cannot add a token at column {}".format(
                    column_choice))
        else:
            print("AI's turn")
            child_scores = position_tree.get_children_scores()

            # select the best child score from the children
            best_child = 0
            for index, score in enumerate(child_scores):
                # pick the score based on player selecting O token
                if score is not None and player_token != GameTree.MIN_PLAYER and score < GameTree.MAX_WIN_SCORE:
                    best_child = index

                    # pick the best score based on player selecting # token
                elif score is not None and player_token != GameTree.MAX_PLAYER and score > GameTree.MIN_WIN_SCORE:
                    best_child = index

            board.add_token(best_child)
            # navigate to the next child in the tree
            position_tree = position_tree.get_child(best_child)

    print(board)
    # Display the winner if its not a draw
    if board.get_winner() != board.DRAW:
        print("Player {} wins!".format(board.get_winner()))
    else:
        print(board.get_winner())