Trying to create a connect 4 game with text-base then implement GUI later

You can use the __str__ method to print your board:
Once the game works the way you need, you can easily build a GUI with tkinter around it, in a separate set of files.

In the following example, I am using a list of lists to represent the board. I changed the settings to represent the traditional 6 rows and 7 columns of connect-4.

class Board:    
    def __init__(self, N):
        '''Board for a Connect-N game, the board will have N+2 Col and N+3 Rows'''
        self.num_row = N + 2
        self.num_col = N + 3        
        self.cells = [[None for _ in range(self.num_col)] for _ in range(self.num_row)]
    
    def __str__(self):
        res = []
        for row in self.cells:
            for cell in row:
                c = cell
                if cell is None:
                    c = '.'
                res.append(f'{c} ')
            res.append('\n')
        return ''.join(res)

    
    def drop_disk(self, c, player='X'):
        '''drop a disk at column c
        return True if successful
        '''
        for r, row in enumerate(self.cells):
            if row[c] is None:
                continue
            elif r == 0:
                break
            self.cells[r-1][c] = player
            return True
        else:   # this executes when there is `no break`
            self.cells[r][c] = player
            return True
        return False
        

    def check_winning_condition(self):
        '''check if there is a winner'''
        return True

N = 4 #standard Connect-4
board = Board(N)
print(board)

output:

. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 

I also added the drop_disk method in order to test the output; the method returns True if a disc was successfully played, and False otherwise.

print(board.drop_disk(3, 'X'))
print(board)
print(board.drop_disk(3, 'O'))
print(board)
print(board.drop_disk(4, 'X'))
print(board)
print(board.drop_disk(5, 'O'))
print(board)
print(board.drop_disk(3, 'X'))
print(board)
print(board.drop_disk(3, 'O'))
print(board)
print(board.drop_disk(3, 'X'))
print(board)
print(board.drop_disk(3, 'O'))
print(board)
print(board.drop_disk(3, 'X'))
print(board)
print(board.drop_disk(3, 'O'))
print(board)

Output:

True
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . X . . . 

True
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . O . . . 
. . . X . . . 

True
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . O . . . 
. . . X X . . 

True
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . O . . . 
. . . X X O . 

True
. . . . . . . 
. . . . . . . 
. . . . . . . 
. . . X . . . 
. . . O . . . 
. . . X X O . 

True
. . . . . . . 
. . . . . . . 
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X X O . 

True
. . . . . . . 
. . . X . . . 
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X X O . 

True
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X X O . 

False
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X X O . 

False
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X . . . 
. . . O . . . 
. . . X X O .

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top