Given a board of n x n size that is greater than 4 and no larger than 100. How can we check if a list[str] has connecting elements from left to right, up to down, and diagonally (top left to bottom right / top right to bottom left)?
for example if we have a list:
Matrix = [ 'XXXOO',
'X.OXX',
'XOXOO',
'O.XOO',
'XXXOO']
we should be able to see that player O has a matching four
this is the code that I have at the moment:
Matrix = [
'XXXOO',
'X.OXX',
'XOXOO',
'O.XOO',
'XXXOO']
def WhoWon(Matrix:list[str]) -> str:
rows = len(Matrix)
columns = len(Matrix[0])
if 4 <= len(Matrix) <= 100:
for Row in range(rows-3):
for Col in range(columns-3):
if Matrix[Row][Col] == Matrix[Row][Col + 1] == Matrix[Row][Col + 2] == Matrix[Row][Col + 3]: # left and right
return Matrix[Row][Col]
elif Matrix[Row][Col] == Matrix[Row + 1][Col] == Matrix[Row + 2][Col] == Matrix[Row + 3][Col]: # up and down
return Matrix[Row][Col]
elif Matrix[Row][Col] == Matrix[Row + 1][Col - 1] == Matrix[Row + 2][Col - 2] == Matrix[Row + 3][Col - 3]:
return Matrix[Row][Col]
elif Matrix[Row][Col] == Matrix[Row + 1][Col + 1] == Matrix[Row + 2][Col + 2] == Matrix[Row + 3][Col + 3]:
return Matrix[Row][Col]
print(WhoWon(Matrix))```