简体   繁体   中英

Stuck in infinite loop, cant figure out why - python

When this code is run, it should alternate between the player and the computer, but when the computer's turn comes, it just finishes the game. I have checked the indentation level repeatedly but I can't locate the issue.

The way you have written this makes it somewhat hard to really figure out how the control flow goes. There is a lot duplication that's not really necessary for the game which increases the complexity. One main thing is that you have four locations where you ask the player whether to restart the game or not. If you just make sure to have that once, you can already make everything easier by a large amount.

The first thing that you will notice then is that the structure is like this:

while winFlag == False:
    # player move
    while sticksOnBoard != 0:
        # evaluate winner
        # AI move
        # evaluate winner

So unless any player already wins by going below 0 sticks, the AI move will be happening over and over until someone won. But the player never gets the chance to move again. Instead, you probably want to remove your winFlag altogether since the game is over when there are no more sticks on the board. So you would just have it like this:

while sticksOnBoard > 0:
    # player move
    if sticksOnBoard <= 0:
        # evaluate winner
    # AI move
# evaluate winner

Or to remove the duplication of the evaluation too, you would just do switch the active player:

isPlayerMove = True
while sticksOnBoard > 0:
    if isPlayerMove:
        # player move
    else:
        # AI move
    isPlayerMove = not isPlayerMove # toggle current player
# evaluate winner

This is how I would make this game, reducing all duplication:

def playerVsAiGame (sticksOnBoard):
    # main game loop
    while True:
        sticks = sticksOnBoard
        isPlayerMove = True

        # only ask when there are more than 3 left; otherwise the current
        # player can just choose the remaining number and win
        while sticks > 3:
            print('There are {} sticks on the board.'.format(sticks))
            if isPlayerMove:
                move = int(input('How many sticks would you like to take? (1-3) '))
            else:
                # the computer just chooses randomly between 1, 2, and 3 sticks
                # this is where you could add additional game logic to make the AI
                # smarter
                move = random.choice((1, 2, 3))
                print('The computer takes {} sticks.'.format(move))

            sticks -= move
            isPlayerMove = not isPlayerMove

        # the current player wins
        print('There are {} sticks on the board.'.format(sticks))
        if isPlayerMove:
            print('You win!')
        else:
            print('The computer wins!')

        # break the main loop unless the player wants to play again
        if input('Play again? Yes (1) / No (0) ').lower() not in ('1', 'yes', 'y'):
            break

I think you get something very wrong in this piece of code.

You never request user input in your inner loop.

You check for sticksOnBoard being smaller than 0, as well as checking for it being 1. However, in most of the cases, sticksOnBoard will simply have a larger value and thus, while sticksOnBoard != 0 will loop until your game is finished.

In the meantime your basic AI will continuously draw sticks from the board, without the user ever doing anything.

It seems that you are not that familiar with programming (which is no problem, everyone once started). This is why I would recommend you to take pen and paper and go step by step through an example run of your function. That way it should become obvious why the program does not work as you want to.

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