Reputation: 33
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 + 3
self.num_col = N + 2
self.cols = [Column(self.num_row) for i in range(self.num_col)]
self.row = [Column(self.num_col) for i in range(self.num_row)]
pass
#return
def display(self):
'''display the board'''
print ('\u26AA' '\u26AB')
#for rown in range(3):
# for col in range(2):
# btn = tk.Button(width = '5',height = '2', text = '')
# btn.bind("<Button-1>", on_btn_click)
# btn.grid(row = rown, column = col )
def drop_disk(self, c):
'''drop a disk at column c'''
pass
def check_winning_condition(self):
'''check if there is a winner'''
return True
class Column:
def __init__(self, r):
'''each column has r rows'''
self.rows = [0]*r
N = 4 #standard Connect-4
board = Board(N)
board.display()
I am trying to create a connect-4 game but not sure how to do it. I got some outline but don't know how to create it. I'm trying to create a board with 2 columns and 3 rows. The image is what I want it to look like but it didn't work.
Upvotes: 0
Views: 347
Reputation: 36682
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 .
Upvotes: 1