this is my first time asking a question. I am trying to make a connect 4 python game with the height of the table that can be modified by the players but I am having problems with checking the diagonals since the height can changed. Anyone can help me ?
Asked
Active
Viewed 62 times
-1
-
Could you please show something you have tried so far? – Christian Feb 16 '22 at 11:11
1 Answers
0
try this function it will check if the user wins or not but instead of checking the whole board it will check the last play position you will pass row and col position of the last play
def check_diagonal(row,col):
# First diagonal
for i in range(-min(3, row, col), min(1, 3-row, 4-col)):
if self.grid[row+i][col+i] == self.grid[row+i+1][col+i+1] == self.grid[row+i+2][col+i+2] == self.grid[row+i+3][col+i+3]:
return True
# Second diagonal
for i in range(-min(3, row, 6-col), min(1, 3-row, col-2)):
if self.grid[row+i][col-i] == self.grid[row+i+1][col-i-1] == self.grid[row+i+2][col-i-2] == self.grid[row+i+3][col-i-3]:
return True
return False

Super sub
- 269
- 2
- 8