0

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 :)

3
  • 1
    It depends. If board is 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 change AI(self.board) to AI(self) and use self.game.board inside AI Commented Sep 3, 2019 at 8:25
  • That does indeed work! You said it may introduce bugs, what do you think the best way to tackle this then ? Call the variable board to every functions ? Commented Sep 3, 2019 at 8:48
  • Read about scopes-and-namespaces - global and class-variables Commented Sep 3, 2019 at 9:49

1 Answer 1

1

See if you can make use of Observer Pattern.

Something like, Create subscriber and call when "board" changes value in Game class. If a class needs the updated value of "board" they can subscribe, which gets triggered on value of "board" changes.

In this way, you can be very sure of the value being changed irrespective of immutability of data type used.

class Game(object):
 def __init__(self):
    self.board = 1
    self.observers = []

 def functionThatUpdateTheBoard(self, new_value): 
    # you can change this to setter ,
    # refer https://www.tutorialspoint.com/What-are-Getters-Setters-methods-for-Python-Class
    self.board = new_value
    for fn in self.observers:
        fn(self.board) # send your board value here.


 def subscribe(self, callback):
    self.observers.append(callback)

class AI(object):
  def game_board_value_changed(self, value):
      print("Printing value in AI class")
      print("Value of 'board' changed in Game class to {}".format(value))


game = Game()
ai = AI()
game.subscribe(ai.game_board_value_changed)
print("*"*50)
modified_value = 10
print("Board value changed in Main to {}".format(modified_value))
game.functionThatUpdateTheBoard(modified_value)
print("*"*50)
modified_value = 100
print("Board value changed in Main to {}".format(modified_value))
game.functionThatUpdateTheBoard(modified_value)
print("*"*50)

**************************************************
Board value changed in Main to 10
Printing value in AI class
Value of 'board' changed in Game class to 10
**************************************************
Board value changed in Main to 100
Printing value in AI class
Value of 'board' changed in Game class to 100
**************************************************
Sign up to request clarification or add additional context in comments.

1 Comment

I am currently reading about Observer pattern, but I'm not sure how to implement what you said. I would like to know if you would could show me a very simple implementation for this example Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.