Terminal Game Q&A

ok thanks for showing me where to start! i managed to get something working so i’ll post the code modifications i made here so others don’t have to spend 2 hours debugging like i did (ok that might be because im a terrible programmer but still)

i made these modifications to the algocore file:

def getinfo(self):
    return self.state

def __init__(self):
    self.config = None
    self.info = None
    self.state = None
    self.get_info = True

if stateType == 0:
    self.get_info = True
    """
    This is the game turn game state message. Algo must now print to stdout 2 lines, one for build phase one for
    deploy phase. Printing is handled by the provided functions.
    """
    self.on_turn(game_state_string)

elif stateType == 1:
    """
    If stateType == 1, this game_state_string string represents the results of an action phase
    """
    if self.get_info:
        # debug_write("getting info...")
        self.state = game_state_string
        self.get_info = False
        continue
    else:
        continue

to see which location the opponent attacked from, you’d only want to get the first frame of the action. the get_info variable tells you whether to collect the frame or not, and in this program i have it set to false after it gets data, and back to true during the beginning of each turn. the frame is stored in self.state.

i then added this to the algo_strategy.py file:

def getinfo(self):
    return super().getinfo()

this allows me to call the getinfo method from algocore when i want to, which is cool because algocore isn’t very nice with letting you do that stuff.

modifications to game_state.py:

    (in __init()__)
    self.my_unit_types = []
    self.my_unit_locations = []
    self.enemy_unit_types = []
    self.enemy_unit_locations = []
    self.__parse_state(serialized_string)

            if unit_type == REMOVE:
                self.game_map[x,y][0].pending_removal = True
            else:
                unit = GameUnit(unit_type, self.config, player_number, hp, x, y)
                self.game_map[x,y].append(unit)
                # debug_write("type of unit is " + str(unit_type))
                if not is_stationary(unit_type):
                    if player_number == 1:
                        self.enemy_unit_types.append(unit.unit_type)
                        self.enemy_unit_locations.append([unit.x, unit.y])
                    else:
                        self.my_unit_types.append(unit.unit_type)
                        self.my_unit_locations.append([unit.x, unit.y])

the __create_parsed_units method didn’t have any ways to use information units because it usually wasn’t dealing with them (which was my main problem, the gamestate object was being called at the beginning of the turn when there were no information units). i added them here.

finally, this was added to the on_turn method in algo_strategy:

    info = self.getinfo()
    if info is not None:
        frame_0_gamestate = gamelib.GameState(self.config, info)
        for attack in frame_0_gamestate.enemy_unit_locations:
            # gamelib.debug_write("The enemy attacked from " + str(attack) + " last turn.")
            self.past_enemy_attacks.append(attack)

it uses the frame collected from the modified algocore file to make a second gamestate object, which has the information units on it.

this is probably a pretty inefficient solution but hopefully it will help some people!!

1 Like

I knew that I’d forgotten to put something into my answer… I guess that it was the __create_parsed_units. I have it in my code, but I forgot to place it into the thread. Thank you for clarifying. Do you mind placing your answer here into the thread that I mentioned, so that people can see that as well? If I see people asking about that, that is the thread that I always give to them, and it would be nice to be able to continue doing that :grinning:.

@C1 people:
Would you want a fixed version of something like this in the starter kit as an open source contribution, or is this one of the things that you want people to puzzle out for themselves? I could get together a nice, working version of this, but I am lazy, and don’t really want to go through the effort if you won’t use it.

@Tim

If you have a specific proposal for an open source contribution, you can message me directly for feedback.