Hi Guys,

I am making a board game, where there is a 3x3 grid and the user enters a row and column and it should put an x in that spot on the grid and ask for the user for another location, until they enter 0 to quit. Here is what I have so far. I've built the grid, now I need to figure out how to modify it. Could someone point me in the right direction? Thanks!

import sys

class Board:
   SIZE = 3
   grid = []

   def __init__(self):
      # Create the grid and set by default each location to an empty space.      
      for rowIndex in range(self.SIZE):
         for columnIndex in range(self.SIZE):
            aPiece = Piece(Piece.EMPTY_MARK)

   def displayConsole(self):
      for rowIndex in range(self.SIZE):
         print(' - - - ')
         for columnIndex in range(self.SIZE):
      print(' - - - ')

   def markSquare(self, rowIndex, columnIndex) :
      self.grid[rowIndex][columnIndex] = Piece(Piece.X_MARK)

class Piece:
   EMPTY_MARK = ' '
   X_MARK = 'X'

   def __init__(self, anAppearance):
      self.appearance = anAppearance

   def displayConsole(self):

class Move:

def main():
   aBoard = Board()
   aMove = Move()

5 Years
Discussion Span
Last Post by woooee

"grid is not an instance variable, but a class variable. That means that it is common to all class instances. See the example below. I think you want to use an instance variable.

class Board:
   SIZE = 3
   grid = []
   def __init__(self, var):
      print self.grid

b = Board("b")     ## first class instance
c = Board("c")     ## another class instance

Updating done with a list of lists.

class Board:
    def __init__(self):
        self.grid = []          ## instance variable
        for ctr in range(3):
            self.grid.append(['-', '-', '-'])

    def print_grid(self):
        for gr in self.grid:
            print "%s|%s|%s" % (gr[0], gr[1], gr[2])

    def get_row_col(self):
        ## note that this check is not complete----->test for 1 <= entry <= 3
        row=raw_input("Enter row # (1, 2, 3) ")
        if row in ["1", "2", "3"]:
            col=raw_input("Enter column # (1, 2, 3) ")
            if col in ["1", "2", "3"]:
                ## convert to a list offset by subtracting one
                list_index_row = int(row)-1
                list_index_col = int(col)-1
                if self.grid[list_index_row][list_index_col] == "-": # not occupied
                    self.grid[list_index_row][list_index_col] = "X"


## a simple test

c=Board()     # a different board
## will not print entries from the first class instance

There are multiple problems with your code so you might want to read this tutorial for classes http://hetland.org/writing/instant-python.html

Edited by woooee: n/a


Awesome, thanks so much for the help. And, that code that I posted earlier, was actually checked over by the instructor and he approved ;\, but your code looks more efficient and makes more sense. Thanks again!


My main problem is that you are appending a class instance to the list and not using the class as a class, but just as a way to insert "X", which would work just as well with a variable. Also, this code creates a class instance and you pass a variable to the class that is already part of the class, and the class does nothing useful with it.
aPiece = Piece(Piece.EMPTY_MARK)
You do use the "displayConsole()" function but all it does is print the character that was passed to the class (that is already a class variable). So, instead of

aPiece = Piece(Piece.EMPTY_MARK)
## and then later
# you can use
            self.grid[rowIndex].append(Piece.EMPTY_MARK)   ## append 3 times for 3x3 grid
# and
            print self.grid[rowIndex][columnIndex]

I hope this helps your understanding of classes and instance variables vs. class variables. Classes are used in most non-trivial programs and understanding how they work is something that is worth the time and effort spent IMHO.

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.