1

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.

class TicTacToe:
"""Defines a Tic Tac Toe game"""

def get_current_state(self):
    """Returns the current state of the Tic-Tac-Toe game"""
    return self._current_state

def __init__(self):
    """Initiates a new TicTacToe game with a board and current state"""
    self._board = [["", "", ""], ["", "", ""], ["", "", ""]]
    self._current_state = "UNFINISHED"

def make_move(self, row, column, player):
    """Places the users move on the board"""

    # Checks if any legal moves allowed and if so, places player on board
    if self._board[0][column] != player and self._board[1][column]\
            != player and self._board[2][column] != player or \
            self._board[row][0] != player and self._board[row][1] != player\
            and self._board[row][2] != player or self._board[0][0] != player\
            and self._board[1][1] != player and self._board[2][2] != player\
            or self._board[0][2] != player and self._board[2][0] != player\
            and self._board[1][1] != player and self._current_state == "UNFINISHED":

        self._board[row][column] = player


        # Checks for vertical wins and updates _current_state
        if self._board[0][column] == player and self._board[1][column] == player \
                and self._board[2][column] == player:
            self._current_state = player.upper() + "_WON"
            return True

        # Checks for horizontal wins and updates _current_state
        elif self._board[row][0] == player and self._board[row][1] == player \
                and self._board[row][2] == player:
            self._current_state = player.upper() + "_WON"
            return True

        # Checks for diagonal wins and updates _current_state
        elif self._board[0][0] == player and self._board[1][1] == player\
            and self._board[2][2] == player or self._board[0][2] == player\
                and self._board[2][0] == player and self._board[1][1] == player:
            self._current_state = player.upper() + "_WON"
            return True


        # Checks if the board is full with no wins and declares game a draw
        elif "" not in self._board and self._current_state == "UNFINISHED":
            self._current_state = "DRAW"
            return True

        return True

    else:
        return False
  • 2
    `self._board[row][col] != "x" and "o"` isn't going to work, you need to state `self._board[row][col] != "x" and self_board[row][col] != "o" n.b. you have that in there more than once. – Rolf of Saxony Mar 03 '20 at 18:39
  • 2
    …or `self._board[row][col] not in {"x", "o"}` – Błotosmętek Mar 03 '20 at 18:40
  • 1
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – G. Anderson Mar 03 '20 at 18:48
  • @RolfofSaxony, Thank you for clarifying that. I went ahead and changed portion in the first if statement, however it still runs the same. I think I understand why, as when it inputs the first move, there is no "x" or "o" in self._board[row][col], but I can't figure out how to make it change it to a draw on that move as opposed to the next. – unarmeddeadguy Mar 03 '20 at 19:14
  • 1
    Without the full code I'd have to guess the rest, plus I'm logging out now. – Rolf of Saxony Mar 03 '20 at 19:23
  • @RolfofSaxony, I've made a few modifications, but now it's calling all moves a draw instead of keeping the status of "UNFINISHED". It is returning false when it's supposed to though. I'm guessing it's something wrong with my elif statement for a draw condition? – unarmeddeadguy Mar 04 '20 at 17:38
  • It would seem that you really want help with debugging rather than a coding problem. For that you will need to supply a runnable piece of code, not a fragment. I believe that there is a Stack Exchange website dedicated to code checking. – Rolf of Saxony Mar 04 '20 at 19:14

1 Answers1

0

I have had the same issue with 2-dimensional lists (that is what you call a list inside a list). Simply checking if "" is inside your entire board will not work, because your "board" list is full of lists. I suggest making code to check to see if every single tile on the board is covered.

CheetSheatOverlode
  • 121
  • 1
  • 1
  • 11