井字游戏错误(越界错误)

时间:2019-12-01 00:03:52

标签: python python-3.x class

我在我的一个课程中提交的TicTacToe Python程序有问题。这是错误:

Try out of bounds move and x win
Test Failed: 'O_WON' != 'X_WON'
- O_WON
? ^
+ X_WON
? ^

我通过了所有其他测试。我通过了尝试在中心位置单动,尝试不匹配的对角线行,尝试移至占领方格并赢了,尝试一次完成两行并尝试平局。我不确定自己做错了什么。以下是说明:

它应该有一个名为make_move的方法,该方法需要三个参数,一个行和一个列(按顺序),其中每个参数都是0-2范围内的整数,并且用'x'或'o'表示玩家谁在采取行动。如果行或列超出范围,或者该正方形已被占用,或者游戏已经获胜或绘制,则make_move应该返回False。否则,它应该记录移动,将current_state更新为适当的值,然后返回True。 current_state的可能值为:“ X_WON”,“ O_WON”,“ DRAW”或“ UNFINISHED”。同一位玩家可以连续进行多次移动。当所有方格都填满,但没有一个玩家获胜时,便开始游戏。

这是我的全部代码。该错误意味着由于测试参数,平地机期望使用“ X_WON”,但是我的程序返回了“ O_WON”。您能帮我解决它吗?

class TicTacToe:

"""
Represents the TicTacToe game's board and current state.
"""
def __init__(self):
    """
    Initializes the board to a list of three lists that each contain three empty strings and 
    initializes the current_state to "UNFINISHED".
    """
    self.__board = [[' '] * 3 for row in range(3)]

    self.__current_state = 'UNFINISHED'

def get_current_state(self):
    """
    A get method that returns the value of 'X' or 'O' in whichever empty string depending on
    the integers each player entered for the row and column.
    """
    return self.__current_state

def make_move(self, row, col, symbol):
    """
    This method calculates how the 'x' and 'o' players make their move whenever they choose which 
    integer (0-2) to enter for the row and column. When an "x" or "o" player makes enough successful 
    moves and wins the game, the value will return either as "X_WON" or "O_WON". If the row or 
    column are out of bounds, or if that square is already occupied, or if the game has already been 
    won or drawn, make_move should return False. Otherwise, it should record the move, update 
    current_state to the appropriate value, and return True.
    """
    if 0 <= row <= 2 and 0 <= col <= 2:
        if self.__board[row][col] == ' ': # sets up the board
            self.__board[row][col] = symbol # each player makes their move with an 'X' or an 'O'
            self.__check_winner() # calculates who the winner is
            return True

        else:
            return False
    else:
        return False

def __check_winner(self):
    """
    This additional method checks all the rows and columns then calculates whichever moves Players 1 
    and 2 make. Whoever makes the correct amount of moves first vertically, horizontally, or 
    diagonally is the winner. If nobody wins, then it's a draw.
    """
    if self.__board[0][0] == self.__board[0][1] == self.__board[0][2] and self.__board[0][0] is not ' ':
        self.__current_state = 'X_WON' if self.__board[0][0] == 'X' else 'O_WON' # First type of successful move
        return
    if self.__board[1][0] == self.__board[1][1] == self.__board[1][2] and self.__board[0][0] is not ' ':
        self.__current_state = 'X_WON' if self.__board[1][0] == 'X' else 'O_WON' # Second type of successful move
        return
    if self.__board[2][0] == self.__board[2][1] == self.__board[2][2] and self.__board[2][0] is not ' ':
        self.__current_state = 'X_WON' if self.__board[2][0] == 'X' else 'O_WON' # Third type of successful move
        return
    if self.__board[0][0] == self.__board[1][0] == self.__board[2][0] and self.__board[0][0] is not ' ':
        self.__current_state = 'X_WON' if self.__board[0][0] == 'X' else 'O_WON' # Fourth type of successful move
        return
    if self.__board[0][1] == self.__board[1][1] == self.__board[2][1] and self.__board[0][1] is not ' ':
        self.__current_state = 'X_WON' if self.__board[0][1] == 'X' else 'O_WON' # Fifth type of successful move
        return
    if self.__board[0][2] == self.__board[1][2] == self.__board[2][2] and self.__board[0][2] is not ' ':
        self.__current_state = 'X_WON' if self.__board[0][2] == 'X' else 'O_WON' # Sixth type of successful move
        return
    if self.__board[0][0] == self.__board[1][1] == self.__board[2][2] and self.__board[0][0] is not ' ':
        self.__current_state = 'X_WON' if self.__board[0][0] == 'X' else 'O_WON' # Seventh type of successful move
        return
    if self.__board[0][2] == self.__board[1][1] == self.__board[2][0] and self.__board[2][0] is not ' ':
        self.__current_state = 'X_WON' if self.__board[2][0] == 'X' else 'O_WON' # Eighth type of successful move
        return

    for row in range(3):
        for col in range(3):
            if self.__board[row][col] == ' ':
                self.__current_state = 'UNFINISHED' # the game hast just begun
                return

            self.__current_state = 'DRAW' # the game ends in a tie

# ALL OF THE METHODS BELOW ARE MY TESTING METHODS!

   def printBoard(self):
       """
       A useful method that prints out the board as a way for testing and debugging.
       """
       print(' | '.join(self.__board[0]))
       print('-'*9)
       print(' | '.join(self.__board[1]))
       print('-' * 9)
       print(' | '.join(self.__board[2]))
       # prints out the layout of the board with its three rows and three columns

# game.make_move(0,0,'X')
def main():
    """
    Let the games begin!
    """
    game = TicTacToe()
    game.printBoard()
    while game.get_current_state() =='UNFINISHED':
        x=o=0
        while True:
            x = int(input('Player 1 - Enter row [0-2]: ')) # Player 1 goes first
            o = int(input('Player 1 - Enter col [0-2]: '))
            if game.make_move(x,o,'X'): # if the move was successful, then Player 2 goes next
                break
            else: # if the space in the row and column is already occupied, then the movie is invalid
                print('Your move was invalid!') # if the move was invalid, then Player 1 goes again
        game.printBoard()
        if game.get_current_state()=='X_WON' or game.get_current_state()=='DRAW':
            break

        while True:
            x = int(input('Player 2 - Enter row [0-2]: ')) # Player 2's turn
            o = int(input('Player 2 - Enter col [0-2]: '))
            if game.make_move(x,o,'O'): # if the move was successful, then Player 1 goes next
                break
            else: # if the space in the row and column is already occupied, then the movie is invalid
                print('Your move was invalid!') # if the move was invalid, then Player 2 goes again
        game.printBoard()
        if game.get_current_state()=='O_WON' or game.get_current_state()=='DRAW':
            break

    if game.get_current_state()=='DRAW':
        print('It is a DRAW!') # nobody wins
    elif game.get_current_state()=='X_WON':
        print('Player 1 Wins') # Player 1 wins
    else:
        print('Player 2 Wins') # Player 2 wins

main()

1 个答案:

答案 0 :(得分:0)

我认为您在检查第二个条件时有误

if self.__board[1][0] == self.__board[1][1] == self.__board[1][2] and self.__board[0][0] is not ' ':

我认为应该这样(区别在于and之后)

if self.__board[1][0] == self.__board[1][1] == self.__board[1][2] and self.__board[1][0] is not ' ':

尽管对测试内容一无所知,但很难说这是否引起了问题。