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.

#!/usr/bin/python

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
        break
    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')
      connect.place(intcol, sym)
   except:
      print 'Game exited'
      break

if __name__ == '__main__'
  main()

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s