Hey I am having trouble with a tic tac toe game and I could really use some help. My if statement(line 51) is underlined, says its invalid syntax, and I cant fix it no matter what I do. Here's my code: http://pastebin.com/UtVTz4gg

Recommended Answers

All 3 Replies

That is because "! =" does not equal "!=" (original code copied for reference).

    #-------------------------------------------------------------------------------
    # Name:        module1
    # Purpose:
    #
    # Author:      User
    #
    # Created:     18/12/2012
    # Copyright:   (c) User 2012
    # Licence:     <your licence>
    #-------------------------------------------------------------------------------
    #tictactoe board

    import random

    board = [0,1,2,
             3,4,5,
             6,7,8]

    def game():
        print (board[0],'|',board[1],'|',board[2])
        print ('----------')
        print (board[3],'|',board[4],'|',board[5])
        print ('----------')
        print (board[6],'|',board[7],'|',board[8])

    def check(char, spot1, spot2, spot3):
        if board[spot1] == char and board[spot2] == char and board[spot3] == char:
            return True
    def checkAll (char):
        if check (char, 0, 1, 2):
            return True
        if check (char, 1, 4, 7):
            return True
        if check (char, 2, 5, 8):
            return True

        if check (char, 6, 7, 8):
            return True
        if check (char, 3, 4, 5):
            return True
        if check (char, 0, 1, 2):
            return True

        if check (char, 2, 4, 6):
            return True
        if check (char, 0, 4, 8):
            return True
    while True:
        p1 = input("Player 1, where do you want to place your marker?")
        p1 = int(p1)
        if str(board[p1]) ! = 'x' and str(board[p1]) ! = 'o':
            board[p1] = 'x'

    if checkAll('x') == True:
                print "Player 1 wins!"
                break
            break

    while True:
            p2 = input("Player 2, where would you like to place your marker?")
            p2 = int(p2)
            if str(board[p2]) ! = 'x' and str(board[p2]) ! = 'o':
                str(board[p2]) = 'o'

                if checkAll('o') == True:
                    print "Player 2 wins!"
                    break
            break

I made some additional changes to help you out and reduced the code in checkAll() by using a list, but the program is still not ready yet as some testing should show you. Also, think about using a function to get the input instead of duplicating the code for each person i.e. everything under the "while True"s.

def game(board):
    print (board[0],'|',board[1],'|',board[2])
    print ('----------')
    print (board[3],'|',board[4],'|',board[5])
    print ('----------')
    print (board[6],'|',board[7],'|',board[8])

def check(char, spot1, spot2, spot3):
    if board[spot1] == char and board[spot2] == char and board[spot3] == char:
        return True
    return False

def checkAll (char):
    for spot1, spot2, spot3 in [[0, 1, 2],
                                [3, 4, 5],
                                [6, 7, 8],
                                [0, 3, 6],
                                [1, 4, 7],
                                [2, 5, 8],
                                [2, 4, 6],
                                [0, 4, 8]]:
        if check(char, spot1, spot2, spot3):
            return True

    return False

##------------------------------------------------------------------
board = [0,1,2,
         3,4,5,
         6,7,8]

## use a variable instead of while True because you can exit the loop
## by setting "winner" to True
winner = False
while not winner:

    while True:     ## loop until unoccupied square found
        p1 = input("Player 1, where do you want to place your marker?")
        p1 = int(p1)
        if 0 <= p1 <= 8:
            if str(board[p1]) != 'x' and str(board[p1]) != 'o':
                board[p1] = 'x'
                game(board)
                break
            else:
                print "That square is occupied"

    if checkAll('x') == True:
            print "Player 1 wins!"
            winner = True

    while True:
        p2 = input("Player 2, where would you like to place your marker?")
        p2 = int(p2)
        if 0 <= p2 <= 8:
            if str(board[p2]) != 'x' and str(board[p2]) != 'o':
                board[p2] = 'o'
                game(board)
                break
            else:
                print "That square is occupied"

    if checkAll('o') == True:
                print "Player 2 wins!"
                winner = True

oh wow thanks i didnt get an email saying i got a reply so i got a little worried :s I barely had to do anything to get it to work properly now i just have to make it ask if you want to play again, add rem statements and hand it in :)

What happens when there is a tie? When X wins, the program still asks O for a square. What happens if someone enters the letter A?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.