Python Code For Tic-Tac-Toe Game with ai

“””

Dylan Keller

3/27/22

This is a tic-tac-toe game

“””

def play():

    import random

    board = []

    game_runner = 0

    XorO = “a”

    turn = 0

    def win():

        print(str(XorO) + ” wins!”)

    def print_board():

        for row in range (len(board)):

            for col in range (len(board[0])):

                print(board[row][col], end = ” “)

            print()

    for i in range (3):

        board.append([“-“] * 3)

    print_board()

    print(“”)

    while game_runner == 0:

        if turn %2 == 0:

            Entry1 = int(input(“What row… “))

            Entry2 = int(input(“What column… “))

        elif turn %2 != 0:

            b = random.randint(1, 3)

            d = random.randint(1, 3)

            Entry1 = b

            Entry2 = d

        while Entry1 > 3 or Entry2 > 3 or Entry1 <= 0 or Entry2 <= 0:

            print(“Must enter only 1, 2, or 3. Try again! “)

            Entry1 = int(input(“What row… “))

            Entry2 = int(input(“What column… “))

        while board[Entry1 -1][Entry2 -1] != “-“:

            if turn % 2 == 0:

                print(“Hey! There is already a mark there! Try again!”)

                Entry1 = int(input(“What row… “))

                Entry2 = int(input(“What column… “))

            elif turn % 2 != 0:

                b = random.randint(1, 3)

                d = random.randint(1, 3)

                Entry1 = b

                Entry2 = d

        if turn % 2 == 0:

            board[Entry1 -1][Entry2 -1 ] = “X”

            turn += 1

            XorO = “X”

        elif turn % 2 != 0:

            board[Entry1 -1][Entry2 -1 ] = “O”

            turn += 1

            XorO = “O”

        print(“”)

        print(“”)

        print_board()

        print(“”)

        if board[0][0] == XorO and board[1][1] == XorO and board[2][2] == XorO:

            win()

            game_runner += 1

        elif board[0][1] == XorO and board[1][1] == XorO and board[2][1] == XorO:

            win()

            game_runner += 1

        elif board[0][2] == XorO and board[1][1] == XorO and board[2][0] == XorO:

            win()

            game_runner += 1

        elif board[1][0] == XorO and board[1][1] == XorO and board[1][2] == XorO:

            win()

            game_runner += 1

        elif board[0][0] == XorO and board[0][1] == XorO and board[0][2] == XorO:

            win()

            game_runner += 1

        elif board[0][0] == XorO and board[1][0] == XorO and board[2][0] == XorO:

            win()

            game_runner += 1

        elif board[2][2] == XorO and board[2][1] == XorO and board[2][0] == XorO:

            win()

            game_runner += 1

        elif board[2][2] == XorO and board[1][2] == XorO and board[0][2] == XorO:

            win()

            game_runner += 1

        else:

            if turn == 9:

                print(“Nobody wins!”)

                break

play()

print(“”)

print(“”)

print(“Would you like to play again?”)

again = input(“(Y / N)… “)

if again == “Y” or “y”:

    play()