How to find where my (or my enemy's) information units scored?

Ok. I’ll try that. Thank’s for your reply! I hope it turns out ok.

Yeah so I read the entire thing, and I still have no idea what to do. Can someone explain to me exactly what to do in terms of someone who really didn’t start coding at all a month ago? I’m learning so much from doing this! I would really like to know how to do this…

So, I’ve made a function that gets the gamestate, but I… don’t really know how to get it out of the function.
Please… help?
Thanks in advance

So there are 2 JSON files? I check through all of the starter kit master and I found no other JSON file, is it in the engine.jar? If so, it is too hard for my computer to load it…

Please clarify what you are asking.

Where is the second JSON file? I only found game-configs.json

the JSON file for the action phase or what?

@DuckyQuack236 are you talking about this:

If so, I was talking about two place in the code where data is is retrieved in JSON format. There is no file that is saved. The JSON is sent directly (through stdout/in which is why you cannot print()) from the engine to and from python. There are two places in the code (no file) that reference and store data using JSON.

ah i understand, so somewhere in algocore they reference JSON correct?

I believe it is
state = json.loads(game_state_string)
parsed_config = json.loads(game_state_string)
though the “parsed_config” might be of a replay.

1 Like

Nowhere in the python code does it access replays, it is 100% handled by the java engine. The two lines you have identified are for the following:

state = json.loads(game_state_string)
Contains the information for each turn/round.

parsed_config = json.loads(game_state_string)
Contains generic information (a reference)

Look at when each gets called. parsed_config is only called at the start while state is called every round.

What do you mean by you don’t know how to get it out of the function? I don’t really understand which specific part you are having trouble with

One thing that I didn’t mention (because I didn’t know) is that GameUnit doesn’t like REMOVE units. If you create a GameState based on a game_state_string from the action phase, and a REMOVE unit is present, your algo will crash. This is because certain qualities (such as speed, damage, range, etc.) are not defined for the REMOVE unit. If a GameUnit of a REMOVE unit is created (GameUnits are created when a GameState is created), you will receive an error. To fix this, you will have to edit In the function __serialize_self, add 2 lines after the line
(Or really any time before any undefined qualities are requested). The lines to add are:

if self.unit_type == REMOVE:
<indent>self.max_stability = 0

Thanks to the same thread that I referenced earlier (Parsing Replay Files) for mentioning that this was an issue

Another issue that I have found is that this sometimes crashes despite this fix. The error is (ignoring the giant line of functions leading up to the error):

File "/tmp/algo16180919916539904124/gamelib/", line 136, in __create_parsed_units self.game_map[x,y][0].pending_removal = True
IndexError: list index out of range

To fix it, around the line

if unit_type == REMOVE:
<indent>self.game_map[x,y][0].pending_removal = True

Change it to:

if unit_type == REMOVE:
<indent><indent>self.game_map[x,y][0].pending_removal = True
<indent><indent>gamelib.debug_write("Error! Program tried to die while parsing REMOVE unit")

Side note: This is a very hacky fix, so if somebody knows how to fix it properly, feel free to say so. I don’t really know why this is happening.

I will also edit my previous answer to add this

If you make a get_action_stuff function in Algostrategy and run it in Algocore, how are you supposed to get the information(action phase gamestate) back into Algostrategy?
Basically, I’m just asking about what you said:

If I were to take the game_state_string as an argument in a function and run it in Algocore, how would I get the information?
Thanks in advance

Let me try and help clarify how these two classes work. When you create an AlgoStrategy object you are also creating an AlgoCore object. AlgoStrategy inherits all of the functions and data, etc from AlgoCore unless you override it. Thus, if you create data in AlgoCore, it is inherently accessible in the AlgoStrategy class, as long as it has been initialized, just like any other variable. Thus, you are never sending data from one class to the other, AlgoStrategy is an AlgoCore when it is created as an object.

Here is an article specifically regarding Object-Oriented Programming (OOP) in Python:

If you have any questions about how OOP works, etc just ask :).

1 Like

Your explanation was very helpful for me. But one thing I don’t get is: How can I extract the location of the breach from state["events"]["breach"]? Could you give me an example, please?

state["event"]["breach"] is a list of units. Not GameUnits, but unit information. You can access a specific unit by adding another set of square brackets [] after. For example, state["events"]["breach"][0] would access the first unit. However, since you don’t know how many units there are, you will want to iterate through with a for loop. The information about the units is also an array, and you can see what all of the numbers mean in the link that I showed earlier (but I will place it again): Parsing Replay Files


I have tried now for 4 hours, but im not able to get the game_state_string from the action method (line 62 in AlgoCore to AlgoStrategy. I have tried self.action_state = json.loads(game_state_string) in AlgoCore, but somehow everything trying to “import” it in AlgoStrategy fails. I have tried it with gamelib.AlgoCore.action_state and gamelib.AlgoCore.start.action_state and MUCH more but i cant get it to work, please help

Ok, first of all, AlgoStrategy can already access the class variables and functions from AlgoCore because it is subclassed. So let’s look at some examples that may be similar to what you tried:

In AlgoCore, you create a class variable:

class AlgoCore(object):
    def __init__(self):
        self.config = None
        self.action_state = None

Then you get the game state string:

elif stateType == 1:
     If stateType == 1, this game_state_string string represents the results of an action phase
     self.action_state = game_state_string

You can now access this in AlgoStrategy, by the same name. Stuff = self.action_state in AlgoStrategy will make stuff equal to the last value self.action_state was set to in AlgoCore.

There are a few problems with this that might prevent what you actually want from happening. For one thing, that stateType == 1 case will typically trigger many, many times during each turn, once for every frame of the action phase. Depending on what you want to do with the game state string, this may require additional processing.

If you want to analyse each frame for something, a better approach might be to declare a function in AlgoCore, and override it in AlgoStrategy.

Let’s say in AlgoCore, this function is declared:

def on_action(self, game_state):

Then this function is called in the action block:

elif stateType == 1:
     If stateType == 1, this game_state_string string represents the results of an action phase

Then in AlgoStrategy, you declare this same function (“override” it).

class AlgoStrategy(gamelib.AlgoCore):
    #All other functions 'n' stuff

    def on_action(self, game_state):
         state = json.loads(game_state)
         #Do stuff with state, find what you wanted, store the results in AlgoStrategy somewhere

Now when AlgoCore runs, every game_state_string will be passed to that function in AlgoStrategy, and when the next turn starts all the results will be wherever you put them in AlgoStrategy.

This may not even be the best way to do this, but it is one way if you are stuck.


im really glad you helped me :wink: Im now trying it out. I thought, that no one would help me, cause of strategical reasons or something, but im happy, that you helped me :wink:

1 Like