一个井字小游戏,切换玩家功能没有实现,请问如何解决
from tkinter import *import random
def next_turn(row, column):
global player
if buttons['text'] == "" and check_winner() is False:
if player == players:
buttons['text'] = player
if check_winner is False:
player = players
label.config(text=(players+" turn"))
elif check_winner() is True:
label.config(text=(players+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
else:
buttons['text'] = player
if check_winner is False:
player = players
label.config(text=(players + " turn"))
elif check_winner() is True:
label.config(text=(players + " wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
def check_winner():
for row in range(3):
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
for column in range(3):
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
elif buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
elif empty_spaces() is False:
for row in range(3):
for column in range(3):
buttons.config(bg="yellow")
return "Tie"
else:
return False
def empty_spaces():
spaces = 9
for row in range(3):
for column in range(3):
if buttons['text'] != "":
spaces -= 1
if spaces == 0:
return False
else:
return True
def new_game():
global player
player = random.choice(players)
label.config(text=player+" turn")
for row in range(3):
for column in range(3):
buttons.config(text="", bg="#F0F0F0")
window = Tk()
window.title("Tic-Tac-Toe")
players = ["x", "o"]
player = random.choice(players)
buttons = [,
,
]
label = Label(text= player + " turn", font=('consolas',40))
label.pack(side="top")
reset_button = Button(text="restart", font=('consolas',20), command=new_game)
reset_button.pack(side="top")
frame= Frame(window)
frame.pack()
for row in range(3):
for column in range(3):
buttons = Button(frame, text="", font=('consolas',20), width=5, height=2,
command= lambda row=row, column=column: next_turn(row, column))
buttons.grid(row=row, column=column)
window.mainloop() from tkinter import *
import random
def next_turn(row, column):
global player
if buttons['text'] == "" and check_winner() is False:
if player == players:
buttons['text'] = player
if check_winner is False:
player = players
label.config(text=(players+" turn"))
elif check_winner() is True:
label.config(text=(players+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
else:
buttons['text'] = player
if check_winner is False:
player = players
label.config(text=(players + " turn"))
elif check_winner() is True:
label.config(text=(players + " wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
def check_winner():
for row in range(3):
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
for column in range(3):
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
elif buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
elif empty_spaces() is False:
for row in range(3):
for column in range(3):
buttons.config(bg="yellow")
return "Tie"
else:
return False
def empty_spaces():
spaces = 9
for row in range(3):
for column in range(3):
if buttons['text'] != "":
spaces -= 1
if spaces == 0:
return False
else:
return True
def new_game():
global player
player = random.choice(players)
label.config(text=player+" turn")
for row in range(3):
for column in range(3):
buttons.config(text="", bg="#F0F0F0")
window = Tk()
window.title("Tic-Tac-Toe")
players = ["x", "o"]
player = random.choice(players)
buttons = [,
,
]
label = Label(text= player + " turn", font=('consolas',40))
label.pack(side="top")
reset_button = Button(text="restart", font=('consolas',20), command=new_game)
reset_button.pack(side="top")
frame= Frame(window)
frame.pack()
for row in range(3):
for column in range(3):
buttons = Button(frame, text="", font=('consolas',20), width=5, height=2,
command= lambda row=row, column=column: next_turn(row, column))
buttons.grid(row=row, column=column)
window.mainloop() {:9_241:} 已解决 好吧,其实是这个,前面的中间漏了一行update代码 from tkinter import *
import random
def next_turn(row, column):
global player
if buttons['text'] == "" and check_winner() is False:
if player == players:
buttons['text'] = player
if check_winner() is False:
player = players
label.config(text=(players+" turn"))
elif check_winner() is True:
label.config(text=(players+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
else:
buttons['text'] = player
if check_winner() is False:
player = players
label.config(text=(players+" turn"))
elif check_winner() is True:
label.config(text=(players+" wins"))
elif check_winner() == "Tie":
label.config(text="Tie!")
def check_winner():
for row in range(3):
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
for column in range(3):
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
if buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
elif buttons['text'] == buttons['text'] == buttons['text'] != "":
buttons.config(bg="green")
buttons.config(bg="green")
buttons.config(bg="green")
return True
elif empty_spaces() is False:
for row in range(3):
for column in range(3):
buttons.config(bg="yellow")
return "Tie"
else:
return False
def empty_spaces():
spaces = 9
for row in range(3):
for column in range(3):
if buttons['text'] != "":
spaces -= 1
if spaces == 0:
return False
else:
return True
def new_game():
global player
player = random.choice(players)
label.config(text=player+" turn")
for row in range(3):
for column in range(3):
buttons.config(text="", bg="#F0F0F0")
window = Tk()
window.title("Tic-Tac-Toe")
players = ["x", "o"]
player = random.choice(players)
buttons = [,
,
]
label = Label(text=player + " turn", font=('consolas',40))
label.pack(side="top")
reset_button = Button(text="restart", font=('consolas',20), command=new_game)
reset_button.pack(side="top")
frame = Frame(window)
frame.pack()
for row in range(3):
for column in range(3):
buttons = Button(frame, text="", font=('consolas',40), width=5, height=2,
command= lambda row=row, column=column: next_turn(row, column))
buttons.grid(row=row, column=column)
window.mainloop()
页:
[1]