【问题标题】:tic tac toe does not change the player and does not win even if created condition井字游戏不会改变玩家,即使创造条件也不会获胜
【发布时间】:2020-02-13 23:01:30
【问题描述】:

我被要求用 Python 编写一个简单的井字游戏。

目前我有两个主要问题:

  1. 玩家符号不会从 X 变为 O,也不会从 O 变为 X(如 moveandturn 函数中所写)

  2. 即使我使用第一个选择的符号创建中奖条件,我的代码也无法识别中奖。(请参阅代码中的 win 函数)

对我来说看起来没问题,只是我不确定应该向我的 win 函数发送什么

代码:

def moveandturn(whoseturn,board):
    rowloc=int(input("Player, insert the deserved row to place your symbol"))
    coloc=int(input("Player, insert the deserved column to place your symbol"))
    if board[rowloc][coloc] =='e':
        board[rowloc][coloc] = whoseturn
    else:
        print("The deserved place is taken")
        rowloc = int(input("Player, insert the deserved row to place your symbol"))
        coloc = int(input("Player, insert the deserved column to place your symbol"))
    for i in range(0,3):
        print(board[i], '\n')
    if whoseturn=='O':
        whoseturn='X'
    if whoseturn=='X':
        whoseturn='O'
    return whoseturn
def win(board,x,y, whoseturn):
    if board[0][y] == (whoseturn) and board[1][y] == (whoseturn) and board [2][y] == (whoseturn):
        return True
    if board[0][y] == (whoseturn) and board[1][y] == (whoseturn) and board [2][y] == (whoseturn):
        return True
    if board[x][0] == (whoseturn) and board[x][1] == (whoseturn) and board [x][2] == (whoseturn):
        return True
    if board[x][0] == (whoseturn) and board[x][1] == (whoseturn) and board [x][2] == (whoseturn):
        return True
    if board[0][0] == (whoseturn) and board[1][1] == (whoseturn) and board [2][2] == (whoseturn):
        return True
    if board[0][0] == (whoseturn) and board[1][1] == (whoseturn) and board [2][2] == (whoseturn):
        return True
    if board[0][2] == (whoseturn) and board[1][1] == (whoseturn) and board [2][0] == (whoseturn):
        return True
    if board[0][2] == (whoseturn) and board[1][1] == (whoseturn) and board [2][0] == (whoseturn):
        return True
    return False
def isfull(board):
    for i in range(0,3):
        for j in range(0,3):
            if board[i][j]=='e':
                return False
    return True
def main():
    board = [['e','e','e']
            ,['e','e','e']
            ,['e','e','e']]
    print("Welcome to the great tic tac toe game!")
    print("Your board is now loading...")
    for i in range(0,3):
        print(board[i],'\n')
    player1=input("Player 1, select your symbol (X/O)")
    if player1 =='O':
        print('X is player 2s symbol')
        player2 = 'X'
    else:
        print('O is player 2s symbol')
        player2 = 'O'
    print("Player 1 will start")
    whoseturn=player1
    while(not (win(board,0,0,whoseturn)) and not isfull(board)):
       whoseturn=moveandturn(whoseturn,board)
       moveandturn(whoseturn,board)
    if not win(board,0,0,whoseturn) and isfull(board):
        print("Tied")
    else:
        print(whoseturn,"wins")


if __name__ == '__main__':
    main()

我将非常感谢您为解决上述问题提供的任何帮助!

【问题讨论】:

  • 当您使用调试器运行它时,它的行为与预期不同的第一个点是什么?
  • 第三回合不改变符号。例如,如果第一个选择的符号是 X,它可以让我将它放在板上,然后更改为 O 并让我放置它,但之后不会变回 O @ScottHunter
  • 这并不能回答我提出的问题。您是否尝试过使用调试器?
  • 除了答案中已经提到的 if / elif 问题之外,我认为您的代码中存在逻辑错误。代码管理玩家移动,然后切换玩家,然后检查玩家是否获胜(但检查改变的玩家而不是最后移动的玩家)。我建议此时重构代码以按此顺序管理移动、检查获胜和切换玩家。

