Detecting Breaches

ok thanks, where should I put the last peace of code (what file)?

I have it in the algo_strategy.py

what does the all_turn_states[frame[“turnInfo”][1]][frame[“turnInfo”][2]] = frame do?
this may be a stupid question, but im not very good

From a quick look above, I believe all_turn_states is a python list. This means frame[“turnInfo”][1]][frame[“turnInfo”][2] returns an integer (which it does).

Thus, it may be a little bit clearer to see the code this way:

index = frame[“turnInfo”][1]][frame[“turnInfo”][2]
all_turn_states[index] = frame

where frame is a python dictionary (frame = json.loads(game_state)).

Thus, this is code to store previous game_state information in list format, hence the name all_turn_states.

whta does “turninfo” return exactly?

frame is a python dictionary, which means it stores information using key->val format. Essentially, a dictionary has a key which returns a val. So generally you could do:

myDict = {} # create an empty python dictionary
myDict[0] = 'test' # set the key 0 to the val 'test'
myDict['key'] = 5 # set the key 'key' to the val 5

and then to get that information:

print (myDict[0])
print (myDict['key'])

which would output:

test
0

Thus, turnInfo is a key for the dictionary frame. It returns information about that turn as given by the game engine. You can see a lot more about how the information is formatted here. You can also try printing it out (eg print (frame['turnInfo']))

what does the if breach[4]==2 do?
is it looking for a specific condition?

breach[4] == 2 is checking the player index. when you loop through frame['events']['breach'] you are looping through every single breach, for both players. Inside breach, specifically at index 4, the player index is recorded. So this checks to see if the owner of the breaching unit is player 2 (the other player). If it is, you record it by adding it to list_breaches.

I highly recommend using this topic as a reference for the structure of this data:

What is wrong with this? Im just bad at coding xD sorry.

the “turnInfo” quotes look kinda weird, don’t you think?
Isn’t it supposed to make the text look green?
This happens when you copy-paste the stuff above directly into your code.

Oh yeah I saw that but is everything else correct?
Also were do I call detectBreach?

i believe so did you run it in terimnal?

yeah I did and I get this error:

File “/tmp/algo13700944336319812296/algo_strategy.py”, line 37, in on_action
frame = json.loads(game_state)
NameError: name ‘json’ is not defined

thats weird algocore imports json in its file

Have you ever been able to get this code to work?

json is a module that is included by default with Python when you install it, so the error means you either never import json (you deleted it somewhere - check to make sure its at the top of your code and if not add it).

If your code still fails after adding import json at the top:
Try opening an cmd or terminal window:

  1. Type py or python and enter (interactive python should show up, will have >)
  2. Type and enter import json. If that fails then you have a problem with your installation.

Yeah I must have deleted it somewhen, thanks it works now.
Where do I call detectBreach?

im guessing you can call whenever in your strategy after the action phase

So im currently calling detectBreaches like this:

self.detectBreaches(game_state)

and it doesnt crash but (as you see in the code im outputting the breaches in the console) it keeps printing this:

[[[26, 12]]]
[[[26, 12], [26, 12]]]
[[[26, 12], [26, 12], [26, 12]]]

and I dont know what this is supposed to be, its not the coordinate where the breach is, nor anything else I can tell.

What now? I would like the coordinates where my opponents information units actually breach.

in the code above(picture) you put list_breaches_turns inside the for frame and for breach loops. So for every breach it appends the list again and again, so if a unit actually breaches [26,12] it will add it again and again. list_breaches_turns should append the list after all the for loops.
You’re also calling gamelib.debug_write(possibly list_breaches_turns in here) also inside the for breach and for frame loops, outputting the repetitive list_breaches_turns. This is why it outputs that way.
It is unlikely that the code itself would change the coordinates without some specific script that was added to change the breach info. Also, why would you even want to change that breach information?
Hope this answers your question

Yeah thanks this kind of answered my question, but im also wondering about why its outputting [26,12] if it has nothing to do with where anything breached.

Also to answer your question, I dont quite understand what you mean by

why would you even want to change that breach information?

I want to get the information of were the enemy breached my side, to place firewalls there or something along those lines, I dont intend to change anything about that information.

EDIT:

Erm nevermind I fixed it