-2

我有不同的情况 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.')
4

1 回答 1

0

您必须检查对板单元的任何分配是否会改变其先前的值。如果是这样,那么isValid应该设置为True

我还建议您减少代码,因为目前它对于四个不同方向的代码非常相似。使方向成为您在进一步逻辑中使用的变量。

例如:

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 in "wasd":
            dx, dy = {
                "a": (-1, 0),
                "w": (0, -1),
                "s": (0, 1),
                "d": (1, 0) 
            }[choice]
            isValid = False
            for line in range(4):
                sourcerow = targetrow = [0, line, 3][dy + 1]
                sourcecol = targetcol = [0, line, 3][dx + 1]
                for steps in range(3):
                    sourcerow -= dy
                    sourcecol -= dx
                    source = game_board[sourcerow][sourcecol]
                    if source:
                        target = game_board[targetrow][targetcol]
                        if source == target:
                            game_board[sourcerow][sourcecol] = 0
                            game_board[targetrow][targetcol] *= 2
                            targetrow -= dy
                            targetcol -= dx
                            isValid = True
                        else:
                            if target:
                                targetrow -= dy
                                targetcol -= dx
                            if (targetrow, targetcol) != (sourcerow, sourcecol):
                                game_board[sourcerow][sourcecol] = 0
                                game_board[targetrow][targetcol] = source
                                isValid = True
        elif choice == 'q':
            print('Goodbye')
            break
        else:
            print('Invalid input.')
于 2021-11-15T22:04:09.657 回答