从同一个类调用一个方法

exo*_*ter 1 python class

我正在为一个连续4的简单游戏编写一个类,但是我遇到了在同一个类中调用方法的问题.为了完整起见,这是全班:

class Grid:
    grid = None
    # creates a new empty 10 x 10 grid
    def reset():
        Grid.grid = [[0] * 10 for i in range(10)]
    # places an X or O
    def place(player,x,y):
        Grid.grid[x][y] = player
    # returns the element in the grid
    def getAt(x,y):
        return Grid.grid[x][y]
    # checks for wins in a certain direction
    def checkLine(player,v,count,x,y):
        x = x+v[0]
        y = y+v[1]
        if x < 0 or x > 9:
            return
        if y < 0 or y > 9:
            return
        if Grid.grid[x][y] == p:
            count = count+1
            if count == 4:
                return True
            checkLine(player,v,count,x,y)
        return False
    # returns the number of the player that won
    def check():
        i = 'i'
        for x in range(0,10):
            for y in range(0,10):
                if Grid.grid[x][y] > 0:
                    p = Grid.grid[x][y]
                    f = checkLine(p,0,array(i,[1,0]),x,y)
                    if f:
                        return p
                    f = checkLine(p,0,array(i,[0,1]),x,y)
                    if f:
                        return p
                    f = checkLine(p,0,array(i,[1,1]),x,y)
                    if f:
                        return p
                    f = checkLine(p,0,array(i,[-1,0]),x,y)
                    if f:
                        return p
                    f = checkLine(p,0,array(i,[0,-1]),x,y)
                    if f:
                        return p
                    f = checkLine(p,0,array(i,[-1,-1]),x,y)
                    if f:
                        return p
                    f = checkLine(p,0,array(i,[1,-1]),x,y)
                    if f:
                        return p
                    f = checkLine(p,0,array(i,[-1,1]),x,y)
                    if f:
                        return p
        return 0
    reset = staticmethod(reset)
    place = staticmethod(place)
    getAt = staticmethod(getAt)
    check = staticmethod(check)
    checkLine = staticmethod(checkLine)
Run Code Online (Sandbox Code Playgroud)

我试图从check()调用checkLine(),但是我收到错误" NameError:全局名称'checkLine'未定义 ".当我调用Grid.checkLine()时,我得到" TypeError:'module'对象不可调用 "

我如何调用checkLine()?

编辑:

@beer_monk

class Grid(object):
    grid = None
    # creates a new empty 10 x 10 grid
    def reset(self):
        Grid.grid = [[0] * 10 for i in range(10)]
    # places an X or O
    def place(self,player,x,y):
        Grid.grid[x][y] = player
    # returns the element in the grid
    def getAt(self,x,y):
        return Grid.grid[x][y]
    # checks for wins in a certain direction
    def checkLine(self,player,v,count,x,y):
        x = x+v[0]
        y = y+v[1]
        if x < 0 or x > 9:
            return
        if y < 0 or y > 9:
            return
        if Grid.grid[x][y] == p:
            count = count+1
            if count == 4:
                return True
            checkLine(self,player,v,count,x,y)
        return False
    # returns the number of the player that won
    def check(self):
        i = 'i'
        for x in range(0,10):
            for y in range(0,10):
                if Grid.grid[x][y] > 0:
                    p = Grid.grid[x][y]
                    for vx in range(-1,2):
                        for vy in range(-1,2):
                            f = self.checkLine(p,0,array(i,[vx,vy]),x,y)
                            if f:
                                return p
        return 0
    reset = staticmethod(reset)
    place = staticmethod(place)
    getAt = staticmethod(getAt)
    check = staticmethod(check)
    checkLine = staticmethod(checkLine)
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 8

摆脱这个阶级.使用普通函数和模块级变量grid.班级没有以任何方式帮助你.

PS.如果你真的想checkline在课堂上打电话,你可以打电话Grid.checkline.例如:

class Foo:
    @staticmethod
    def test():
        print('Hi')
    @staticmethod
    def test2():
        Foo.test()

Foo.test2()       
Run Code Online (Sandbox Code Playgroud)

版画

Hi
Run Code Online (Sandbox Code Playgroud)