Search code examples
python-2.7if-statementsyntax-errortic-tac-toe

Error in Tic tac toe Program (Python)


The code shown below is that of a Tic tac toe game in which there are two players (Player1 and Player2 are humans). I have an issue in the if else statements in the "# Player 1 Plays now" and "# Player 2 Plays now" sections of the code. To be precise, the computer always thinks that every box is already filled with some value other than 1,2,3,4,5,6,7,8,9 and it keeps displaying the message "That cell is already marked. Please try another cell" defined in the nested else statement. Can somebody enlighten me how to fix this problem ?

board = [0,1,2,3,4,5,6,7,8,9]
print type(board[1])
def board_invoke():
print "| ",board[1], " | ", board[2], " | ", board[3], " | ", "\n", "-------------------", "\n", "| ", board[4], " | ",board[5], " | ", board[6], " | ", "\n", "-------------------", "\n", "| ",  board[7], " | ",board[8], " | ", board[9], " | "

def game_start():
Player1= raw_input("Select Player 1 between X or O : ")
if Player1 not in ('X','x','O','o'):
    print "That is not an expected player"
    game_start()
else:
    print "\nSince you have selected Player 1 as %s" %Player1
    print "\nThe Player 2 is assigned :",
    if Player1 in ('X','x'):
        Player2 = ("O")
    else:
        Player2 = ("X")
    print Player2
    print "\nThe game Starts now....\n"
    board_invoke()
    while (1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9) in board:

        # Winning Condition
        if board[1]==board[2]==board[3]==Player1:
            print Player1," wins"
            break
        elif board[4]==board[5]==board[6]==Player1:
            print Player1, " wins"
            break
        elif board[7]==board[8]==board[9]==Player1:
            print Player1, " wins"
            break
        elif board[1]==board[2]==board[3]==Player2:
            print Player2, " wins"
            break
        elif board[4]==board[5]==board[6]==Player2:
            print Player2, " wins"
            break
        elif board[7]==board[8]==board[9]==Player2:
            print Player2, " wins"
            break
        elif board[1]==board[5]==board[9]==Player1:
            print Player1, " wins"
            break
        elif board[3]==board[5]==board[7]==Player1:
            print Player1, " wins"
            break
        elif board[1]==board[5]==board[9]==Player2:
            print Player2, " wins"
            break
        elif board[3]==board[5]==board[7]==Player2:
            print Player2, " wins"
            break
        elif board[1]==board[4]==board[7]==Player1:
            print Player1, " wins"
            break
        elif board[2]==board[5]==board[8]==Player1:
            print Player1, " wins"
            break
        elif board[3]==board[6]==board[9]==Player1:
            print Player1, " wins"
            break
        elif board[1]==board[4]==board[7]==Player2:
            print Player2, " wins"
            break
        elif board[2]==board[5]==board[8]==Player2:
            print Player2, " wins"
            break
        elif board[3]==board[6]==board[9]==Player2:
            print Player2, " wins"
            break

        # Player 1 Plays now

        Cell_no = raw_input("Player 1 : Please select a number you want to mark....")
        Cell_no = int(Cell_no)

        if Cell_no not in board:
            print "Please enter a cell number within the scope of available cells"
        else:
            if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
                print "That cell is already marked. Please try another cell"
                continue
            else:
                board[Cell_no] = Player1
                board_invoke()

        # Player 2 Plays now

        Cell_no = raw_input("Player 2 : Please select a number you want to mark....")
        Cell_no = int(Cell_no)

        if Cell_no not in board:
            print "Please enter a cell number within the scope of available cells"
        else:
            if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
                print "That cell is already marked. Please try another cell"
                continue
            else:
                board[Cell_no] = Player2
                board_invoke()

    print "Do you want to play again ?"
    user_decision = raw_input("Please type Yes or No : ")
    if user_decision == ('YES' or 'Yes' or 'yes'):
        game_start()
    else:
        print "Ok. I take it that we will wrap up !"
        print "See you again !"



game_start()

Solution

  • Your if statement is not completely correct. You are evaluating:

    else:
    if 'X' or 'x' or 'O' or 'o' in board[Cell_no]:
        print "That cell is already marked. Please try another cell"
    

    which is always true. The or keywords combines each comparison logically. The first compare statement would then simply be 'X', which is not null or 0 and therefor always true. As the first statement is true, the whole if-statement is true.

    I see this quite often on people starting to learn programming. If-statements can't be written exactly like human sentences. You have to compare every single character to your variable. Here is a simple (not python specific and quite ugly) solution to your problem:

    else:
     if 'X' == board[Cell_no] or 'x' == board[Cell_no] or 'O' == board[Cell_no] or 'o' == board[Cell_no]:
          print "That cell is already marked. Please try another cell"
    

    An even better approach though would be to turn that check around and compare it with a list:

    else:
     if board[Cell_no] in ['X','x', 'O', 'o']:
          print "That cell is already marked. Please try another cell"