0

Having trouble with this tic-tac-toe script. For some reason my 'winner' function is causing an error. However I tweak the winner function, I get a syntax error on the line after the print statement. When I comment the winner function out the script runs as expected. I can't see where the syntax error is. Thanks.

def winner(board):
    if board[0][0]==board[0][1]==board[0][2] or board[1][0]==board[1][1]==board[1][2] or board[2][0]==board[2][1]==board[2][2] or board[0][0]==board[1][0]==board[2][0] or board[0][1]==board[1][1]==board[2][1] or board[0][2]==board[1][2]==board[2][2] or board[0][0]==board[1][1]==board[2][[2] or board[0][2]==board[1][1]==board[2][0]:
        print('Player {} wins!!!'.format(player))
        return False
    else:
        return True

def move(coord, player):
    marker = ' X '
    if player == 2:
        marker = ' O '
    if coord == '0,0':
        board[0][0] = marker
    elif coord == '0,1':
        board[0][1] = marker
    elif coord == '0,2':
        board[0][2] = marker
    elif coord == '1,0':
        board[1][0] = marker
    elif coord == '1,1':
        board[1][1] = marker
    elif coord == '1,2':
        board[1][2] = marker
    elif coord == '2,0':
        board[2][0] = marker
    elif coord == '2,1':
        board[2][1] = marker
    elif coord == '2,2':
        board[2][2] = marker

board = [[(0,0), (0,1), (0,2)],
        [(1,0), (1,1), (1,2)],
        [(2,0), (2,1), (2,2)]]

turn = 2
while True:
    print('\n'.join(map(str, board)))
    player = turn % 2 + 1
    x = input('Player {}, where will you play? (i.e. 0,0 for upper left) '.format(player))
    move(x,player)
    winner(board)
    turn += 1
1
  • 1
    Towards the end of line 2: board[0][0]==board[1][1]==board[2][[2]. There's an extra left square bracket there. Commented Jun 11, 2018 at 16:16

1 Answer 1

2

It's just a simple mistake, your code works: you had an extra [ in that part of the condition:

board[0][0]==board[1][1]==board[2][[2]

Remove the extra bracket and it will work as expected!

  • I noticed it because I copied the code to notepad++, and it highlights the matching bracket when standing on one of the brackets.
    I traveled through the condition with the arrow key and noticed it became red:

    enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Awesomesauce. Thanks. Funny that python was wrong about where the syntax error was.
Glad it helped! And welcome to StackOverflow! - Note that if you receive an answer that solved your problem for your satisfaction, it's recommended to mark the answer as the selected answer, and so the whole question will be marked as answered. If you also thought an answer was good (regardless if you choose to accept it), you can up vote it to show it was useful/helpful, or down vote if the opposite.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.