I'm currently working on writing a script for a Tic Tac Toe game but am running into one issue. While my winning statements are working, instantly declaring whether a player has one, my draw statement is not. When filling the last empty slot that would result in a draw, it instead acts as if it has made an ordinary move, and only on the subsequent move declares a draw. For clarification, the board is a list of lists. My code is as follows.
def move(self, row, col, user):
"""Places the users move on the board"""
# Checks if the move is invalid and notifies the user
if row not in range(3) and column not in range(3) and\
self._board[row][col] == "x" and "o" or\
self._current_state != "UNFINISHED":
return False
# Checks if any legal moves allowed and if so, places user on board
elif self._board[row][col] != "x" and "o":
if self._board[0][col] != user and self._board[1][col]\
!= player and self._board[2][col] != user or \
self._board[row][0] != user and self._board[row][1] != user\
and self._board[row][2] != user or self._board[0][0] != user\
and self._board[1][1] != user and self._board[2][2] != user\
or self._board[0][2] != user and self._board[2][0] != user\
and self._board[1][1] != user:
self._current_state = "UNFINISHED"
self._board[row][col] = user
# Checks for vertical wins and updates _current_state
elif self._board[0][col] == user and self._board[1][col] == user\
and self._board[2][col] == user:
self._current_state = user.upper() + "_WON"
return True
# Checks for horizontal wins and updates _current_state
elif self._board[row][0] == player and self._board[row][1] == user \
and self._board[row][2] == user:
self._current_state = user.upper() + "_WON"
return True
# Checks for diagonal wins and updates _current_state
elif self._board[0][0] == user and self._board[1][1] == user\
and self._board[2][2] == user or self._board[0][2] == user\
and self._board[2][0] == user and self._board[1][1] == user:
self._current_state = user.upper() + "_WON"
return True
# Checks if the board is full with no wins and declares game a draw
else:
self._current_state = "DRAW"
return True
return True
Aucun commentaire:
Enregistrer un commentaire