Пример #1
0
def perfectPlay():
    # set up 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 ")
    game_over = False
    AI_turn = True

    while not game_over:
        if AI_turn:
            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)

        else:
            ternaryBoard = boardTernaryConversion.boardConvertToTernary(board)

            perfectTernaryBoard = request.takeBoardReturnReponseBoard(
                ternaryBoard)

            board = boardTernaryConversion.ternaryConvertToBoard(
                perfectTernaryBoard)

            print("The perfect pentago bot took action. The new board is: ")

            GamePlay.printBoard(board)

        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

        AI_turn = not AI_turn

    if final_score == 9999999:
        print("Game over, the AI has won")
    if final_score == -9999999:
        print(":( the perfect pentago bot won")
    if final_score == -.005:
        print("Its a tie!")
    if final_score == -.006:
        print("The board is full, its a tie!")
Пример #2
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()