我不知道如何创建代码的某个部分,而不需要过于冗长和乏味。在我需要我的elif语句时,是否有一种使用范围函数的方法?任何帮助缩短这将是非常感谢!
#just some sample lists
Player.PlayerList[1].carrier_row = [0,1,2,3,4]
Player.PlayerList[1].carrier_col = [5,5,5,5,5]
Player.PlayerList[1].battleship_row = [10,11,12,13]
Player.PlayerList[1].battleship_col = [15,15,15,15]
if Player.PlayerList[1].carrier_row[0] == column:
if Player.PlayerList[1].carrier_col[0] == row:
print('hit')
elif Player.PlayerList[1].carrier_row[1] == column:
if Player.PlayerList[1].carrier_col[1] == row:
print('hit')
elif Player.PlayerList[1].carrier_row[2] == column:
if Player.PlayerList[1].carrier_col[2] == row:
print('hit')
elif Player.PlayerList[1].carrier_row[3] == column:
if Player.PlayerList[1].carrier_col[3] == row:
print('hit')
elif Player.PlayerList[1].carrier_row[4] == column:
if Player.PlayerList[1].carrier_col[4] == row:
print('hit')
elif Player.PlayerList[1].battleship_row[0] == column:
if Player.PlayerList[1].battleship_col[0] == row:
print('hit')
elif Player.PlayerList[1].battleship_row[1] == column:
if Player.PlayerList[1].battleship_col[1] == row:
print('hit')
elif Player.PlayerList[1].battleship_row[2] == column:
if Player.PlayerList[1].battleship_col[2] == row:
print('hit')
elif Player.PlayerList[1].battleship_row[3] == column:
if Player.PlayerList[1].battleship_col[3] == row:
print('hit')
else:
print('miss')发布于 2022-05-13 14:28:06
老实说,我认为数据格式是不合适的。如果您有能力更改它,我建议您重新设计数据模型以处理点,而不是单个点坐标(x, y),例如:
Player.PlayerList[1].carrier = [(0, 5), (1, 5), (2, 5), (3, 5), (4,5)]那么,您对命中的检查可以非常简单,如下所示:
def is_hit(row: int, column: int) -> bool:
point = (row, column)
return (point in Player.PlayerList[1].carrier
or point in Player.PlayerList[1].battleship)编辑:假设您试图构建一个战舰游戏,我可能会编写一个类Ship来管理一艘船,然后使用其中的多个,所以如下所示:
class Point(NamedTuple):
x: int
y: int
class Ship(object):
def __init__(self, points: List[Point]):
self.points = points
def check_hit(self, point: Point) -> bool:
return point in self.points这样做的好处是,您现在可以轻松地开始向Ship添加逻辑,例如添加一个.hits属性,它允许您跟踪ship的运行情况。
然后,玩家可以简单地列出一份船只清单:
Player.PlayerList[1].ships = [
Ship([Point(0, 5), Point(1, 5), Point(2, 5), Point(3, 5), Point(4,5)],
Ship(...),
...
]发布于 2022-05-13 14:16:41
您可以在循环中这样做,以查看所有的可能性,而不必全部编写:
Player.PlayerList[1].carrier_row = [0,1,2,3,4]
Player.PlayerList[1].carrier_col = [5,5,5,5,5]
Player.PlayerList[1].battleship_row = [10,11,12,13]
Player.PlayerList[1].battleship_col = [15,15,15,15]
hit = False
for i in range(len(Player.PlayerList[1].carrier_row)):
if Player.PlayerList[1].carrier_row[i] == row and Player.PlayerList[1].carrier_col == column:
print("hit")
hit = True
for i in range(len(Player.PlayerList[1].battleship_row)):
if Player.PlayerList[1].battleship_row[i] == row and Player.PlayerList[1].battleship_col == column:
print("hit")
hit = True
if not hit:
print("miss")发布于 2022-05-13 14:34:47
由于行和列的值以及类定义是未知的,这更简洁,但没有经过测试:
Player.PlayerList[1].carrier_row = [0,1,2,3,4]
Player.PlayerList[1].carrier_col = [5,5,5,5,5]
Player.PlayerList[1].battleship_row = [10,11,12,13]
Player.PlayerList[1].battleship_col = [15,15,15,15]
pli = Player.PlayerList[1]
for cr, cc, br, bc in zip(pli.carrier_row, pli.carrier_col, pli.battleship_row, pli.battleship_col):
if row in (cr, br) or column in (cc, bc):
hit = True
break
else:
hit = False
print('hit' if hit else 'miss')https://stackoverflow.com/questions/72230928
复制相似问题