How to access game_state info from previous turns

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):
	self.prevHP = 20

def on_turn(self, turn_state):
	print("Last round's HP was:")
	#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):
	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("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.

1 Like

that first bit of code goes into your main program?

Both functions should already exist in your, 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) 
	self.prevGame_State = game_state

so the 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 file (default starter algo) it says:

The 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.

1 Like