我是使用Python的游戏机内战舰游戏,并且我有一个代码来检查飞船是否适合列表。它看起来不太吸引人,我想将其简化为更具吸引力的版本。
if (direction is "N" and row - size < 0) or \
(direction is "E" and column + size > 10) or \
(direction is "S" and row + size > 10) or \
(direction is "W" and column - size < 0):
print("It does not fit")
我当时在考虑使用字典,但没想到在内部使用变量和数学运算符(-+ <>)的方法
if any([direction is "N" and row - size < 0, direction is "E" and column + size > 10, direction is "S" and row + size > 10, direction is "W" and column - size < 0]):
print("It does not fit")
确保您的代码有很多改进,但这是一个开始
其他改进
if any([direction == "N" and size > row, direction == "E" and column + size > 10, direction == "S" and row + size > 10, direction == "W" and size > column]):