All pastes #751257 Raw Edit

Connect Four

public text v1 · immutable
#751257 ·published 2007-10-27 04:58 UTC
rendered paste body
class ConnectFour(object):
	
	def __init__():
		gameBoard = [[0]*7]*7
		turn = 1
		
	def printBoard():
		print "\n"
		for a in range(7):
			for b in range(7):
				if not b == 6:
					print gameboard[a][b]
				else:
					print "\n"
		
	def putPiece(row,col):
			if turn == 1:
				gameBoard[row][col] = 'o'
			else:
				gameBoard[row][col] = 'x'
			turn = -turn
			
	def prompt():
		col = raw_input("Enter column: ")
		return col
	
	def getNextRow(col):
		for i in range(7):
			if not gameboard[col][i] == ' ':
				return i-1
	
	def wonHoriz():
		oconsecutive = 0
		xconsecutive = 0
		for a in range(7):
			for b in range(7):
				if gameboard[a][b] == 'o':
					oconsecutive=oconsecutive+1
				elif gameboard[a][b] == 'x':
					xconsecutive=xconsecutive+1
			if xconsecutive >= 4:
				return 'x'
			elif oconsecutive >= 4:
				return 'o'
			else:
				return '-'
			oconsecutive,xconsecutive = 0
			
	def wonVert():
		oconsecutive = 0
		xconsecutive = 0
		for a in range(7):
			for b in range(7):
				if gameboard[b][a] == 'x':
					xconsecutive = xconsecutive+1
				elif gameboard [b][a] == 'o':
					oconsecutive = oconsecutive+1
			if xconsecutive >= 4:
				return 'x'
			elif oconsecutive >= 4:
				return 'o'
			else:
				return '-'
			oconsecutive,xconsecutive = 0
	
	def checkWin():
		if wonHoriz() == 'o' or wonVert() == 'o':
			print "o is the winner!"
		elif wonVert() == 'x' or wonHoriz() == 'x':
			print 'x is the winner!'
	
	def main():
		for a in range(49):
			printBoard()
			prompt()
			putPiece(getNextRow(),prompt())
			checkWin()		
		
	if __name__ == '__main__':
		newGame = ConnectFour()
		newGame.main()