gameloop中函数输出的python页面未计算

2024-09-30 10:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是,在调用gameloop中的函数check_win之后, 即使函数的计算结果为false(我已经检查过了,所以这是 case),我将这个假值赋给循环中的gamePlaying, 然而,游戏循环gamePlaying条件仍然计算为true 继续前进

def check_win(): #function checks for three in a row across, down, and diagonals
        for x in range (0, 3) :
            y = x*3
            if (board[y] == board[(y + 1)] and board[y] == board[(y + 2)]):
                gamePlaying = False
            else:
                gamePlaying = True
            if (board[x] == board[(x + 3)] and board[x] == board[(x + 6)]):
                gamePlaying = False
            else:
                gamePlaying = True
            if((board[0] == board[4] and board[0] == board[8]) or 
                (board[2] == board[4] and board[4] == board[6])):
                gamePlaying = False
            else:
                gamePlaying = True
        return(gamePlaying)

    currentPlayer = [first_player,second_player] #creates list to iterate over turns
    gamePlaying = True #bool for gameloop

    while gamePlaying: #main game loop
        for i in currentPlayer: #iterates over current player to switch turns

            draw_board()
            place_move = int(input(first_move + ' what is your move? ')) #inputs then places move for first_player
            board[place_move] = first_player
            gamePlaying = check_win() #should take bool output of check_win and store to cont or end gameloop

            draw_board()
            place_move = int(input(second_move + ' what is your move? ')) #inputs then places move for second_player
            board[place_move] = second_player
            gamePlaying = Check_win() #should take bool output of check_win and store to cont or end gameloop

Tags: andtoinboardtrueformovecheck
1条回答
网友
1楼 · 发布于 2024-09-30 10:34:41

问题是,当您打算使用elif时,您正在使用if语句。见docs

但是,您可能希望在这些点上执行return False,因为这将允许函数提前退出,并且您必须担心gamePlaying会被稍后的if语句设置回True

相关问题 更多 >

    热门问题