当变量设置为 False 时,为什么 Tic Tac Toe 游戏中的 while 循环不结束?

Why doesn't while-loop in Tic Tac Toe game end when variable is set to False?

我开始学习在线课程Python,课程的第一个里程碑是为井字游戏编写脚本。当我尝试在 while 循环中同时设置所有函数时出现问题。

井字游戏应该 运行 在 while 循环 运行ning 时停止

game_onTrue 时 while 循环 运行,当 game_onFalse 时应该中断,但它永远不会中断!

谢谢!

#Create a tic tac toe game for two players. 
#ROADMAP: display board->player assignment->update board with player's choice->keep playing if win condition not met

#For testing
#board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']

#DISPLAY
from typing import Counter


def display_game(board): #can I write the print statement in one line? I coud add ,'\n', inbetween each line
    print((board[7] + '|' + board[8] + '|' + board[9]))
    print((board[4] + '|' + board[5] + '|' + board[6]))
    print((board[1] + '|' + board[2] + '|' + board[3]))

#PLAYER 1 and PLAYER 2 ASSIGNMENT
def player_assign():
    marker=''
    while marker != 'X' and marker != 'O':
        marker = input('Player 1, select a character X or O: ')
        if marker not in ('X','O'):
            print('Invalid choice you dumb head!!!')
        player1 = marker
    if player1=='X':
        player2='O'
    else:
        player2='X'

#MARKER PALCE CHOICE
def player1_choice():
    position1 = 'Wrong1'
    while position1 not in ['1','2','3','4','5','6','7','8','9']: #can use it with list(range(0,9))
        position1=input('Player 1, choose a position to place marker: ')
        if position1 not in ['1','2','3','4','5','6','7','8','9']:
            print("Invalid choice, dick sucker")
    return int(position1)
def player2_choice():
    position2 = 'Wrong2'
    while position2 not in ['1','2','3','4','5','6','7','8','9']:
        position2=input('Player 2, choose a position to place marker: ')
        if position2 not in ['1','2','3','4','5','6','7','8','9']:
            print("Invalid choice Luigi")
    return int(position2)

#CHOICE REPLACEMENT
def marker_replacement1(board,position1):
    board[position1]='X'
    return board

def marker_replacement2(board,position2):
    board[position2]='O'
    return board
    
#WIN CONDITION
#player 1 wins
def winner_player1(board):
    #Check horizonatl
    if board[1:4]=='X' or board[4:7]=='X' or board[7:10]=='X':
        print('Player 1 WINS!')
        return False
    #check vertical1
    elif board[1]==board[4]==board[7]=='X' or board[2]==board[5]==board[8]=='X' or board[3]==board[6]==board[9]=='X':
        print('Player 1 WINS!')
        return False
    #check diagonal1
    elif board[1]==board[5]==board[9]=='X' or board[3]==board[5]==board[7]=='X':
        print('Player 1 WINS!')
        return False
    else:
        return True #this means there is no winner yet beacuse we will assign this variable to game_on
#player 2 wins
def winner_player2(board):
    #Check horizontal2
    if board[1:4]=='O' or board[4:7]=='O' or board[7:10]=='O':
        print('Player 2 WINS!')
        return False
    #check vertical2
    elif board[1]==board[4]==board[7]=='O' or board[2]==board[5]==board[8]=='O' or board[3]==board[6]==board[9]=='O':
        print('Player 2 WINS!')
        return False
    #check diagonal12
    elif board[1]==board[5]==board[9]=='O' or board[3]==board[5]==board[7]=='O':
        print('Player 2 WINS!')
        return False
    else:
        return True #this means there is no winner yet beacuse we will assign this variable to game_on



#put all together
game_on = True
board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']
counter=0
player_assign()
while game_on: #OBS: game_on = False, the while loop doesn't break, Why?
    #Displays the game
    display_game(board) 
    #Player1 Position choice
    position1 = player1_choice() 
    #rewrite position 1 and update game list
    marker_replacement1(board,position1)
    #display the updated game
    display_game(board)
    #keep playing?
    if ' ' not in board:
        game_on = False
        print("It's a TIE")
    game_on=winner_player1(board)
    print(game_on)
    #Player 2 Position choice
    position2 = player2_choice()
    #rewrite position 2 and update game list
    marker_replacement2(board,position2)
    #display the updated game
    display_game(board)
    #keep playing?
    if ' ' not in board:
        game_on=False
        print("It's a TIE")
    game_on=winner_player2(board)
    print(game_on)

当您在代码中编写 game_on = False 时,紧跟 行会显示

game_on=winner_player2(board)

game_on=winner_player1(board)

表示 game_on = False 立即被覆盖。请注意,至少在第二次调用 game_on = False 时,您有:

if ' ' not in board:
    game_on=False
    print("It's a TIE")
game_on=winner_player2(board)
print(game_on)

然后循环重新开始,所以你可以这样做

if ' ' not in board:
    print("It's a TIE")
    break

注意:break 会立即退出 while 循环,因此请确保在调用所有其他相关代码后调用它。也有可能你的意思是写

if ' ' not in board:
    game_on=False
    print("It's a TIE")
else:
    game_on=winner_player2(board)