First Attempt at Connect 4 in Python

After I stopped panicing and relaxed it was not that difficult – but that is the ay the cookie crumbles. Still need to do some debugging but as first attempt and my limited knowledge of python I don’t think its that bad as it was written with the nano text editor.


class board(object)

  def __init__(self,cells=6,rows=7) :
    self.cells = cells
    self.rows = rows
    self.board = {}
    for row in xrange(rows) :
      self.board[row] = ' ' * cells

  def __str__() :
    print " " + "_ " * self.cells
    for row in xrange(self.rows):
      print '|', for cell in row : print cell + '|'
    print " " + "_ " * self.cells

  def occupied(self, row, cell) :
    return self.board(row)[cell] == ''

  def unoccupied(self, row, cell) :
    return not occupied(row,cell)

class connect(object) :

  def __init__(self) :
    self.board = board()

  def place(self, cell, sym) :
    for row in reversed(range(self.board.rows)) :
      if self.board.unoccupied(row, cell) :
        self.board.board(row)[cell] = sym
    print self.board

def main() :
  game = connect
  print "To exit game enter non integer when prompted for coloumn"
  while True
    try :
      strcol = raw_input('Enter column number')
      intcol = int(strcol)
      sym = raw_input('Enter your playing symbol'), sym)
      print 'Game exited'

if __name__ == '__main__'

IDE’s are for sissies, f I could write whole JEE application in nano … I can


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s