I am currently working on a Battleships project in Python where we have been given the task of creating our own strategy for the game which has a board in the shape of an L (removing the upper right quadrant). My strategy, when choosing the moves, was going to be like a checkerboard effect where when it hit a ship it would check the adjacent squares. The problem I have is that when the function is returned it no longer keeps the values for the row and column number where the hit was last turn.
I am wondering if there is a way to record the row and column variables for when the function is run again for the next move.
Here is the section of the code containing the variables I want to recall the next time the code is run:
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