Reputation: 1
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
Upvotes: 0
Views: 421
Reputation: 2146
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:
Upvotes: 2