from random import randint

"""def player(start_board):
    player_choice = input("Pick a place: ")
    if start_board.count(player_choice) == 1:
        start_board[start_board.index(player_choice)] = "X"
    else:
        player(start_board)"""


def player(start_board):
    com_choice = str(randint(1, 9))
    if start_board.count(com_choice) == 1:
        start_board[start_board.index(com_choice)] = "X"
    else:
        player(start_board)


def com(start_board):
    com_choice = str(randint(1, 9))
    if start_board.count(com_choice) == 1:
        start_board[start_board.index(com_choice)] = "O"
    else:
        com(start_board)


def print_board(s_board):
    board = "".join(s_board)
    print(board)


def check(s_b):
    if s_b[0] == s_b[2] == s_b[4] == 'X' or s_b[6] == s_b[8] == s_b[10] == 'X' or s_b[12] == s_b[14] == s_b[16] == 'X' \
            or s_b[0] == s_b[6] == s_b[12] == 'X' or s_b[2] == s_b[8] == s_b[14] == 'X' or s_b[4] == s_b[10] == s_b[16] \
            == 'X' or s_b[0] == s_b[8] == s_b[16] == 'X' or s_b[4] == s_b[8] == s_b[12] == 'X':
        print("you won")
        main()
    elif s_b[0] == s_b[2] == s_b[4] == 'O' or s_b[6] == s_b[8] == s_b[10] == 'O' or s_b[12] == s_b[14] == s_b[16] == 'O' \
            or s_b[0] == s_b[6] == s_b[12] == 'O' or s_b[2] == s_b[8] == s_b[14] == 'O' or s_b[4] == s_b[10] == s_b[16] \
            == 'O' or s_b[0] == s_b[8] == s_b[16] == 'O' or s_b[4] == s_b[8] == s_b[12] == 'O':
        print('computer won')
        print_board(s_b)
        main()


def main():
    # main program
    start_board = ["1", "|", "2", "|", "3", "\n",
                   "4", "|", "5", "|", "6", "\n",
                   "7", "|", "8", "|", "9", "\n", ]
    print_board(start_board)
    while 1:
        try:
            player(start_board)
            check(start_board)

            com(start_board)
            check(start_board)

            print_board(start_board)
        except RecursionError:
            print_board(start_board)
            print("draw")
            main()


if __name__ == "__main__":
    main()
