I am trying to develop an AI for a game using Python. Therefore, I have a class "Game" and a class "AI".
I would like to use a variable of Game (e.g. the board of Game) inside AI, but I am not sure what is the best way to do it. Right now the only way I found to pass information from game to AI is to put it as an argument every time. I feel like there is a way better way to share variable with AI without needing to put in argument every time.
I would do something a little similar to that (I know this code can't work, but still) :
class Game():
def __init__(self):
self.board = 1 #It will be an array
self.ai = AI(self.board)
def functionThatUpdateTheBoard(self):
self.board = 2
def useTheAI(self):
print(self.board)
self.ai.getAction()
class AI():
def __init__(self,board):
self.board = board
print(self.board)
def getAction(self):
print(self.board)
game = Game()
game.functionThatUpdateTheBoard()
game.useTheAI()
and getting :
1
2
2
thanks for the help :)
boardis an immutable object like an int then this wont work. If you use an array and modify it, it will work (though mutable stuff tend to introduce bugs since you have to be very careful). Alternatively changeAI(self.board)toAI(self)and useself.game.boardinsideAI