2

My question is about the second break statement: if x == xstart and y == ystart: break

So this break statement breaks out of the while true loop correct? But then does it go back to the next closest loop (namely the while board[x][y] = OtherTile:) But because this condition is not met, it goes back to the original for xdirection, ydirection loop? I just wanted to see if my understanding was correct.

if board[xstart][ystart] not OnBoard(xstart, ystart) or board[xstart][ystart] != ' ':
    return False

#temporarily set the tile on the board, but change it back to a blank before the end of this function
board[xstart][ystart] = tile

#Set computer tile
if tile == 'X':
    OtherTile = 'O'
else:
    OtherTile = 'X'

#empty list of tiles to flip
TilesToFlip = []

#this is a for loop for two variables.  Each list of two represents a position away from the orignal spot
for xdirection, ydirection in [[1,0], [0,1], [-1,0], [0,-1], [1,-1], [1,1], [-1,1], [-1,-1]]:

    #we set x and y to the original coordinates passed to us because we want to preserve the orignal values
    x, y = xstart, ystart
    x = x + xdirection
    y = y + ydirection

    #after the first iteration, check to see if the adjacent piece is on the board and if it's the OtherTile:
    if isOnBoard(x,y) and board[x][y] == OtherTile:
        x = x + xdirection
        y = y + ydirection
        #if the next piece is not on the board, go back to the for loop to test another direction
        if not isOnBoard(x,y):
            continue
        while board[x][y] = OtherTile:
            x = x + xdirection
            y = y + ydirection
            #we break here because if we had just continued it would have gone back to the while loop.
            #since we're breaking, it goes back to the original for loop (if it goes off the board)
            if not isOnBoard(x,y):
                break

        #it finishes with the while loop if it reaches a tile that is not the OtherTile (i.e. it's blank or it's the player's)
        #so we check to see if it's the player's
        if board[x][y] == tile:
            #if it is the player's tile, then we go in the reverse direction, appending each tile to the TilesToFlip list
            while True:
                x = x - xdirection
                y = y - ydirection
                #when we reach the original tiles, we break (by then we have all the tiles that need to be flipped in store in the new list)
                if x == xstart and y == ystart:
                    break
                TilesToFlip.append([x,y])

1 Answer 1

2

It will only break the while True loop. The loop will continue forever until x == xstart and y == ystart.

Once that condition is met, it will continue on in the that outer for loop.

Sign up to request clarification or add additional context in comments.

3 Comments

Right, but I was wondering what does it do after breaking from the while True loop? Does it go back and try the while board[x][y] = OtherTile: loop again? But since it's not true, it goes back to the first loop (for xdirection, ydirection )?
When that while True loop breaks, it will do the next iteration of the for loop, and continue on from there. If you want it to try the while board[x][y] = OtherTile: loop again, you need to indent everything starting from "#it finishes with the while loop..."
In general, when a loop is "break-ed"-out-of, it continues at the next instruction following the end of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.