If Statement Error (Tic Tac Toe)

ale.of.ginger at gmail.com ale.of.ginger at gmail.com
Tue Nov 1 15:55:30 EST 2005


Greetings!  I am trying to make a multiplayer (no AI, 2 person) game of
tic tac toe in Python.  So far it has been pretty simple.  My only
concern is with the win checking to see if a person has won.  At first
it looked like it was working, but now it sometimes assigns a win when
you enter an X or O (doesn't matter) on certain tiles (row 1, column 1
won't be an error, but row 2, column 3 will be...).  If you can find
the problem, I'd be very thankful!  Here's the code:

# TIC TAC TOE
# Started: 10/31/05
# Ended:  still in progress

loop = 1

while loop == 1:
    print "TIC TAC TOE"
    print "1 - Play Multiplayer"
    print "2 - Quit"
    option = input("> ")

    if option == 2:
        # QUIT
        loop = 0

    if option == 1:
        # MAIN GAME LOOP
        print "Rules:  You will alternate turns."
        print "On your turn, you can place your letter (O = Player 1 or
X = Player 2)",
        print "in any unoccupied square."
        print "The first to get 3 in a row wins.  Good luck!"

        gameboard = [' ',' ',' ',' ',' ',' ',' ',' ',' ']

        win = 0
        turnnumber = 0

        while win != 1:
            if turnnumber % 2 == 0:
                print " "
                print "Player 1"
                print " "
                print
"[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]"
                print
"[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]"
                print
"[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]"
                print "What row?"
                row = input("> ")
                print "What column?"
                column = input("> ")

                if (row > 3 or row < 1) or (column > 3 or column < 1):
                    print "Exceeeded limits."
                    turnnumber = turnnumber - 1

                if row == 1 and column == 1:
                    if gameboard[0] != ('O' or 'X'):
                        gameboard[0] = ('O')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 2 and column == 1:
                    if gameboard[3] != ('O' or 'X'):
                        gameboard[3] = ('O')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1
                if row == 3 and column == 1:
                    if gameboard[6] != ('O' or 'X'):
                        gameboard[6] = ('O')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 1 and column == 2:
                    if gameboard[1] != ('O' or 'X'):
                        gameboard[1] = ('O')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 2 and column == 2:
                    if gameboard[4] != ('O' or 'X'):
                        gameboard[4] = ('O')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 3 and column == 2:
                    if gameboard[7] != ('O' or 'X'):
                        gameboard[7] = ('O')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 1 and column == 3:
                    if gameboard[2] != ('O' or 'X'):
                        gameboard[2] = ('O')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 2 and column == 3:
                    if gameboard[5] != ('O' or 'X'):
                        gameboard[5] = ('O')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 3 and column == 3:
                    if gameboard[8] != ('O' or 'X'):
                        gameboard[8] = ('O')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                turnnumber = turnnumber + 1

            if (((gameboard[0] and gameboard[1] and gameboard[2]) ==
'O') or ((gameboard[3] and gameboard[4] and gameboard[5]) == 'O') or
((gameboard[6] and gameboard[7] and gameboard[8]) == 'O')\
            or ((gameboard[0] and gameboard[3] and gameboard[6]) ==
'O') or ((gameboard[1] and gameboard[4] and gameboard[7]) == 'O') or
((gameboard[2] and gameboard[5] and gameboard[8]) == 'O')\
            or ((gameboard[0] and gameboard[4] and gameboard[8]) ==
'O') or ((gameboard[2] and gameboard[4] and gameboard[6]) == 'O')):
                print "Player 1 wins!"
                win = 1

            if ((gameboard[0:9]) == ('O' or 'X')) and (win == 0):
                print "Tie."
                win = 1

            if turnnumber % 2 == 1 and win != 1:
                print " "
                print "Player 2"
                print " "
                print
"[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]"
                print
"[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]"
                print
"[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]"
                print "What row?"
                row = input("> ")
                print "What column?"
                column = input("> ")

                if (row > 3 or row < 1) or (column > 3 or column < 1):
                    print "Exceeeded limits."
                    turnnumber = turnnumber - 1
                if row == 1 and column == 1:
                    if gameboard[0] != ('O' or 'X'):
                        gameboard[0] = ('X')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 2 and column == 1:
                    if gameboard[3] != ('O' or 'X'):
                        gameboard[3] = ('X')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 3 and column == 1:
                    if gameboard[6] != ('O' or 'X'):
                        gameboard[6] = ('X')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 1 and column == 2:
                    if gameboard[1] != ('O' or 'X'):
                        gameboard[1] = ('X')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 2 and column == 2:
                    if gameboard[4] != ('O' or 'X'):
                        gameboard[4] = ('X')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 3 and column == 2:
                    if gameboard[7] != ('O' or 'X'):
                        gameboard[7] = ('X')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 1 and column == 3:
                    if gameboard[2] != ('O' or 'X'):
                        gameboard[2] = ('X')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 2 and column == 3:
                    if gameboard[5] != ('O' or 'X'):
                        gameboard[5] = ('X')
                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                if row == 3 and column == 3:
                    if gameboard[8] != ('O' or 'X'):
                        gameboard[8] = ('X')

                    else:
                        print "This cell is already filled."
                        turnnumber = turnnumber - 1

                turnnumber = turnnumber + 1

            if (((gameboard[0] and gameboard[1] and gameboard[2]) ==
'X') or ((gameboard[3] and gameboard[4] and gameboard[5]) == 'X') or
((gameboard[6] and gameboard[7] and gameboard[8]) == 'X')\
            or ((gameboard[0] and gameboard[3] and gameboard[6]) ==
'X') or ((gameboard[1] and gameboard[4] and gameboard[7]) == 'X') or
((gameboard[2] and gameboard[5] and gameboard[8]) == 'X')\
            or ((gameboard[0] and gameboard[4] and gameboard[8]) ==
'X') or ((gameboard[2] and gameboard[4] and gameboard[6]) == 'X')):
                print "Player 2 wins!"
                win = 1




More information about the Python-list mailing list