在Python中有任何方法来缩短这4个函数吗?
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我最感兴趣的是缩短colid+1 +2 +3、rowid+1 +2 +3部分,而不必重复每个if值为"O“的部分。
发布于 2022-01-27 18:41:56
首先,您只是不需要重复使用"O“。条件基本上检查所有的单元格都有相同的值--不管是X还是O,只要检查它们是否都一样。如果是的话,打印这个重复的值。
此外,您总是检查连续的索引,因此可以使用一个循环而不是许多and。
最后,实现check if multiple items are identical的一种方法是使用一个集合:
def right_check(board, rowid, colid):
try:
if len(set(board[rowid][colid+i]["val"] for i in range(5))) == 1:
print(f"{board[rowid][colid]["val"]} won")
return True
except IndexError:
return False同样的也适用于其他方向的函数。
https://stackoverflow.com/questions/70883587
复制相似问题