Итак, я разработал программу «крестики-нолики» для школьного проекта. Я создал случайную функцию, которая рандомизирует любое число до 8. У меня было 9 кнопок, каждая для O (который является игроком) и X (который является компьютером).
Что я хочу сделать, так это то, что если игрок нажимает первую кнопку, то случайная функция (названная comesy, сокращение от «легкий компьютер») будет случайным образом выбирать любое число из 8 и настраивать или нажимать кнопку, связанную с ее номером. , Например, если я нажму на кнопку 1, которая является верхней левой кнопкой, комедия может рандомизировать 2, чем она нажмет кнопку 2, которая является верхней средней кнопкой. В основном как ИИ.
Я попытался добавить дополнительную переменную, равную нулю, которая находится внутри функции для button1. Переменная будет добавлена к единице при выполнении функции для кнопки 1. Я сделал оператор if, который бы утверждал, что если эта переменная равна единице, то заставь комеди выбрать номер и случайным образом нажимать кнопку, кроме кнопки 1.
Это мой код:
from tkinter import *
import random
from tkinter import font
from tkinter import *
import random
from tkinter import font
window = Tk()
window.geometry("750x500")
window.title("Tic Tac Toe")
butn = 0
butn2 = 0
butn3 = 0
#These will be the images
blnk = PhotoImage(file="blank.png")
lbl = Label(text="")
#Easy random
def btn1():
global butn
button1.configure(text="O")
button1.configure(state="disabled")
butn+=1
def btn2():
global butn2
button2.configure(text="O")
button2.configure(state="disabled")
butn2=2
def btn3():
global butn3
button3.configure(text="O")
button3.configure(state="disabled")
butn3=3
def btn4():
button4.configure(text="O")
button4.configure(state="disabled")
def btn5():
button5.configure(text="O")
button5.configure(state="disabled")
def btn6():
button6.configure(text="O")
button6.configure(state="disabled")
def btn7():
button7.configure(text="O")
button7.configure(state="disabled")
def btn8():
button8.configure(text="O")
button8.configure(state="disabled")
def btn9():
button9.configure(text="O")
button9.configure(state="disabled")
#These well be the def functions
def exits():
window.destroy()
#This is the restart function
def restart():
lbl.configure(text="Please choose one", font=("Times New Roman 5 bold",15))
f = font.Font(lbl, lbl.cget("font"))
f.configure(underline=True)
lbl.configure(font=f)
hrd.configure(state='normal')
mdm.configure(state='normal')
esy.configure(state='normal')
twply.configure(state='normal')
button1.configure(text="")
button1.configure(state='normal')
button2.configure(text="")
button2.configure(state='normal')
button3.configure(text="")
button3.configure(state='normal')
button4.configure(text="")
button4.configure(state='normal')
button5.configure(text="")
button5.configure(state='normal')
button6.configure(text="")
button6.configure(state='normal')
button7.configure(text="")
button7.configure(state='normal')
button8.configure(text="")
button8.configure(state='normal')
button9.configure(text="")
button9.configure(state='normal')
window.mainloop()
#This is the two player function
def twoPlay():
lbl.configure(text="You chose Two Player!")
mdm.configure(state='disabled')
esy.configure(state='disabled')
hrd.configure(state='disabled')
lbl.place_configure(x=150, y=9)
window.mainloop()
#This is the medium function
def medium():
lbl.configure(text="You chose Medium!")
lbl.place_configure(x=180,y=9)
hrd.configure(state='disabled')
esy.configure(state='disabled')
twply.configure(state='disabled')
window.mainloop()
#This is the easy function
def easy():
lbl.configure(text="You chose Easy!")
lbl.place_configure(x=180,y=9)
mdm.configure(state='disabled')
hrd.configure(state='disabled')
twply.configure(state='disabled')
window.mainloop()
#This is the hard function
def hard():
lbl.configure(text="You chose Hard!")
lbl.place_configure(x=180,y=9)
mdm.configure(state='disabled')
esy.configure(state='disabled')
twply.configure(state='disabled')
window.mainloop()
#These are the buttons in the tic tac toe
button1 = Button(window,text="",width=14,height=7,command=btn1)
button1.place(x=100,y=95)
button2 = Button(window,text="",width=14,height=7,command=btn2)
button2.place(x=200,y=95)
button3 = Button(window,text="",width=14,height=7,command=btn3)
button3.place(x=300,y=95)
button4 = Button(window,text="",width=14,height=7,command=btn4)
button4.place(x=100,y=200)
button5 = Button(window,text="",width=14,height=7,command=btn5)
button5.place(x=200,y=200)
button6 = Button(window,text="",width=14,height=7,command=btn6)
button6.place(x=300,y=200)
button7 = Button(window,text="",width=14,height=7,command=btn7)
button7.place(x=100,y=305)
button8 = Button(window,text="",width=14,height=7,command=btn8)
button8.place(x=200,y=305)
button9 = Button(window,text="",width=14,height=7,command=btn9)
button9.place(x=300,y=305)
#These are the play mode buttons
hrd = Button(window, text="Hard",width=7,height=1,command=hard)
hrd.place(x=100,y=40)
mdm = Button(window,text="Medium",width=7,height=1,command=medium)
mdm.place(x=180,y=40)
esy = Button(window,text="Easy",width=7,height=1,command=easy)
esy.place(x=260,y=40)
twply = Button(window,text="2 Player",width=7,height=1,command=twoPlay)
twply.place(x=340,y=40)
#Restart and exit button
rst = Button(window, text="Restart",width=7,height=1,command=restart)
rst.place(x=424,y=385)
ext = Button(window, text="Exit", width=7,height=1,command=exits)
ext.place(x=490,y=385)
#This is going to be the label thats above the mode btns
lbl = Label(window,text="Please choose one", font=("Times New Roman 5 bold",15))
lbl.place(x=180,y=9)
f = font.Font(lbl, lbl.cget("font"))
f.configure(underline=True)
lbl.configure(font=f)
#Easy random
comesy = random.randrange(8)
if button1==btn1 and butn==1:
print("Button 1 clicked")
if comesy==1:
button2.configure(text="X")
comesy = random.randrange(8)
elif comesy==2:
button3.configure(text="X")
comesy = random.randrange(8)
elif comesy==3:
button4.configure(text="X")
comesy = random.randrange(8)
elif comesy==4:
button5.configure(text="X")
comesy = random.randrange(8)
elif comesy==5:
button6.configure(text="X")
comesy = random.randrange(8)
elif comesy==6:
button7.configure(text="X")
comesy = random.randrange(8)
elif comesy==7:
button8.configure(text="X")
comesy = random.randrange(8)
elif comesy==8:
button9.configure(text="X")
comesy = random.randrange(8)
window.mainloop()