我如何让玩家循环直到他们做出有效选择?

How do I loop a players turn until they make a valid choice?

我目前正在尝试编写一个 connect 4 游戏,我遇到了一个错误,如果玩家 1 进行了无效移动,出于某种原因它将继续到玩家 2 回合。我想知道是否有一种方法可以循环代码,直到玩家 1 进行有效转弯,然后照常进行到玩家 2。

我尝试了一个 while 循环来检查轮到谁了,但是它陷入了一个持续的循环并崩溃了。我附上了我尝试如何使其工作的代码。

我正在使用 Pygame 作为程序的图形部分。

编辑:我将代码还原为原始代码,但使用了简单的 while 循环。我还附上了 is_valid() 和 main().

# Graphical representation of dropping the piece
def draw_drop_piece(event, turn):
    game_over = False
    while turn == 0:  # Checks to See if its Player 1 Turn
        posx = event.pos[0]
        col = int(math.floor(posx / SQUARESIZE))

        if is_valid(board, col):  # Checks Every Turn if Where the Player is Putting Their Piece is Valid
            row = next_open_row(board, col)
            drop_piece(board, row, col, 1)

            if winning_move(board, 1):  # Checks Every Turn if the Player Won
                label = FONT.render("PLAYER 1 WINS!", 1, RED)
                screen.blit(label, (40, 10))
                game_over = True
                break
            break

        else:
            label = FONT.render("Enter Valid Spot", 1, BLUE)
            screen.blit(label, (40, 10))

    while turn != 0:
        posx = event.pos[0]
        col = int(math.floor(posx / SQUARESIZE))

        if is_valid(board, col):  # Checks Every Turn if Where the Player is Putting Their Piece is Valid
            row = next_open_row(board, col)
            drop_piece(board, row, col, 2)

            if winning_move(board, 2):  # Checks Every Turn if the Player Won
                label = FONT.render("PLAYER 2 WINS!", 1, YELLOW)
                screen.blit(label, (40, 10))
                game_over = True
                break
            break

        else:
            label = FONT.render("Enter Valid Spot", 1, BLUE)
            screen.blit(label, (40, 10))

    return game_over

def is_valid(board, col):
    return board[ROW_COUNT - 1][col] == 0
if __name__ == '__main__':
    pygame.init()
    board = create_board()  # Creates Board
    print_board(board)  # Prints Board
    game_over = False  # The Game Isn't Over as it Just Started
    turn = 0  # Player One Starts First

    FONT = pygame.font.SysFont("monospace", 75)

    width = COLUMN_COUNT * SQUARESIZE
    height = (ROW_COUNT + 1) * SQUARESIZE
    size = (width, height)

    screen = pygame.display.set_mode(size)
    draw_board(board)
    pygame.display.update()

    while not game_over:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # Exiting the game
                sys.exit()

            track_mouse()

            if event.type == pygame.MOUSEBUTTONDOWN:
                pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))

                game_over = draw_drop_piece(turn)

                print_board(board)
                draw_board(board)

                # This makes sure the turn indicator alternates between 1 and 0
                turn = (turn + 1) % 2

                if game_over:
                    pygame.time.wait(5000)

尝试像我在下面做的那样构建一些东西。这样,在 while 循环中,您可以通过声明 player2 为 false 来禁用与其他玩家有关的任何事情。

玩家 1 = 真

player2 = True

而玩家 1:

player2 = False

您的函数需要能够 return 三个 值,而不仅仅是当前的两个 return。它需要发出信号表示游戏是否获胜,移动后是否仍在进行中,或者移动是否无效且回合尚未完成。

我不确定这是否是最好的方法,但是如果您添加 returning None 而不是 TrueFalse 函数可能会起作用调用代码的特殊情况逻辑:

game_over = draw_drop_piece(turn)

print_board(board)
draw_board(board)

if game_over is not None:             # don't change turns if the attempted move was invalid
    turn = (turn + 1) % 2

while 循环实际上不需要更改,因为 None 是假的,就像实际的 False 值一样。

这是 draw_drop_piece 中需要的更改:

if is_valid(board, col):
    ... # this part all stays the same

else:
    label = FONT.render("Enter Valid Spot", 1, BLUE)
    screen.blit(label, (40, 10))
    game_over = None                 # signal an invalid move