Проблемы с оператором if с Ti c Ta c Toe - PullRequest
1 голос
/ 03 марта 2020

В настоящее время я работаю над написанием сценария для игры Ti c Ta c Toe, но сталкиваюсь с одной проблемой. В то время как мои выигрышные заявления работают, сразу же объявляя, есть ли у игрока, мои, ничьи не делают. При заполнении последнего пустого слота, который может привести к ничьей, он вместо этого действует так, как если бы он сделал обычный ход, и только на последующем ходу объявляет ничью. Для пояснения на доске есть список списков. Мой код выглядит следующим образом.

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

1 Ответ

0 голосов
/ 05 марта 2020

У меня была такая же проблема с двумерными списками (это то, что вы называете списком внутри списка). Простая проверка, если "" находится внутри всей вашей доски, не будет работать, потому что ваш список "доски" полон списков. Я предлагаю сделать код, чтобы проверить, покрыты ли все плитки на доске.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...