I keep getting errors around this section, I can't move the block, can someone help me fix it? [closed]
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 hours ago.
Improve this questioncode error KeyError: 2
num is 2. the dict numberPosReverse doesn't have a key of '2' But I don't know how to fix it. I'm not sure if the question should appear on numberPosReverse[num] or where.
def number_move(number_pos, move_input, score_list):
values = list(number_pos.values())
keys = list(number_pos.keys())
numberPosReverse = dict(zip(values, keys))
newScorelist = score_list.copy()
oldScorelist = {}
while newScorelist != oldScorelist:
oldScorelist = newScorelist.copy()
for num in range(1, 17):
pos = eval(numberPosReverse[num])
x, y = pos[0] + move_input[0], pos[1] + move_input[1]
pos[0] = [x if 1 <= x <= 4 else pos[0]][0]
pos[1] = [y if 1 <= y开发者_Go百科 <= 4 else pos[1]][0]
number = number_pos['{}'.format(pos)]
oldNumberscore = newScorelist[num]
nextNumberscore = newScorelist[number]
syn = list(map(lambda x, y: abs(x) * abs(y), move_input, pos))
# zero value movement
if nextNumberscore == 0:
newScorelist[number] = oldNumberscore
newScorelist[num] = 0
# no movement
elif num == number:
pass
# merge movement
elif oldNumberscore == nextNumberscore and num != number:
newScorelist[number] = 2 * oldNumberscore
newScorelist[num] = 0
# boundary movement
elif oldNumberscore != nextNumberscore and 1 in syn or 4 not in syn:
pass
# non-boundary movement
elif oldNumberscore != nextNumberscore and 1 not in syn and 4 not in syn:
x, y = pos[0] + move_input[0], pos[1] + move_input[1]
next2Numberscore = newScorelist[number_pos['{}'.format([x, y])]]
if next2Numberscore != nextNumberscore:
pass
elif next2Numberscore == nextNumberscore:
newScorelist[number_pos['{}'.format([x, y])]] = 2 * next2Numberscore
newScorelist[number] = oldNumberscore
newScorelist[num] = 0
return newScorelist
精彩评论