简体   繁体   中英

Why won't my while loop break in Tictactoe?

I am writing code to a basic tictactoe game, but I am having trouble with a certain part of my code when trying to get it to break out of the while loop.

I have a list (= board) that contains 10 items = ' space '. The fullBoardCheck function should return a boolean = True when the condition is met, otherwise False.

Within initializeGame, the while loop is set to continue while gameStatus = False. However, once I run the game and fill out 9 spaces of the Board, gameStatus changes to True but it does not break out of the loop.

I cannot find what I am missing, and would appreciate any help you could offer.

Thanks

def placeMarkerO(board, position):
    board[position] = 'O'
    return board

def takeTurnO(board):
    print("It's O's turn!")
    wantedPosition = int(input('O, where would you like to go? (1-9): '))
    while not spaceCheck(board, wantedPosition):
        wantedPosition = int(input('This space is taken. Please choose an EMPTY space: '))
    return wantedPosition

def fullBoardCheck(board):
    # Returns true or false boolean checking if board is full
    return len([x for x in board if x == " "]) == 1

def initializeGame():
    board = [' '] * 10

    gameStatus = False

    while gameStatus == False:
        drawBoard(board)
        gameStatus = fullBoardCheck(board)
        position = takeTurnX(board)
        placeMarkerX(board, position)

        drawBoard(board)
        fullBoardCheck(board)
        position = takeTurnO(board)
        placeMarkerO(board, position)

You need to change the fullBoardCheck function:

def fullBoardCheck(board):
    return len([x for x in board if x == ' ']) == 0

You checked if the len([x for x in board if x == ' ']) == 1 ,
however, you need to check if the length is 0 because you want to return whether the board is completely full. Good Luck!

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