Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a method from the same class
    primarykey
    data
    text
    <p>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:</p> <pre><code>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 &lt; 0 or x &gt; 9: return if y &lt; 0 or y &gt; 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] &gt; 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) </code></pre> <p>I'm trying to call checkLine() from check(), but I get the error "<strong>NameError: global name 'checkLine' is not defined</strong>". When I call Grid.checkLine() instead, I get "<strong>TypeError: 'module' object is not callable</strong>"</p> <p>How do I call checkLine()?</p> <p>EDIT:</p> <p>@beer_monk</p> <pre><code>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 &lt; 0 or x &gt; 9: return if y &lt; 0 or y &gt; 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] &gt; 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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload