I'm making a function that checks a tic-tac-toe board for any wins. This is done by brute-forcing through the list seeing if the rows, diagonals, or columns share the same letters:
def check_board(board):
if (board[0] + board[1] + board[2]) == "XXX" or # Top row
(board[3] + board[4] + board[5]) == "XXX" or # Middle row
(board[6] + board[7] + board[8]) == "XXX" or # Bottom row
(board[0] + board[4] + board[8]) == "XXX" or # Left-to-right diagonal
(board[2] + board[4] + board[6]) == "XXX" or # Right-to-left diagonal
(board[0] + board[3] + board[6]) == "XXX" or # Left column
(board[1] + board[4] + board[7]) == "XXX" or # Middle column
(board[2] + board[5] + board[8]) == "XXX": # Right column
return "X" # Win for X
if (board[0] + board[1] + board[2]) == "OOO" or # Top row
(board[3] + board[4] + board[5]) == "OOO" or # Middle row
(board[6] + board[7] + board[8]) == "OOO" or # Bottom row
(board[0] + board[4] + board[8]) == "OOO" or # Left-to-right diagonal
(board[2] + board[4] + board[6]) == "OOO" or # Right-to-left diagonal
(board[0] + board[3] + board[6]) == "OOO" or # Left column
(board[1] + board[4] + board[7]) == "OOO" or # Middle column
(board[2] + board[5] + board[8]) == "OOO": # Right column
return "O" # Win for O
else:
return None # No one has won yet
Even when surrounding each comparison with ()
, the same error still persists:
Traceback (most recent call last):
File "python", line 16
if (board[0] + board[1] + board[2]) == "XXX" or # Top row
^
SyntaxError: invalid syntax
Input is directly put in as a list of strings (which allows the concatenating of strings within the if
statements). So what is incorrect with my code? Is it my usage of or
or my interpreter not processing comments correctly? Try it here
Aucun commentaire:
Enregistrer un commentaire