我是 Python 新手,并试图在简单的胸部游戏中编写一个验证函数来实现:
一块应该移动它的完全延伸(这意味着在它的移动方向上,如B7:E4,另一块F4被阻挡,这是合法的移动)
def validate_move_rule(from_position, to_position, position_hash):
blocker_position = []
blocker_position.extend(position_hash)
if(abs((int(to_position[1]) - int(from_position[1]))) == abs(ord(to_position[0]) - ord(from_position[0]))):
if((int(to_position[1]) - int(from_position[1])) > 0 and (ord(to_position[0]) - ord(from_position[0])) > 0):
next_position = str_plus(to_position[0],1) + str(int(to_position[1]) + 1)
if(next_position in blocker_position):
return True
elif((int(to_position[1]) - int(from_position[1])) < 0 and (ord(to_position[0]) - ord(from_position[0])) < 0):
next_position = str_plus(to_position[0],-1) + str(int(to_position[1]) - 1)
if(next_position in blocker_position):
return True
# left_up
elif((int(to_position[1]) - int(from_position[1])) > 0 and (ord(to_position[0]) - ord(from_position[0])) < 0):
next_position = str_plus(to_position[0],1) + str(int(to_position[1]) - 1)
if(next_position in blocker_position):
return True
#left_down
elif((int(to_position[1]) - int(from_position[1])) < 0 and (ord(to_position[0]) - ord(from_position[0])) > 0):
next_position = str_plus(to_position[0], -1) + str(int(to_position[1]) + 1)
if(next_position in blocker_position):
return True
#right_up
elif(from_position[1] == to_position[1]):
if((ord(to_position[0]) - ord(from_position[0])) > 0):
next_position = str_plus(to_position[0],1) + to_position[1]
if(next_position in blocker_position):
return True
#right
elif((ord(to_position[0]) - ord(from_position[0])) < 0):
next_position = str_plus(to_position[0],-1) + to_position[1]
if(next_position in blocker_position):
return True
#left
elif(from_position[0] == to_position[0]):
if((int(to_position[1]) - int(from_position[1])) > 0):
next_position = to_position[0] + str(int(to_position[1]) + 1)
if(next_position in blocker_position):
return True
#down
elif((int(to_position[1]) - int(from_position[1])) < 0):
next_position = to_position[0] + str(int(to_position[1]) - 1)
if(next_position in blocker_position):
return True
#up
else:
puts("Error: it's not a legal move(hint: must diagonal horizontal vertical)")
return False
我发现我的代码真的很乏味,我认为 Python 应该有一些功能来简化我的代码,有什么建议吗?