我目前的工作,我们已经给出了创建我们自己的具有在形状的板游戏战略任务Python中的战舰项目被退回一个L(去除右上象限)。我的策略是,在选择棋步时,就像棋盘格效果,当它碰到船时,它会检查相邻的方格。我遇到的问题是,当函数返回时,它不再保留最后一次命中行和列号的值。回顾/记忆变量后,他们已经在一个函数中使用Python
我想知道如果有一种方法来记录当函数再次运行下次移动的行和列变量。
下面是代码包含我想要的代码运行下一次召回变量部分:
def chooseMove():
global playerBoard, opponentBoard
row = int()
col = int()
if (opponentBoard[row][col] == const.HIT):
row = row - 1
if (opponentBoard[row][col] == const.MISSED) or (opponentBoard[row][col] == const.HIT) or (opponentBoard[row] < 0):
row = row + 1
if (opponentBoard[row][col] == const.MISSED) or (opponentBoard[row][col] == const.HIT) or ((opponentBoard[row] > 5) and (opponentBoard[col] < 6)):
col = col - 1
if (opponentBoard[row][col] == const.MISSED) or (opponentBoard[row][col] == const.HIT) or (opponentBoard[col] < 0):
col = col + 1
if (opponentBoard[row][col] == const.MISSED) or (opponentBoard[row][col] == const.HIT) or ((opponentBoard[row] > 5) and (opponentBoard[col] < 6)):
while (opponentBoard[row][col] == const.HIT) or (opponentBoard[row][col] == const.MISSED) or ((row + col) % 2 == 1):
row = randint(0,len(opponentBoard)-1)
col = randint(0,len(opponentBoard[row])-1)
else:
return row, col
else:
return row, col
else:
return row, col
else:
return row, col
else:
return row, col
else:
while (opponentBoard[row][col] == const.HIT) or (opponentBoard[row][col] == const.MISSED) or ((row + col) % 2 == 1):
row = randint(0,len(opponentBoard)-1)
col = randint(0,len(opponentBoard[row])-1)
else:
return row, col
你不想存储在列表中的最后几个动作,然后将它传递给chooseMove()函数作为参数的原因吗? – ballsatballsdotballs 2014-09-25 16:20:11
我会在每个回报前存储行和列的值吗? python会记住列表已经存储在列表中后,它已被返回,然后再次通过? – ThomasB2685 2014-09-25 17:26:21