Does python have a better control structure or other feature to simplify the code?
-
06-07-2021 - |
Вопрос
I'm new to Python and trying to write a validate function in the simple chest game to achieve:
the a piece should move it's full extend (which mean in it's moving direction like B7:E4, have a another piece F4 blocked, which is a legal move )
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
I found my code is really tedious, I thought Python should have some feature simplify my code, Any suggestion ?
Решение
- use variables for the difference between the positions
- move the blocker_position check to the bottom
- you don't need blocker_position
- only use int()/str() at the beginning or end of the function
- in the middle section only figure out how much x and y change, not their value
Like so:
def validate_move_rule(from_position, to_position, position_hash):
to_pos_x = ord(to_position[0])
to_pos_y = int(to_position[1])
delta_pos_x = to_pos_x - ord(from_position[0]) # x
delta_pos_y = to_pos_y - int(from_position[1]) # y
delta_x = 0
delta_y = 0
if abs(delta_pos_x) == abs(delta_pos_y):
# diagonal
delta_x = 1 if delta_pos_x > 0 else -1
delta_y = 1 if delta_pos_y > 0 else -1
elif from_position[1] == to_position[1]:
# horizontal
delta_x = 1 if delta_pos_x > 0 else -1
elif from_position[0] == to_position[0]:
# vertical
delta_y = 1 if delta_pos_y > 0 else -1
else:
puts("Error: it's not a legal move(hint: must diagonal horizontal vertical)")
return False
new_position = chr(to_pos_x + delta_x) + str(to_pos_y + delta_y)
return new_position in position_hash
Другие советы
Think about a little table of booleans. Valid or not valid.
Не связан с StackOverflow