Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a method from the same class

Tags:

python

class

I'm writing a class for a simple game of 4 in a row, but I'm running into a problem calling a method in the same class. Here's the whole class for the sake of completeness:

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)

I'm trying to call checkLine() from check(), but I get the error "NameError: global name 'checkLine' is not defined". When I call Grid.checkLine() instead, I get "TypeError: 'module' object is not callable"

How do I call checkLine()?

EDIT:

@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)
like image 437
exodrifter Avatar asked Jul 18 '26 00:07

exodrifter


2 Answers

Get rid of the class. Use plain functions and module level variable for grid. The class is not helping you in any way.

PS. If you really want to call checkline from within the class, you'd call Grid.checkline. For example:

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

Foo.test2()       

prints

Hi
like image 144
unutbu Avatar answered Jul 19 '26 13:07

unutbu


Syntax:

class_Name.function_Name(self)

Example:

Turn.checkHoriz(self)
like image 43
ZigZag Avatar answered Jul 19 '26 13:07

ZigZag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!