Я не знаю, как правильно настроить повороты. Основываясь на моем коде, движение компьютера должно быть вызвано щелчком игрока на доске. Я сделал это хорошо, человек против человека. Следующий код слишком урезан, но я думаю, что мы можем понять общую идею.
from tkinter import *
import random
root = Tk()
root.title("Tic Tac Toe")
root.resizable(0, 0)
board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
def lucky_guess():
#The computer moves randomly
def check_winner():
if board[0] == 'X' and board[1] == 'X' and board[2] == 'X':
print("Player's won. Game over.")
# sys.exit()
# All successful combinations
def set_movement(square, n):
global turn
if board[n] == " " and turn == 'X':
square["text"] = "X"
board[n] = "X"
check_winner()
messages.config(text="Computer's turn")
turn = 'O'
elif turn == 'O':
lucky_guess()
check_winner()
messages.config(text="Player's turn")
turn = 'X'
print(board)
# 9 tiles
tile_6 = Button(root, text="6", bg='gray', fg='white',
height=4, width=8,command=lambda: set_movement(tile_6, 6))
tile_6.grid(row=0, column=0)
#Set turns
choice = random.choice('XO')
beginning = choice, "'s turn."
messages.config(text=beginning)
turn = choice
root.mainloop()