标签: python


【解决方案1】:

第一个问题

  1. 玩家符号不会从 X 变为 O,也不会从 O 变为 X(如 moveandturn 函数中所写)

在函数 moveandturn 中,问题是条件不按预期工作。

if whoseturn=='O':   # 1
    whoseturn='X'
if whoseturn=='X':   # 2
    whoseturn='O'

当 whoturn == 'O' 为 True 时,whichturn 由上面的 1 变为 'X'。然后立即,其轮到上面的 2 变回 'O'

解决方案是在条件为:

if whoseturn == 'X':
  whoseturn = 'O'
else:
  whoseturn = 'X'

或者更简单地说:

whoseturn = 'X' if whoseturn == 'O' else 'O'

代码重构

解决上述问题和代码中的其他问题(包括第二个问题)

def get_move(whoseturn, board):
  # changed from moveandturn (to make the function do only one thing rather than two unrelated things, which was getting a move and switching turns)

  rowloc=int(input("Player, insert the deserved row to place your symbol: "))
  coloc=int(input("Player, insert the deserved column to place your symbol: "))
  while True:
    if not (0 <= rowloc < 3 and 0 <= coloc < 3):
      print('row and column must be 0, 1, or 2')
    elif  board[rowloc][coloc] !='e':
      print("The deserved place is taken")
    else:
      board[rowloc][coloc] = whoseturn
      break

  return rowloc, coloc

def display_board(board):
  print('\n'.join([' '.join(board[i]) for i in range(3)]))

def win(board, whoseturn, x, y):
  """ using code from https://codereview.stackexchange.com/questions/24764/tic-tac-toe-victory-check """
  # The posted code had many of the if conditions identical and always called win using row 0 and column 0 which is incorrect

  #check if previous move caused a win on vertical line 
  if board[0][y] == board[1][y] == board [2][y] == whoseturn:
    return True

  #check if previous move caused a win on horizontal line 
  if board[x][0] == board[x][1] == board [x][2] == whoseturn:
    return True

  #check if previous move was on the main diagonal and caused a win
  if x == y and board[0][0] == board[1][1] == board [2][2] == whoseturn:
      return True

  #check if previous move was on the secondary diagonal and caused a win
  if x + y == 2 and board[0][2] == board[1][1] == board [2][0] == whoseturn:
    return True

  return False       

def isfull(board):
    for i in range(0,3):
        for j in range(0,3):
            if board[i][j]=='e':
                return False
    return True

def main():
    board = [['e','e','e']
            ,['e','e','e']
            ,['e','e','e']]
    print("Welcome to the great tic tac toe game!")

    player1=input("Player 1, select your symbol (X/O): ")
    if player1 =='O':
        print('X is player 2s symbol')
        player2 = 'X'
    else:
        print('O is player 2s symbol')
        player2 = 'O'
    print("Player 1 will start")


    whoseturn=player1
    while True:
      # We alternative players, checking for win at each move
      display_board(board)

      rowloc, coloc = get_move(whoseturn, board)
      if win(board,whoseturn, rowloc, coloc):
        print(f'{whoseturn} wins')
        break

      if isfull(board):
        print('Tied')
        break

      # cange turns
      whoseturn = 'X' if whoseturn == 'O' else 'O'


if __name__ == '__main__':
   main()

【讨论】:

    【解决方案2】:

    无需查看其余代码:

    if whoseturn=='O':
        whoseturn='X'
    if whoseturn=='X':
        whoseturn='O'
    return whoseturn
    

    如果whoseturn 是“O”,则将其更改为“X”。然后您立即将其改回“O”,因为whoseturn=='X' 为真。您希望这两个选项相互排斥。你在想:

    if whoseturn == 'O':
        whoseturn = 'X'
    elif whoseturn == 'X':
        whoseturn = 'O'
    return whoseturn
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多