简体   繁体   中英

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

I started an online course to learn Python, the first milestone of the course is to write a script for a tic-tac-toe. The problem occurs when I try to set up all the funcions together inside a while loop.

The tic-tac-toe game should run when the while loop is running and stop when it breaks

The while loop run when game_on is True , and should break when game_on is False , but it never breaks!

Thank you!

#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)

When you write game_on = False in your code, the immediately following line says

game_on=winner_player2(board)

or

game_on=winner_player1(board)

which means that game_on = False is overwritten immediately. Notice that, at least for your second call to game_on = False you have:

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

and then the loop restarts, so you could just do

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

Be careful though: break exits the while loop immediately, so make sure you call it after all other relevant code has been called. It's also possible that you mean to write

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM