dimanche 20 janvier 2019

While loop returns back to previous if statement?

I am writing TicTacToe game and I am setting up a control for a draw.

However I don't understand why while loop after getting answer "wrong" answer, if player wants a replay, is returning to first replay question "Do you want to play again? yes/no1 : "?

I thought that after getting answer on question: replay = input("You have not answered, please write yes or no 2.") the game should continue to ask X to place a sign.

Why/how can while loop return to "Do you want to play again? yes/no1 : " and ask again?

If I haven't clarified the question please run the game by placing X and O repeatedly on the same place until you exhaust counter (counts moves).

def tictactoe():
    #graphical interface
    def gi():
        print('{0:>10} | {1:>10} | {2:>10}'.format(1,2,3))
        print('{0:^10} | {1:^10} | {2:^10}'.format(listik[1],listik[2],listik[3]))
        print('{0:^10} | {0:^10} | {0:^10}'.format(''))
        print('{0:-^10} | {0:-^10} | {0:-^10}'.format(''))
        print('{0:>10} | {1:>10} | {2:>10}'.format(4,5,6))
        print('{0:^10} | {1:^10} | {2:^10}'.format(listik[4],listik[5],listik[6]))
        print('{0:^10} | {0:^10} | {0:^10}'.format(''))
        print('{0:-^10} | {0:-^10} | {0:-^10}'.format(''))
        print('{0:>10} | {1:>10} | {2:>10}'.format(7,8,9))
        print('{0:^10} | {1:^10} | {2:^10}'.format(listik[7],listik[8],listik[9]))
        print('{0:^10} | {0:^10} | {0:^10}'.format(''))

    #game logic
    a = ''
    b = ''
    c = ''
    d = ''
    e = ''
    f = ''
    g = ''
    h = ''
    i = ''
    z = 'Z'
    listik = [z,a,b,c,d,e,f,g,h,i]

    space = '\n' * 3
    gameison = True
    counter = 0
    replay = ''
    print('TicTacToe game, place your sign by choosing from 1 to 9, 10 breaks the game and you can overwrite your foe\'s sign!')
    gi()

    while gameison:
        print(space)
    #win
        if ['X', 'X', 'X'] in (listik[1:4], listik[4:7], listik[7:10], listik[1::3], listik[2::3], listik[3::3], listik[1::4], listik[3:8:2]):
            print(f'GAME OVER, X has won! Number of moves: {counter}')
            gameison = False
            #place replay here

        elif ['O', 'O', 'O'] in (listik[1:4], listik[4:7], listik[7:10], listik[1::3], listik[2::3], listik[3::3], listik[1::4], listik[3:8:2]):
            print(f'GAME OVER, O has won! Number of moves: {counter}')
            gameison = False
            #place replay here

    #draw and replay
        elif counter >= 9:
            print('DRAW')
            gi()
            replay = (input("Do you want to play again? yes/no1 : "))
            if replay == 'yes':
                print("\n\n\n\nHere we go!")
                tictactoe()
            elif replay == 'no':
                print('Goodbye1')
                break
            else:
                while (replay != 'yes') and (replay != 'no'):
                    replay = input("You have not answered, please write yes or no 2.")
                    print(replay, '<        replay is on the left')
                    print("while loop condition is:",(replay != 'yes') and (replay != 'no'))

        elif counter % 2 == 1:
            choice = (int(input('Place your choice X:  ')))
            if choice == 10:
                break
            elif choice <=0 or choice >=11:
                while choice not in range(1,11):
                    print("place your choice correctly please")
                    gi()
                    break
            else:
                listik[choice] = 'X'
                counter += 1
                gi()          
        elif counter % 2 == 0:
            choice = (int(input('Place your choice O:  ')))

            if choice == 10:
                break
            elif choice <=0 or choice >=11:
                while choice not in range(1,11):
                    while choice not in range(1,11):
                        print("place your choice correctly!")
                        gi()
                        break
            else:
                listik[choice] = 'O'
                counter += 1
                gi()

    if counter < 9:
        again = input("Do you want to play again? yes/no3 : ")
        if again == 'yes':
            tictactoe()
        else:
            print('Goodbye2')





Aucun commentaire:

Enregistrer un commentaire