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"

Recommended Answers

All 3 Replies

from connect4cell2 import Connect4Cells
make sure the file you are importing is saved as connect4cell2.py

self.cells = Connect4Cells(rows, columns, 3)
self.cells can now use any of the instances methods like
row_flag = self.cells.checkRow(rowIndex)
you have to supply the rowIndex value

You could also have a list of win squares instead of checking for rows, diagonals, and columns separately. The following is pseudo-code:

def check_row(self, row):
    control=this_column[0]
    for column in row
        ## not occupied or not all the same
        if column == 0 or column != control:
            return False
    return True

def checkWin(self):
    ## check rows
    for j in range(self.rows)
        result = self.check_row(self.rows[j])  ## or just use for row in self.rows
        if result:
            print "Winner is player", self.player
            return result, self.player

    ## check_column would be the same as check_row, i.e. you could use
    ## the same function if you sent it a list of all rows[0] instead
    ## for example, and the same for diagonals
    result = check_column()   
    result = check_diagonal()

oh ok, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.