Skynet Revolution 1. SI not updating

Hello guys,

I have almost solved this puzzle, but I came across of some issue. Basically, I have the entire solution written in a single function, that needs to be fed a current node index of a Skynet agent (SI) at each game turn. But the problem is that, regardless of the fact that the game turns change, the value of SI remains the same (even though visually agent moves)!

Here’s how it is set-up on my side:

Function solve_this_puzzle(){
...
}

//game loop

while(true){
var SI = parseInt(readline());
solve_this_puzzle(SI);
}

Do you know why SI value remains the same, even though the function is being repeated?

Thanks

According to you code sample, i believe you code in javascript. I can’t say for sure, but maybe you just have a scope problem and the SI you are using in not what you think.

1 Like

That could be the problem. What is more important, is that I am not sure I understand what a “game turn” means. Does the game run only the script which is in the “game loop” (all what is in while(true) loop), or does the game run all the script I’ve written (including all the reading of information before while(true) loop). Do you know?

Somehow, by using switch() conditioning, I managed to get different SI values on each game turn, but it looks really weird and I do not understand why it is working. It looks like it:

switch(SI){
case 1:
do1();
SI = parseInt(readline())
break;
case 2:
do2();
SI = parseInt(readline())
break;

}

It looks like after each time I sever a link I can try to read and get new node ind formation of an agent, but if simply read this information without switch statement, it doesn’t work:

//doesn’t work like this

while(true){
var SI = parseInt(readline());
solve_this_puzzle(SI);
}

//it doesn’t update SI value even if I use this (SI was declared before game loop):

while(true){
solve_this_puzzle(SI);
SI = parseInt(readline());
}

I’m really stuck on this one :slight_smile:

The way things work is that your script is connected to a program on the server that gives input to your script, waits for the script to produce output, then updates the game state and produces new input reflecting the new game state, until you either lose, win, or fail to produce the expected output in time. So one “turn” is effectively one iteration of that loop.

In code, one “game turn” is basically the contents of the while( true ) loop in the code below:

// read initialization input (given only once at startup)
while( true ) {
    // read turn input, as specified in the problem description
    // compute output
    // print output for this turn, as specified in the problem description
}

Rule of thumb: always start by checking that the input is what you expect it to be. So the first thing I’d try in your case is to print the contents of SI to stderr right after you read it, then maybe output a specific sequence of link kills just to see whether SI evolves accordingly.

Once you have that input and output code working, leave it as-is (the default code should usually be correct in that regard) and only do stuff between those parts.

As Magus suggested, you may want to check for variable scoping issues. Also, do make sure that you respect the formatting and amount of both input and output: if the description asks for one line of output per turn and you provide two inside your main while(), then the second line will be interpreted by the game engine as your output for the turn after the current one and you will desynchronize from the engine.

1 Like

Make sure you’re always outputting something at the end of your loop, too. Your program may be looping back to main without a print out. I’m not 100% sure, but I think the codingame inputs will act weird if you don’t output before looping.

If you don’t output you will get a timeout :slight_smile:

Right! Nevermind my answer.

Thank you for the assistance, gentlemen! I had to revise the entire code of mine and I found that FOR loop was causing this. It would take long for me to explain it, but in general I forgot to exit ({break;}) a loop when a need link was found, so FOR loop was acting weird - it would output several values in a sequence, disregarding the SI value. SI value was changing, just like Bob_ explained. I completed this puzzle.

Thanks again!

1 Like