import random def ai_move(): for i in range(3): for j in range(3): if board[i][j] == " ": board[i][j] = "O" if check_winner(): return board[i][j] = " " while True: x, y = random.randint(0, 2), random.randint(0, 2) if board[x][y] == " ": board[x][y] = "O" break