如何让用户在 Tic Tac Toe 中逐架选择飞机

我想让用户选择 Tic Tac Toe 中的棋盘有多大。


我不明白如何创建动态变量来检查玩家是否获胜。


我能想到的唯一可行的选择是对用户可以选择的每个可能的 x 进行 make if 语句。这就是我迄今为止制作电路板的内容。


x = int(input("How many x-by-x do you want?: "))

index = x**2

value = ["-"]*index



def board(index):

    for i in range(index):

        if i != 0:

            if (i % x) == 0:

                print()

        print("-", end=" ")


一只萌萌小番薯
浏览 104回答 1
1回答

翻阅古今

这个问题有点宽泛,但我会尽力为您指明正确的方向:首先,让用户选择他们的板尺寸相当简单。类似于以下内容的操作应该可行,您可以将变量分配给用户的输入,然后运行循环以创建棋盘大小的数组数组:board_size = int(input("How big do you want your tic-tac-toe board to be?"))board = []for row in range(board_size):  board.append([])  for column in range(board_size):    board[row].append([])给定输入 4,board 变量将等于:[[[], [], [], []],  [[], [], [], []],  [[], [], [], []],  [[], [], [], []]]但定义获胜条件有点困难。我们必须考虑您可能获胜的所有模式:横向三排、垂直三排以及对角线三排。由于我们的棋盘很好地保存为数组的数组,因此无论棋盘有多大,我们都可以制定通用规则来查看任意位置是否连续有 3 个“x”或 3 个“o”。水平行非常简单:只需检查一行中是否有三个“x”或“o”:for row in range(board_size):  for column in range(board_size-2):    if board[row][column] == board[row][column+1] == board[row][column+2]:      if board[row][column] == "x":        print("x wins!")      else:        print("o wins!")现在,要添加垂直行,我们必须执行相同的操作,但要遍历列然后行而不是行然后列:for column in range(board_size):  for row in range(board_size-2):    if board[row][column] == board[row+1][column] == board[row+2][column]:      if board[row][column] == "x":        print("x wins!")      else:        print("o wins!")最后是对角线。我们通过遍历每一行和每一列来做到这一点,但我们将它们都加 1,或者向行加 1,并从列中取 1。这使我们能够得到这种形状(行和列都增加)或那种形状(行增加和列减少):"x"                         "x"   "x"                   "x"      "x"             "x"要查找增加的行和增加的列:for row in range(board_size-2):  for column in range(board_size-2):    if board[row][column] == board[row+1][column+1] == board[row+2][column+2]:      if board[row][column] == "x":        print("x wins!")      else:        print("o wins!")并找到增加的行和减少的列:for row in range(board_size-2):  for column in range(board_size-2):    if board[row][column+2] == board[row+1][column+1] == board[row+2][column]:      if board[row][column] == "x":        print("x wins!")      else:        print("o wins!")当我们使用函数将所有这些组合在一起时,它看起来像这样:#user inputs preferred board sizeboard_size = int(input("How big do you want your tic-tac-toe board to be?"))board = []# we make the board arraysfor row in range(board_size):  board.append([])  for column in range(board_size):    board[row].append([])# returns who won the gamedef game_state(board):  # horizontal wins?  for row in range(board_size):    for column in range(board_size-2):      if board[row][column] == board[row][column+1] == board[row][column+2]:        if board[row][column] == "x":          return "x wins!"         else:          return "o wins!"     # vertical wins?  for column in range(board_size):    for row in range(board_size-2):      if board[row][column] == board[row+1][column] == board[row+2][column]:        if board[row][column] == "x":          return "x wins!"         else:          return "o wins!"   # diagonal wins (aigu)?  for row in range(board_size-2):    for column in range(board_size-2):      if board[row][column+2] == board[row+1][column+1] == board[row+2][column]:        if board[row][column] == "x":          return "x wins!"         else:          return "o wins!"     # diagonal wins (grave)?  for row in range(board_size-2):    for column in range(board_size-2):      if board[row][column] == board[row+1][column+1] == board[row+2][column+2]:        if board[row][column] == "x":          return "x wins!"        else:          return "o wins!"    return "the game is still in progress"'''Here's where your game would run. You would get people to play moves, and after each move, see if someone has won.Once the board is filled up with moves (when number of moves == board_size**2), \you will run game_state on the final position to see who won, or if it is a draw.Good luck!'''
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python