trying to impleament a checkWin function that checks all possible wins for a player, but I'm notreally sure how to do it.
from connect4cell2 import Connect4Cells
from Tkinter import *
class Connect4Frame(object):
COLORS = ["White", "Red", "Yellow"]
def __init__(self,rows = 6, columns = 7):
self.cells = Connect4Cells(rows, columns, 3)
self.rows = rows
self.columns = columns
self.atRow = 0
self.atColumn = 0
self.root = Tk()
self.root.minsize(500,520)
self.root.geometry("500x520")
self.root.title("Connect 4")
self.root.grid()
self.player = 1
self.buttons = []
for i in range(self.rows):
self.buttons.append([])
for j in range(self.columns):
btn = Button(self.root,command=lambda x=i,y=j:self.clicked(x,y),\
borderwidth=2,padx=30,pady=20)
btn.grid(row=i,column=j)
self.buttons[i].append(btn)
self.colorButtons()
def clicked(self, rowIndex, columnIndex):
self.atRow = rowIndex
self.atColumn = columnIndex
result = self.cells.clickAt(self.atColumn,self.player)
if result:
self.player = 3 - self.player
self.colorButtons()
def colorButtons(self):
for i in range(self.rows):
for j in range(self.columns):
self.buttons[i][j].configure(bg=Connect4Frame.COLORS[self.cells.getCellNumber(i,j)])
def mainloop(self):
self.root.mainloop()
w = Connect4Frame()
w.mainloop()
the coding i imported into my frame
class Connect4Cells(object):
def __init__(self, rows = 7, columns=6, numcolors = 3):
self._rowCounts = rows
self._numColors = numcolors
self._columnCounts = columns
self._cells = []
for i in range(rows):
self._cells.append([])
for j in range(columns):
self._cells[i].append(0)
def checkRow(self, rowIndex):
for j in range(self.columns):
if self._cells[rowIndex][j] == 0:
return True
return False
def checkColumn(self, columnIndex):
for i in range(self.rows):
if self._cells[i][columnIndex] == 0:
return True
return False
def getFirstEmptyCell(self, columnIndex):
k = self._rowCounts - 1
while self._cells[k][columnIndex] != 0:
k -= 1
return k
def clickAt(self, columnIndex, player):
self.checkWin()
row = self.getFirstEmptyCell(columnIndex)
if row < 0:
return False
self._cells[row][columnIndex] = player
return True
def getCellNumber(self,rowIndex,columnIndex):
return self._cells[rowIndex][columnIndex]
def checkWin(self, rowIndex,columnIndex,player):
for i in range(self.columns):
for j in range(self.rows):
if self._cells[i][j] == self._cells[i+j] == self._cells[i+3][j] == self._cells[i][j+3] == player:
if player == 1:
print "player 1 wins"
else:
print "player 2 wins"