Issues and newbie questions

Hello,

I managed to write my first simple algo and upload it.

Now, issue start when I try to start the game between my algo and any boss, or try to play be hand against a boss. Game gets stuck in loading screen.

Second issue I encounter is when I play a replay (my algo plays automatically against others?) all I see are lightning animations. All the firewalls and whatnot are invisible, thus I cannot tell what is happening.

Am I doing something wrong?

Edit: I’m using Windows 10 and Firefox.

I’d try using google chrome.

And that indeed solved my issue. Many thanks :D.

1 Like

Okay, so another newbie question from me.

I made a variable, say, X, that has value set to 0. I then ask the algo to check a particular field for firewall presence. If there is no firewall present there at the beginning of the turn the value of X is being changed by 1. When X has value of 1 or more things (should) start to happen. To be more precise, I add and remove some positions to and from a list.

Here’s a piece of code I use:

if not game_state.contains_stationary_unit([0,13]):
X += 1

This works nicely for a turn. Next turn, if I rebuild the firewall that triggered the change of value variable X it’s as if there was no change to the list.

Now, I’m a Python newbie, so I’d like to understand what is happening.

I suspect when new turn starts value of variable X is being reset back to 0. Is that the case? If yes, is there a way to “save” the value of X once it is increased?

Many thanks in advance for any advice.

Your belief that it is being reset is correct. Every variable has what is called a scope. This essentially determines where it is defined. Every time you enter that function it creates a new variable X, and then when it leaves that function it deletes it. In Python, the scope of a variable is determined by indentation (tab). So basically inside functions, loops, etc. One way to deal with this is to make the variable global (declare it at the top, the lowest indentation level), but this is bad practice since it can lead to name conflicts, especially with a name as common as X.

What you should do is make that variable a part of the class. This means the variable is a member of that class and thus can be used anywhere in that class as long as it has been created. In Terminal, this is in the AlgoStrategy class. To make a variable part of the class, you add self to its definition, so it would become self.X. If you aren’t familiar with classes I recommend reading more about them. Here is a little test to demonstrate.

This is effectively what you are doing now:

def not_persistent():
    X = 0
    # Do a bunch of stuff here
    print (X)
    X += 1

As you can see, it resets X every time the function is called, this happens every turn in the game.

In contrast, here is an example with the AlgoStrategy class where it will persist:

class AlgoSrategy(gamelib.AlgoCore):
    def __init__(self):
        # other inititalization stuff here
        self.X = 0

    def on_turn(self, turn_state):
        game_state = gamelib.GameState(self.config, turn_state)
        if not game_state.contains_stationary_unit([0,13]):
            self.X += 1

        # Do stuff with self.X

The key here is that you are declaring X as a member of the class AlgoStrategy, which is why you use the . to access it from self. The idea is you could access X from that class, and self represents the current object of that class. So whenever you use X inside the class, you must do self.X and outside it would just be like accessing another objects member. So for an example of outside the class:

class TestClass:
    def __init__(self):
        self.X = 0
    def increment(self):
        self.X += 1

test = TestClass()
print (test.X)
test.increment()
print (test.X)
1 Like

Brilliant! It works now like a charm!

Thank you very much Isaac for all the incredible effort you put into writing this!

No problem! Glad to be of help; feel free to ask about any future stuff, happy coding :).