我写了一个对抗人工智能的井字游戏(我现在正在改进人工智能):
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}$我想让人工智能阻挡玩家的最后一招。在
Player1_row不是全局变量,而是函数turn_X的局部变量
要解决此问题,请尝试依次将Player1_row设置为全局变量
相关问题 更多 >
编程相关推荐