예제 #1
0
def maximin(board, depth, alpha, beta, bestaction, player, enemy):
    initial_eval = evaluate.evaluate(board)
    if ((initial_eval == -9999999) or (initial_eval == 9999999) or (initial_eval == -0.005) or (depth == 0)):
        return initial_eval
    else:
        hashes_seen = set()
        val = evaluate.MAXVAL
        for x in range(0, 6):
            for y in range(0, 6):
                if (isAvailable(board, x, y)):
                    for box in range(1, 5):
                        for direction in ["R", "L"]:
                            a = GamePlay.Action(x, y, box, direction)
                            temp = deepcopy(board)
                            new_board = GamePlay.take_action(temp, a, enemy)
                            if isUnique(symmetricBoards(new_board), hashes_seen):
                                hashes_seen.add(hash(str(new_board)))
                                val2 = minimax(
                                    new_board, depth - 1, alpha, beta, bestaction, False, player, enemy)
                                if (val2 < val):
                                    val = val2
                                beta = min(beta, val)
                                if alpha >= beta:
                                    return val
        return val
예제 #2
0
def generate_quick_best(board, player):
    actions = []
    hashes_seen = set()
    for x in range(0, 6):
        for y in range(0, 6):
            if isAvailable(board, x, y):
                for box in range(1, 5):
                    for direction in ["R", "L"]:
                        a = GamePlay.Action(x, y, box, direction)
                        temp = deepcopy(board)
                        new_board = GamePlay.take_action(temp, a, player)
                        if isUnique(symmetricBoards(new_board), hashes_seen):
                            hashes_seen.add(hash(str(new_board)))
                            initial_eval = evaluate.evaluate(board)
                            actions.append((a, initial_eval))
    actions.sort(key=operator.itemgetter(1), reverse=True)
    return actions
예제 #3
0
def minimax(board, depth, alpha, beta, bestaction, is_top):
    initial_eval = evaluate.evaluate(board)
    if ((initial_eval == -9999999) or (initial_eval == 9999999)
            or (initial_eval == -0.005) or (depth == 0)):
        return initial_eval
    val = evaluate.MINVAL
    if is_top:
        actions = generate_quick_best(board)
        for (a, _) in actions:
            temp = deepcopy(board)
            new_board = GamePlay.take_action(temp, a, "AI")
            val2 = maximin(new_board, depth - 1, alpha, beta, bestaction)
            if (val2 > val):
                val = val2
                bestaction.x_coordinate = a.x_coordinate
                bestaction.y_coordinate = a.y_coordinate
                bestaction.square_index = a.square_index
                bestaction.direction = a.direction
            alpha = max(alpha, val)
            if alpha >= beta:
                return val
        return val
    else:
        hashes_seen = set()
        for x in range(0, 6):
            for y in range(0, 6):
                if isAvailable(board, x, y):
                    for box in range(1, 5):
                        for direction in ["R", "L"]:
                            a = GamePlay.Action(x, y, box, direction)
                            temp = deepcopy(board)
                            new_board = GamePlay.take_action(temp, a, "AI")
                            if isUnique(symmetricBoards(new_board),
                                        hashes_seen):
                                hashes_seen.add(hash(str(new_board)))
                                val2 = maximin(new_board, depth - 1, alpha,
                                               beta, bestaction)
                                if (val2 > val):
                                    val = val2
                                alpha = max(alpha, val)
                                if alpha >= beta:
                                    return val
        return val
예제 #4
0
def new_game():
    board = GamePlay.new_board()
    depth = input("To what depth would you like the AI to search? ")
    while (valid(depth, 4) == False):
        depth = input("invalid depth, please input a non-negative integer ")
    turn = input('Would you like to go first? (y/n) ')
    while turn != 'y' and turn != 'n':
        print(
            'You have entered an invalid input. Type either y or n and then press enter'
        )
        turn = input('Would you like to go first? (y/n) ')
    if turn == 'y':
        GamePlay.printBoard(board)
        turn = 0
    if turn == 'n':
        turn = 1

    game_over = False
    while not game_over:
        if turn == 0:
            x = input(
                "Please input the x coordinate where you would like to place your marble "
            )
            while (valid(x, 1) == False):
                x = input(
                    "Invalid input, please select a number between 0 and 5 ")
            y = input("Please input the y coordinate ")
            while (valid(y, 1) == False):
                y = input(
                    "Invalid input, please select a number between 0 and 5 ")
            while not (minimax.isAvailable(board, int(x), int(y))):
                print(
                    "There is already a marble on the location you selected. Please choose another one"
                )
                x = input(
                    "Please input the x coordinate where you would like to place your marble "
                )
                while (valid(x, 1) == False):
                    x = input(
                        "Invalid input, please select a number between 0 and 5 "
                    )
                y = input("Please input the y coordinate ")
                while (valid(y, 1) == False):
                    y = input(
                        "Invalid input, please select a number between 0 and 5 "
                    )

            s = input(
                "Please input the index of the square you would like to rotate "
            )
            while (valid(s, 2) == False):
                s = input(
                    "Invalid input, please enter an integer between 1 and 4 ")

            d = input(
                "Please input the direction you would like to rotate the sqaure "
            )
            while (valid(d, 3) == False):
                d = input("Invalid input, please enter R or L ")

            action = GamePlay.Action(int(x), int(y), int(s), d)
            board = GamePlay.take_action(board, action, "Player")
            print("The new board is:")
            GamePlay.printBoard(board)
            turn = 1
        else:

            ##minimax.minimax(board, 3, action)

            action = minimax.getBestAction(board, int(depth))
            board = GamePlay.take_action(board, action, "AI")
            print("The AI has taken action: x=" + str(action.x_coordinate) +
                  ", y=" + str(action.y_coordinate) + ", box=" +
                  str(action.square_index) + ", direction=" +
                  str(action.direction))
            print("The new board is: ")
            GamePlay.printBoard(board)
            turn = 0

        if evaluate.evaluate(board) in [9999999, -9999999, -.005]:
            final_score = evaluate.evaluate(board)
            game_over = True
        board_full = True
        for x in range(6):
            for y in range(6):
                if board[x][y] == " ":
                    board_full = False
        if board_full:
            game_over = True
            final_score = -.006

    if final_score == 9999999:
        print("Game over, the AI has won")
    if final_score == -9999999:
        print("Congratulations! You won!")
    if final_score == -.005:
        print("Its a tie!")
    if final_score == -.006:
        print("The board is full, its a tie!")
    again = input('Would you like to play again? (y/n) ')
    while again != 'y' and again != 'n':
        print(
            'You have entered an invalid input. Type either y or n and then press enter'
        )
        again = input('Would you like to play again? (y/n) ')
    if again == 'y':
        new_game()
예제 #5
0
def findAvailableAction(board):
    for x in range(0, 6):
        for y in range(0, 6):
            if isAvailable(board, x, y):
                return GamePlay.Action(x, y, 1, "L")