'Do you know any way to shorten 4 functions in Python?
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".
Solution 1:[1]
First of all you just don't need to duplicate with "O". The condition basically checks that all cells have the same value - regardless of if it's X or O. Just check if they are all the same. If they are, print that repeating value.
Further, you always check consecutive indexes, so you can use a loop instead of many ands.
Finally, one way to check if multiple items are identical, is using a set:
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
The same can be applied to the other direction-functions as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
