【问题标题】:Simple Tic-Tac-Toe in python 2.7python 2.7中的简单井字游戏
【发布时间】:2013-11-19 07:15:24
【问题描述】:

我正在尝试为班级项目创建一个简单的井字游戏,但我不确定如何完成其​​余代码。对于我如何完成剩下的工作,我真的很感激任何见解或意见。该代码目前在技术上可以工作,但它所做的只是显示一个充满none 和“比赛打成平手”的板。

PLAYERS = ["X", "O"]

def display_board(board):
        print board[0][0:3]
        print board[1][0:3]
        print board[2][0:3]

def create_empty_board():
        return [[None, None, None], [None, None, None], [None, None, None]]

def board_is_full(board):
        for row in board:
                if None not in board:
                        return True


def winner(board):
        if board[0][0] == board[0][1] == board[0][2] != None:
                return board[0][0]
        elif board[1][0] == board[1][1] == board[1][2] != None:
                return board[1][0]
        elif board[2][0] == board[2][1] == board[2][2] != None:
                return board[2][0]
        elif board[0][0] == board[1][0] == board[2][0] != None:
                return board[0][0]
        elif board[0][1] == board[1][1] == board[2][1] != None:
                return board[0][1]
        elif board[0][2] == board[1][2] == board[2][2] != None:
                return board[0][2]
        elif board[0][0] == board[1][1] == board[2][2] != None:
                return board[0][0]
        else:
                return None

def game_over(board):
        if board_is_full(board) == True:
                return True
def player_turn(board, playerid):
    """ Ask the player to select a coordinates for their next move. The player needs to select a row and a column. If the coordinates the player selects are outside of the board, or are already occupied, they need to be prompted to select coordinates again, until their input is valid."""
    return 1, 1 # by default, return row 1, column 1 as the player's desired location on the board; you need to implement this

def play():
    """ This is the main function that implements a hot seat version of Tic Tac Toe."""
    # the code below is just an example of how you could structure your play() function
    # if you implement all the functions above correctly, this function will work
    # however, feel free to change it if you want to organize your code differently
    board = create_empty_board()
    display_board(board)
    current_player = 0
    while not game_over(board):
        board = player_turn(board, PLAYERS[current_player])
        current_player = (current_player + 1) % len(PLAYERS)
        display_board(board)
    who_won = winner(board)
    if who_won is None:
        print "The game was a tie."
    else:
        print "The winning player is", who_won

if __name__ == "__main__":
    play()

【问题讨论】:

  • 附带说明,当每行有 3 个成员时,board[0][0:3]board[0] 相同。
  • 请不要编辑您的代码来删除您最初询问的所有问题。这使得问题和答案对任何发现它的人都毫无用处。我已经回滚了。
  • 同时,如果您有新问题,请接受答案,或者编写并接受您自己的答案,然后为新问题创建一个新问题(在两者之间粘贴链接,以便人们知道他们是连接的)。不要一直将此问题更改为原始问题的后续问题。
  • 附带说明,在您尝试发布的新代码中,if " " not in board[0] and board[1] and board[2]: 并不代表您的想法或任何有用的信息。特别是,它并不意味着if (" " not in board[0]) and (" " not in board[1]) and (" " not in board[2]):。你要么必须明确地写出来,要么使用 anyall 函数和理解,或者使用循环语句。

标签: python python-2.7 tic-tac-toe


【解决方案1】:

您的第一个问题是您的board_is_full 错误。对于每一行,不是检查而是检查None not in board。这总是正确的; board 是三个列表的列表,所以 None 永远不会在其中。您要检查每一行,而不是整个板。但只是 if None not in row 也不正确——只要 any 行已满,而不是所有行,这将是正确的。所以当你找到一个非完整的行时你想返回False,如果你没有找到任何结果就返回True。你应该可以自己做。

你的下一个问题是你还没有实现player_turn。对于一个非常 hacky 的实现,你可以使用return input('row, col: ')。除非你能理解它为什么起作用,否则你不应该把它作为你的功课(如果你确实了解它为什么起作用,你就不会想要使用它)。此外,它也不是很友好——它不会告诉你轮到谁了,也不会向你展示棋盘或任何东西。你把这些东西作为参数,所以你可以使用它们。无论如何,单线已经足以让你暂时解决下一个问题,所以你可以稍后再回来。

您的下一个问题是player_turn 返回一对数字,而您只是将这对数字分配给board。你不能那样做。你想要的是这样的:

row, col = player_turn(board, PLAYERS[current_player])
board[row][col] = PLAYERS[current_player]

即使有人已经赢了,游戏也会让你一直玩到棋盘满为止。但实际上,如果董事会已满,有赢家,你想完成。 (事实上​​,理想情况下,您希望在不可能获胜时尽快结束,但这有点复杂。您只需将前面的句子翻译成您的 while 语句的替换即可完成简单的事情。)

这应该可以让您找到任何其他小错误,编写适当的player_turn 函数,寻找简化现有代码的方法,等等。

【讨论】:

  • 我编写了更多代码,我现在唯一的问题是如何设置一个循环,如果它输入的坐标超出了棋盘的范围,它会不断提示用户?
  • @user2843355:这应该是一个新问题。但作为提示:你的player_turn 函数可以有一个while True,在循环内部,获取输入,然后if 有效,return 它。
猜你喜欢
  • 2013-12-21
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
相关资源
最近更新 更多