2

我是 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 应该有一些功能来简化我的代码,有什么建议吗?

4

2 回答 2

4
  • 使用变量来表示位置之间的差异
  • 将 blocker_position 检查移至底部
  • 你不需要 blocker_position
  • 仅在函数的开头或结尾使用 int()/str()
  • 在中间部分只计算 x 和 y 的变化量,而不是它们的值

像这样:

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
于 2012-10-11T06:02:56.703 回答
0

想想一个小布尔值表。有效或无效。

于 2012-10-11T15:25:15.087 回答