Is there an easy way to access some basic game_state data from previous turns, like no. bits or health of player?
One way to do this would be to save each turn as it happens and then reference them later when you would like to use that information.
You can save individual data:
def __init__(self): super().__init__() random.seed() self.prevHP = 20 def on_turn(self, turn_state): print("Last round's HP was:") print(self.prevHP) #Your code here #Put the current hp in the var for next round self.prevHP = game_state.my_health
I use this in my code, and it works.
You can also theoretically store complete game states similarly:
def __init__(self): super().__init__() random.seed() self.prevGame_State = None def on_turn(self, turn_state): if not self.prevGame_State is None: #Make sure this variable contains a game state print("Last round's HP was:") print(self.prevGame_State.my_health) else: print("There is no previous game state yet") #Your code here #Put the current game state in the var for next round self.prevGame_State = game_state
Be aware that a gamestate object is much, much bigger than an integer, so try to stick to ints if you can.
that first bit of code goes into your main algo_strategy.py program?
Both functions should already exist in your algo_strategy.py, and yes, you will add the code to those existing functions and modify it to your needs.
bit should also already be there, so you won’t need to add it.
whereabouts do you include the code in the on_turn function? Before or after the code that already is present in the function?
It depends on what you want the game_state to reflect. If you save the previous state before you make your changes, you are effectively saving the data before you make any changes in that turn. In contrast, saving the state after will reflect any changes you have made to the game_state. Python is running this all on a single thread, so calls are made sequentially. This means the order in which code and functions are called is the order in which the code will run.
However, the catch is that python does not save a unique instance of the game_state object when you set it equal to self.prevGame_State. Rather, it copies the reference to the same object to save memory and speed. This is called a shallow copy. This means any changes to the current state will be reflected in the new variable self.prevGame_State. A copy that creates a completely new object is called a deep copy.
So if you use a shallow copy (how it is now), it shouldn’t matter whether it is before or after you make changes. However, if you do a deep copy then the order matters.
Essentially you have your on_turn function, which is run every turn:
def on_turn(self, turn_state): game_state = gamelib.GameState(self.config, turn_state) # get the current state of the engine # either put this line here self.prevGame_State = game_state # if a shallow copy, it does not matter if here or after # any code to change the game_state, add destructors, etc. # or here # self.prevGame_State = game_state # if a shallow copy, it does not matter if here or before game_state.submit_turn() # submit your changes to the engine
You can learn more about shallow and deep copying here:
Hope that helps somewhat, let me know if anything was confusing.
The algos create a new
GameState every turn:
def on_turn(self, turn_state): game_state = gamelib.GameState(self.config, turn_state) #Code self.prevGame_State = game_state
self.prevGame_State won’t be affected by the newly created game state. We have to set
self.prevGame_State last, so we don’t overwrite it until we don’t need it anymore.
The game_state object will be recreated each turn, but the changes I am talking about are alterations by the coder within the same turn. If you do a deep copy you will not change the original object. This is why at the top of the algo_strategy.py file (default starter algo) it says:
The GameState.map object can be manually manipulated to create hypothetical
board states. Though, we recommended making a copy of the map to preserve
the actual current map state.
Where the previous state is a hypothetical board state since it is not active.