@dannnill

Скажите пожалуйста как исправить ошибку и дописать программу для корректной работы?

from tkinter import *
import random

size_btn = 100
window = Tk()

list_btn = []
btn_click = []
btn_comp = []

# Класс кнопки
class Btn():
    def __init__(self, x0, y0, index_btn):
        self.index_btn = index_btn
        self.x0 = x0
        self.y0 = y0
        self.btn = Button(window, font=('Arial', 50, 'bold'))
        self.btn.place(x=x0, y=y0, width=size_btn, height=size_btn)
        self.wins = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24],
                     [0, 5, 10, 15, 20], [1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24],
                     [0, 6, 12, 18, 24], [4, 8, 12, 16, 20]]

    def check_win(self, lst1):
        for i in range(len(self.wins)):
            if all(elem in lst1 for elem in self.wins[i]):
                self.game_over(i, lst1)
def game_over(self, i, lst1):
    for q in list_btn:
        q.btn.unbind('')
    for w in range(5):
        list_btn[self.wins[i][w]].btn.config(bg='green')
def ctg_o(self, str1):
    self.btn.config(text=str1)
def game(self):
    if (len(btn_comp) + len(btn_click)) < 25:
        if len(btn_click) == 0:
            best_move = random.randint(0, 24)
        else:
            best_score = -float('inf')
            for i in range(25):
                if i not in btn_click and i not in btn_comp:
                    btn_click.append(i)
                    score = self.minimax(btn_click, 0, False, -float('inf'), float('inf'))
                    btn_click.remove(i)
                    if score > best_score:
                        best_score = score
                        best_move = i
            btn_comp.append(best_move)
            list_btn[best_move].ctg_o('O')
            list_btn[best_move].btn.unbind('')
            self.check_win(btn_comp)
            if self.check_winner(btn_comp):
                return
    self.check_win(btn_comp)
def click_start(self):
    global btn_click, btn_comp
    btn_click = []
    btn_comp = []
    for b in list_btn:
        b.btn.config(text='')
        b.btn.bind('', b.click)
        b.btn.config(bg='white')
    self.game()
def click(self, event):
    self.btn.config(text="X")
    btn_click.append(self.index_btn)
    self.check_win(btn_comp)
    self.check_win(btn_click)
    self.btn.unbind('')
    self.game()
def minimax(self, board, depth, is_maximizing, alpha, beta):
    if self.check_winner(btn_comp):
        return 1
    elif self.check_winner(btn_click):
        return -1
    elif len(board) == 25:
        return 0
    if is_maximizing:
        best_score = -float('inf')
        for i in range(25):
            if i not in btn_click and i not in btn_comp:
                board.append(i)
                score = self.minimax(board, depth + 1, False, alpha, beta)
                board.remove(i)
                best_score = max(best_score, score)
                alpha = max(alpha, best_score)
                if beta <= alpha:
                    break
        return best_score
    else:
        best_score = float('inf')
        for i in range(25):
            if i not in btn_click and i not in btn_comp:
                board.append(i)
                score = self.minimax(board, depth + 1, True, alpha, beta)
                board.remove(i)
                best_score = min(best_score, score)
                beta = min(beta, best_score)
                if beta <= alpha:
                    break
        return best_score
def check_winner(self, lst):
    for i in range(len(self.wins)):
        if all(elem in lst for elem in self.wins[i]):
            return True
    return False

# Создание игрового поля
def draw():
    index = 0
    x = 0
    y = 0
    for i in range(5):
        for j in range(5):
            list_btn.append(Btn(x, y, index))
            index += 1
            x += size_btn
        x = 0
        y += size_btn

draw()

# Кнопка для начала игры
Button(text='Start', height=2, width=8, command=list_btn[0].click_start).place(rely=0.9, relx=0.8)
Label(text='Для начала игры нажмите СТАРТ', height=2, width=30).place(relx=0.1, rely=0.9)
window.mainloop()

Traceback (most recent call last):
File "d:\загрузки\Untitled-1.py", line 125, in
Button(text='Start', height=2, width=8, command=list_btn[0].click_start).place(rely=0.9, relx=0.8)
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Btn' object has no attribute 'click_start'
  • Вопрос задан
  • 89 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы