sd

fvf

from tkinter import *               # Графическая библиотека
column = 3                          # Столбцы
row = 3                             # Строки

class Btn(Button):                  # Класс Btn
    btn_all = []                    # Все экземпляры типа Btn
    num = 0                         # Номер клетки
    playground = [0] * (column*row) # Виртуальное игровое поле
    game_over = False
    def __init__(self, *args, **kwards):
        super().__init__(*args, **kwards, command=self.play)
        self.pack(expand=YES, fill=BOTH, side=LEFT)
        self.num = Btn.num
        Btn.num += 1
        Btn.btn_all.append(self)
        
    def play(self):                 # Игра
        if Btn.game_over: return
        if not (self.cget('text')) == '': return
        self.config(text="X")
        Btn.playground[self.num] = 1
        Btn.game_over = self.calculate()
        if Btn.game_over: return
        Btn.set_0(self.best())      # Ход ноликов

    def best(self):                 # Поиск лучшего хода
        if Btn.playground[4] == 0:  # В начале, лучший ход в центр
            return 4
        return 0
        
    def set_0(n):                   # Поставить на клетку с номером n 'O'
        Btn.btn_all[n].config(text="0")
        Btn.playground[n] = -1

    def calculate(self):            # Оценка ситуации в игре
        cash_book = [0] *8          # Суммы по строкам, столбцам и диагоналям
        pg = Btn.playground
        cash_book[0] = pg[0] + pg[1] + pg[2]
        cash_book[1] = pg[3] + pg[4] + pg[5]
        cash_book[2] = pg[6] + pg[7] + pg[8]
        cash_book[3] = pg[0] + pg[3] + pg[6]
        cash_book[4] = pg[1] + pg[4] + pg[7]
        cash_book[5] = pg[2] + pg[5] + pg[8]
        cash_book[6] = pg[0] + pg[4] + pg[8]
        cash_book[7] = pg[2] + pg[4] + pg[6]
        if 3 in cash_book:
            tk.title('Win X')
            return True
        if -3 in cash_book:
            tk.title('Win 0')
            return True
        if 0 not in pg:
            ('I offer You draw')
            return True
        return False

tk = Tk()
tk.title('tic-tac-toe')

for i in range(row):
    f = Frame()                     # Фреймы (строки)
    f.pack(expand=YES, fill=BOTH)
    for j in range(column):         # Колонки
        Btn(f, width=3, height=2, font=('times', 24, 'bold'))


mainloop()                          # Главный цикл программы

Лист. 1.