我有不同的情况 up , down , left 和 right 。有时选择无效,用户需要输入另一个选择。例如,我想将元素向上移动,但它们都已经放置好了,程序不应该生成新的 (2) 值,而是要求用户输入一个新的选择而不是向上。这是我的代码,它非常适合移动,但在选择无效时不起作用。有人可以帮忙吗??
isValid = True
while True:
if isValid:
one = generate_piece(game_board, DEV_MODE)
game_board[one['row']][one['column']] = one['value']
print_board(game_board)
if True:
choice = input()
if choice == 'w':
isValid = False
for col in range(4):
tracker1 = 0
tracker2 = 0
for row in range(4):
if game_board[row][col] == 0:
continue
if tracker2 == game_board[row][col]:
game_board[tracker1][col] = 2 * tracker2
tracker1 = tracker1 + 1
tracker2 = 0
elif tracker2 == 0:
tracker2 = game_board[row][col]
else:
game_board[tracker1][col] = tracker2
tracker1 = tracker1 + 1
tracker2 = game_board[row][col]
if tracker2 != 0:
game_board[tracker1][col] = tracker2
tracker1 = tracker1 + 1
for row in range(tracker1, 4):
game_board[row][col] = 0
isValid = True
elif choice == 's':
isValid = False
for col in range(4):
tracker1 = 3
tracker2 = 0
for row in range(3, -1, -1):
if game_board[row][col] == 0:
continue
if tracker2 == game_board[row][col]:
game_board[tracker1][col] = 2 * tracker2
tracker1 = tracker1 - 1
tracker2 = 0
elif tracker2 == 0:
tracker2 = game_board[row][col]
else:
game_board[tracker1][col] = tracker2
tracker1 = tracker1 - 1
tracker2 = game_board[row][col]
if tracker2 != 0:
game_board[tracker1][col] = tracker2
tracker1 = tracker1 - 1
for row in range(tracker1 + 1):
game_board[row][col] = 0
isValid = True
elif choice == 'd':
isValid = False
for row in range(4):
tracker1 = 3
tracker2 = 0
for col in range(3, -1, -1):
if game_board[row][col] == 0:
continue
if tracker2 == game_board[row][col]:
game_board[row][tracker1] = 2*tracker2
tracker1 = tracker1 - 1
tracker2 = 0
elif tracker2 == 0:
tracker2 = game_board[row][col]
else:
game_board[row][tracker1] = tracker2
tracker1 = tracker1 - 1
tracker2 = game_board[row][col]
if tracker2 != 0:
game_board[row][tracker1] = tracker2
tracker1 = tracker1 - 1
for col in range(tracker1 + 1):
game_board[row][col] = 0
isValid = True
elif choice == 'a':
isValid = False
for row in range(4):
tracker1 = 0
tracker2 = 0
for col in range(4):
if game_board[row][col] == 0:
continue
if tracker2 == game_board[row][col]:
game_board[row][tracker1] = 2 * tracker2
tracker1 = tracker1 + 1
tracker2 = 0
elif tracker2 == 0:
tracker2 = game_board[row][col]
else:
game_board[row][tracker1] = tracker2
tracker1 = tracker1 + 1
tracker2 = game_board[row][col]
if tracker2 != 0:
game_board[row][tracker1] = tracker2
tracker1 = tracker1 + 1
for col in range(tracker1, 4):
game_board[row][col] = 0
isValid = True
elif choice == 'q':
print('Goodbye')
break
else:
print('Invalid input.')