名称错误:未定义全局名称“Player1\u row”

2024-10-03 19:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个对抗人工智能的井字游戏(我现在正在改进人工智能):

Matrix = [[0, 0, 0], 
          [0, 0, 0], 
          [0, 0, 0]]
Matrix_2 = [[" ", " ", " "], 
            [" ", " ", " "], 
            [" ", " ", " "]]
def turnX():
    Player1_row = int(raw_input("P1 What row do you want?"))
    Player1_row = Player1_row - 1
    Player1_column = int(raw_input("P1 What column do you want?"))
    Player1_column = Player1_column - 1
    if Player1_row > Matrix:
        turnX()
    if Player1_column > Matrix:
        turnX()
    if (1 == Matrix[Player1_column][Player1_row] or 500 == Matrix[Player1_column][Player1_row]):
        print "This is an invaild move!"
        turnX()
    else:
        Matrix[Player1_column][Player1_row] = 1
        Matrix_2[Player1_column][Player1_row] = "X"

def turnY():
      global Player1_row
      Player2_row = int(random.randint(1, boardX))
      Player2_row = Player2_row - 1
      Player2_column = int(random.randint(Player1_row, boardY))
      Player2_column = Player2_column - 1
      if (1 == Matrix[Player2_column][Player2_row] or 500 == Matrix[Player2_column][Player2_row]):
          turnY()
      else:
          print "AI Turn:"
          Matrix[Player2_column][Player2_row] = 500
          Matrix_2[Player2_column][Player2_row] = "O"

但我得到了一个错误:

^{pr2}$

我想让人工智能阻挡玩家的最后一招。在


Tags: inputrawifdefcolumndowhat人工智能