简单的井字程序

Simple Tictactoe program

我正在尝试实现一个简单的井字游戏程序,但在实现游戏时遇到了困难。这是我到目前为止的代码:

from Tkinter import *
click = False

class Buttons(object):

    def __init__(self,master):
        frame = Frame(master)
        frame.pack()

        self.button1= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button1.grid(row=0,column=0)

        self.button2= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button2.grid(row=0,column=1)

        self.button3= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button3.grid(row=0,column=2)

        self.button4= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button4.grid(row=1,column=0)

        self.button5= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button5.grid(row=1,column=1)

        self.button6= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button6.grid(row=1,column=2)

        self.button7= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button7.grid(row=2,column=0)

        self.button8= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button8.grid(row=2,column=1)

        self.button9= Button(frame,text=" ",height=4,width=8,
                             command=self.moveYes)
        self.button9.grid(row=2,column=2)

    def moveYes(self):
        global click
        global buttons
        if buttons["text"] == " " and click == False:
            buttons["text"]="X"
            click= True
        elif buttons["text"] == " " and click == True:
            buttons["text"]="O"

root = Tk()
b=Buttons(root)

root.mainloop()

当我 运行 这个时,它说:

global name 'buttons' is not defined

我想我的第二种方法不正确。我不明白如何访问按钮和比较值以便我可以更改文本。

我现在应该从双人模式开始,然后再转向 AI 吗?

我认为问题在于您试图将函数内的 global 关键字分配给不存在的变量。

您的文件主体中没有 buttons 变量。

点击按钮时的问题,程序需要知道你点击的是哪个按钮,这就是为什么你要访问一些全局的buttons,不需要那个,你可以通过按钮和您的命令处理程序所需的任何其他内容,例如command=lambda self.click(self.button_x)

    self.button1= Button(frame,                                                    
                         height=4, width=8,
                         text=' ',
                         command=lambda: self.click(self.button1)
    )       

这是我在上面的按钮命令中使用的 click() 方法

def click(self, button):
    """
    Dummy logic for demo, it toggles between O and X
    """

    if button['text'] == ' ' or button['text'] == 'O':                 
        button['text'] = 'X'                                                
    else:                                                                   
        button['text'] = 'O'  

我有运行这个程序,它的工作原理是按钮可以在 X 和 O 之间切换,这演示了按钮命令功能,我将留下实际的 tic给你的战术游戏。

首先,每当您发现自己在重复几乎相同的一行或几行代码时,这表明您需要编写某种循环。

其次,您可以通过将数据作为参数传递给您的 command 函数来避免使用全局变量——这将使您遇到的错误消失。

在这种情况下,它只是被点击的按钮,可以通过创建一个简短的 lamdba 函数并将按钮作为默认参数来完成。这必须在创建 Tkinter Button 之后进行,因此需要在创建 之后进行单独的 config() 调用 。像这样传递额外数据有时称为 the extra arguments trick 并且可以扩展为包括其他感兴趣的项目,例如按钮的行和列(甚至是它们的整个列表)。

from Tkinter import *

class Buttons(object):
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        self.buttons = []
        for row in xrange(3):
            for col in xrange(3):
                button = Button(frame, text=" ", height=4, width=8)
                button.config(command=lambda b=button: self.clicked(b))
                button.grid(row=row, column=col)
                self.buttons.append(button)

    def clicked(self, button):
        if button["text"] == " ":
            button["text"] = "X"
        else:
            button["text"] = "O"

root = Tk()
b = Buttons(root)
root.mainloop()

既然基本的图形用户界面是 运行 并且只要用户单击网格位置就会调用一个函数,您可以开始实现 "gaming"。这可以通过向 clicked() 方法添加代码来完成,该方法检查哪些位置填充了哪些字符("X""O")并相应地做出响应。

我认为首先实现一个 "AI" 版本是最简单的,因为这样做需要的工作量最少。