Помогите с написанием кода

klaulik

Новичок
Пользователь
Дек 15, 2020
1
0
1
Нужно сделать что бы можно было играть с компьютером (ИИ) с этого кода, помогите пожалуйста

from tkinter import *
from tkinter import messagebox

round=0
player = "X"

playerX = []
player0 = []

def ButtonClick(id):

global round

round = round+1
if round % 2 == 0:
player = "0"
player0.append(id)
else:
player = "X"
playerX.append(id)
print("Hello World", id)
if id == 1:
button1.config(text=player, state=DISABLED)
elif id == 2:
button2.config(text=player, state=DISABLED)
elif id == 3:
button3.config(text=player, state=DISABLED)
elif id == 4:
button4.config(text=player, state=DISABLED)
elif id == 5:
button5.config(text=player, state=DISABLED)
elif id == 6:
button6.config(text=player, state=DISABLED)
elif id == 7:
button7.config(text=player, state=DISABLED)
elif id == 8:
button8.config(text=player, state=DISABLED)
elif id == 9:
button9.config(text=player, state=DISABLED)

res = checkWin()
if res == "X":
messagebox.showinfo("Game over", "Winner is X!")
if res == "0":
messagebox.showinfo("Game over", "Winner is 0!")
if round == 9 and res == "-":
messagebox.showinfo("Game over", "It's Tie")


def checkWin():
winner = "-"
#Horizontal
if (1 in player0) and (2 in player0) and 3 in player0:
winner = "0"
if (1 in playerX) and (2 in playerX) and 3 in playerX:
winner = "X"
if (4 in player0) and (5 in player0) and 6 in player0:
winner = "0"
if (4 in playerX) and (5 in playerX) and 6 in playerX:
winner = "X"
if (7 in player0) and (8 in player0) and 9 in player0:
winner = "0"
if (7 in playerX) and (8 in playerX) and 9 in playerX:
winner = "X"
#Vertical
if (1 in player0) and (4 in player0) and 7 in player0:
winner = "0"
if (1 in playerX) and (4 in playerX) and 7 in playerX:
winner = "X"
if (2 in player0) and (5 in player0) and 8 in player0:
winner = "0"
if (2 in playerX) and (5 in playerX) and 8 in playerX:
winner = "X"
if (3 in player0) and (6 in player0) and 9 in player0:
winner = "0"
if (3 in playerX) and (6 in playerX) and 9 in playerX:
winner = "X"
#Diagonal
if (1 in player0) and (5 in player0) and 9 in player0:
winner = "0"
if (1 in playerX) and (5 in playerX) and 9 in playerX:
winner = "X"
if (3 in player0) and (5 in player0) and 7 in player0:
winner = "0"
if (3 in playerX) and (5 in playerX) and 7 in playerX:
winner = "X"

return winner

window = Tk()
window.title("Tic Tac Toe")


button1 = Button(window, text="")
button1.grid(row=0, column=0, ipadx=60, ipady=60, sticky="snew")
button1.config(command=lambda: ButtonClick(1))

button2 = Button(window, text="")
button2.grid(row=0, column=1, ipadx=60, ipady=60, sticky="snew")
button2.config(command=lambda: ButtonClick(2))

button3 = Button(window, text="")
button3.grid(row=0, column=2, ipadx=60, ipady=60, sticky="snew")
button3.config(command=lambda: ButtonClick(3))

################################################################

button4 = Button(window, text="")
button4.grid(row=1, column=0, ipadx=60, ipady=60, sticky="snew")
button4.config(command=lambda: ButtonClick(4))

button5 = Button(window, text="")
button5.grid(row=1, column=1, ipadx=60, ipady=60, sticky="snew")
button5.config(command=lambda: ButtonClick(5))

button6 = Button(window, text="")
button6.grid(row=1, column=2, ipadx=60, ipady=60, sticky="snew")
button6.config(command=lambda: ButtonClick(6))

###############################################################

button7 = Button(window, text="")
button7.grid(row=2, column=0, ipadx=60, ipady=60, sticky="snew")
button7.config(command=lambda: ButtonClick(7))

button8 = Button(window, text="")
button8.grid(row=2, column=1, ipadx=60, ipady=60, sticky="snew")
button8.config(command=lambda: ButtonClick(8))

button9 = Button(window, text="")
button9.grid(row=2, column=2, ipadx=60, ipady=60, sticky="snew")
button9.config(command=lambda: ButtonClick(9))

###############################



window.mainloop()
 

regnor

Модератор
Команда форума
Модератор
Июл 7, 2020
2 566
454
83
вставьте код через теги code, где пишите сообщение чуть выше есть панель инструментов там есть три точки -> код -> питон, без отступов ничего не понятно...
что значит играть? во что играть? в салки? в вышибалы? в чем проблема конкретно в коде...
вы простите но тут телепатов нет...

p.s.
ИИ громкое название...
 

Форум IT Специалистов