The 4 last contests (MM, BotG, CR, CoK) are different from those in the past in a way that there can be many units to manage, sometimes of different type. I am having hard-time figuring out how to manage them well, especially how to organize my code so that it remains readable easily, but also so that I can keep the the “history” of these units, which can be useful to take heuristic decisions. For example, knowing the previous position of an entity (in order not to go back there for example), knowing if it has already struck me and now I am immune (e.g. YELL in CoK), knowing the past trajectory, etc. Sometimes these inputs are not given (normal, it would be too long), and it is necessary to keep track of what happened and not regenerate units/entities at each turn.
For example, in python (sorry it is the only language I know for now), here is a template of what I used to do:
# game loop
while True:
entities = [] # creating a new empty list
for i in range(number_units):
id,x,y,team,... = input()
entities.append(Entity(id,x,y,team,....) # instantiating an entity class and appending it to the list
# sorting units, by type, owner, distance to something...
my_entities = [e for e in entities if e.team==my_team]
enemy_entities = [e for e in entities if e.team==enemy_team]
... # heuristics and decisions
Unfortunately, as you can see, I re-create every units at each turn. It works and is readable, which is good for a start. However, I am afraid this is not very efficient and it does not allow to keep track of the history, as lists are emptied at the beginning of each turn. This also makes simulation impossible (or at least very difficult as far as I understand).
An improved version, but far from being perfect, would be:
entities = {} # dictionary this time, with unique_id:class_instance
#game loop
while True:
for i in range(number_units):
id,x,y,team,... = input()
if id in entities.keys():
entities[id].update(x,y,team,...) # assuming the class Entity has a method update
else:
entities[id] = Entity(id,x,y,team,....) # instantiating an entity class and keeping track in the dict
for e in entities.keys(): # find the dead at previous iteration and delete them
if e.was_not_updated_at_this_iteration:
del(entities[e])
# sorting units, by type, owner, distance to something...
my_entities = [e for e in entities.values() if e.team==my_team]
enemy_entities = [e for e in entities.values() if e.team==enemy_team]
... # heuristics and decisions
When there are not many units (CSB, GoD, CotC, …), it is easy to update the entity correctly and keep clean code. But when there are many, I was not able to think of a better way than storing them in a dictionary with unique_id as the key and the instance as the value. This is probably not optimal either, as for example I have to go through all the dictionary several times in order to remove the dead, sort the new ones, etc.
I would be really interested in knowing other techniques, or how you cope with this difficulty, as I am really often pulling my hair to keep clean code in this kind of situation. Hope this was clear, please do not hesitate to change the category of the post. Thanks a lot.