Search code examples
syntax-errorsyntax-highlightingtic-tac-toe

Tic-tac-toe computer in Python, invalid syntax problem


I'm just getting started learning Python. I was told this would be a good beginner project. But I'm running into an invalid syntax problem on line 32 here. I'm not getting any specifics, but the problem didn't start until I created the list on line 7. My previous attempt was from line 12 to 20 and 25 to 28, which printed just fine. But that would have only been for the placement of moves on the visible board. I still needed a way to track whether or not the square is occupied, and how it affects the computer's bias toward unoccupied squares. But it seems I'm doing something wrong here.

Even the syntax highlighting between lines 31 and 32 is different, so whatever the problem is, it's triggering on line 32 (picture below). Can someone help me understand the problem?

import copy

def main():
    
    # Board squares list. String is either blank, O, or X. Int is adjusted bias.
    # Bool is whether or not the square is occupied, affects bias int.
    sq = [[" ", 0, False], [" ", 0, False], [" ", 0, False], 
    [" ", 0, False], [" ", 0, False], [" ", 0, False], 
    [" ", 0, False], [" ", 0, False], [" ", 0, False]]
    
    # DEBUG: Attempting to condense square attributes into a list.
    #sq_1 = " "
    #sq_2 = " "
    #sq_3 = " "
    #sq_4 = " "
    #sq_5 = " "
    #sq_6 = " "
    #sq_7 = " "
    #sq_8 = " "
    #sq_9 = " "
    
    # Board print, blank spaces and lines.
    board_blank = ["   |   |   " + "\n", "___|___|___" + "\n"]

    # DEBUG: Attempting to read squares from new list
    #board1 = " " + sq_1 + " | " + sq_2 + " | " + sq_3 + " " + "\n"
    #board2 = " " +  sq_4 + " | " + sq_5 + " | " + sq_6 + " " + "\n"
    #board3 = " " + sq_7 + " | " + sq_8 + " | " + sq_9 + " " + "\n"

    # Board print, updates with occupied squares after each move
    board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
    board2 = " " +  sq[3][0] + " | " + sq[4][0] + " | " + sq[5][0] + " " + "\n"
    board3 = " " + sq[6][0] + " | " + sq[7][0] + " | " + sq[8][0] + " " + "\n"

    # TEST: print board
    print_board(board1, board2, board3, board_blank)  

# Board printing function
def print_board(board1, board2, board3, board_blank):
    print((" " + board_blank[0]), board1, board_blank[1], board_blank[0], board2, 
    board_blank[1], board_blank[0], board3, board_blank[0])

# Acquire player's next move
def player_move():
    pass

# Calculate and execute computer's move
def comp_move():
    pass

main()

enter image description here

enter image description here


Solution

  • You are missing a closing square bracket in line 31:

    board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2[0] + " " + "\n"
                                                             ^
    

    Replace the line with the following:

    board1 = " " + sq[0][0] + " | " + sq[1][0] + " | " + sq[2][0] + " " + "\n"
                                                             ^