Is there any way to shorten these 4 functions in Python?
def right_check(board: List[List[dict]], rowid: int, colid: int) -> bool:
"""This function checks rows for 5 continuously symbols.
Args:
board (List[List[dict]]): The board
rowid (int): The row ID where the win function is currently at.
colid (int): The col ID where the win function is currently at.
Returns:
bool: True when function description applies
"""
try:
if board[rowid][colid]["val"] == "X" and board[rowid][colid+1]["val"] == "X" and board[rowid][colid+2]["val"] == "X" and board[rowid][colid+3]["val"] == "X" and board[rowid][colid+4]["val"] == "X":
print("X won")
return True
if board[rowid][colid]["val"] == "O" and board[rowid][colid+1]["val"] == "O" and board[rowid][colid+2]["val"] == "O" and board[rowid][colid+3]["val"] == "O" and board[rowid][colid+4]["val"] == "O":
print("O won")
return True
except IndexError:
return False
def down_check(board: List[List[dict]], rowid: int, colid: int) -> bool:
"""This function checks columns for 5 continuously symbols.
Args:
board (List[List[dict]]): The board
rowid (int): The row ID where the win function is currently at.
colid (int): The col ID where the win function is currently at.
Returns:
bool: True when function description applies
"""
try:
if board[rowid][colid]["val"] == "X" and board[rowid+1][colid]["val"] == "X" and board[rowid+2][colid]["val"] == "X" and board[rowid+3][colid]["val"] == "X" and board[rowid+4][colid]["val"] == "X":
print("X won")
return True
if board[rowid][colid]["val"] == "O" and board[rowid+1][colid]["val"] == "O" and board[rowid+2][colid]["val"] == "O" and board[rowid+3][colid]["val"] == "O" and board[rowid+4][colid]["val"] == "O":
print("O won")
return True
except IndexError:
return False
def down_right_check(board: List[List[dict]], rowid: int, colid: int) -> bool:
"""This function checks down-right direction for 5 continuously symbols.
Args:
board (List[List[dict]]): The board
rowid (int): The row ID where the win function is currently at.
colid (int): The col ID where the win function is currently at.
Returns:
bool: True when function description applies
"""
try:
if board[rowid][colid]["val"] == "X" and board[rowid+1][colid+1]["val"] == "X" and board[rowid+2][colid+2]["val"] == "X" and board[rowid+3][colid+3]["val"] == "X" and board[rowid+4][colid+4]["val"] == "X":
print("X won")
return True
if board[rowid][colid]["val"] == "O" and board[rowid+1][colid+1]["val"] == "O" and board[rowid+2][colid+2]["val"] == "O" and board[rowid+3][colid+3]["val"] == "O" and board[rowid+4][colid+4]["val"] == "O":
print("O won")
return True
except IndexError:
return False
def down_left_check(board: List[List[dict]], rowid: int, colid: int) -> bool:
"""This function checks down-left direction for 5 continuously symbols.
Args:
board (List[List[dict]]): The board
rowid (int): The row ID where the win function is currently at.
colid (int): The col ID where the win function is currently at.
Returns:
bool: True when function description applies
"""
try:
if board[rowid][colid]["val"] == "X" and board[rowid+1][colid-1]["val"] == "X" and board[rowid+2][colid-2]["val"] == "X" and board[rowid+3][colid-3]["val"] == "X" and board[rowid+4][colid-4]["val"] == "X":
print("X won")
return True
if board[rowid][colid]["val"] == "O" and board[rowid+1][colid-1]["val"] == "O" and board[rowid+2][colid-2]["val"] == "O" and board[rowid+3][colid-3]["val"] == "O" and board[rowid+4][colid-4]["val"] == "O":
print("O won")
return True
except IndexError:
return False
I'm mostly interested in shortening the colid+1 +2 +3, rowid+1 +2 +3 part, and not having to duplicate every if with value "O".