<p>我从你的代码中做了一些调整,成功地修复了它。但是,我可以让它,如果一个X被创建,一个O和X不能在同一个磁贴上创建吗?谢谢你的帮助。这就是我目前所拥有的</p>
<pre><code>from tkinter import *
tk = Tk()
width = 600
third = width / 3
canvas = Canvas(width=width, height=width)
tk.title = "Tic Tac Toe"
line1 = canvas.create_line(200, 0, 200, 600)
line2 = canvas.create_line(400, 0, 400, 600)
line3 = canvas.create_line(0, 200, 600, 200)
line4 = canvas.create_line(0, 400, 600, 400)
class XsorOs:
def __init__(self):
self.turn = 0
self.clicked = []
def click(self, row, col):
if (row, col) not in self.clicked
if self.turn is 0:
canvas.create_line(col * third, row * third, (col + 1) * third, (row + 1) * third)
canvas.create_line((col + 1) * third, row * third, col * third, (row + 1) * third)
self.turn += 1
elif self.turn is 1:
canvas.create_oval(col * third + 5, row * third + 5, (col + 1) * third - 5, (row + 1) * third - 5)
self.turn -= 1
else:
print("Game Over")
self.clicked.append((row, col))
def mouse_click(c, event):
col = int(event.x / third)
row = int(event.y / third)
c.click(row, col)
xo = XsorOs()
canvas.pack()
canvas.bind("<Button-1>", lambda event: mouse_click(xo, event))
canvas.mainloop()
</code></pre>
<p>另外,如果你有理由使用我在评论中提到的方法,如果你能解释一下原因,我将不胜感激</p>