The Accountant

I’m a newbie here and i want to know how the code work in CodinGame, I try some easy methods for test case 1,2,3 but it didn’t work, i have spent 2 days but couldn’t figure out why. Here is the code:
for (int i = 0; i < enemyCount; i++) {
int enemyId;
int enemyX;
int enemyY;
int enemyLife;
cin >> enemyId >> enemyX >> enemyY >> enemyLife; cin.ignore();
a[i]=enemyLife;

    }
    if(a[0]>0){
       cout<<"shoot 0"<<endl;
    }
    else if(a[1]>0)
        cout<<"shoot 1"<<endl;
    else
        cout<<"shoot 2"<<endl;

At first, i create an array , then in the loop that create enemy, i use array to store enemyLife. After that i use if to cout shoot, although enemy 0 died which mean enemyLife==0, it just keep on shooting enemy 0 so i lose, i don’t know why. Please help

If an enemy dies CG won’t send you that enemy again, so the first enemy it sends will be enemy id 1.

1 Like

oh , thanks a lot. then could you please show me how to change target, please?

You can store the enemys in their respective positions in the array, so you don’t put the first enemy in the 0. position, but instead you put it in it’s indexes position: a[enemy.id] = life. Otherwise you create a second array, and store the ids there in the same indexes you put the lifes. Third option would be to create a custom data structure (like Entity or Enemy), and have only one array of these structs, and do the comparison like: a[0].life > 0 shoot a[0].id. I hope this helps. :slight_smile:

2 Likes

thanks again, you’re my savior :smile:

uhm Hi, could you help me. Thanks. In the Accountant, i create a struct out of int main():
struct cord{
int x;
int y;
int id;
};
_Then i create an array type struct: cord p[100];
_After that i create: int id;
_In the loop to create enemy, i store information in the array
for (int i = 0; i < enemyCount; i++) {
int enemyId;
int enemyX;
int enemyY;
int enemyLife;
cin >> enemyId >> enemyX >> enemyY >> enemyLife; cin.ignore();
id=enemyId;
p[enemyId].x=enemyX; //store info
p[enemyId].y=enemyY;
p[enemyId].id=enemyId;
p[enemyId].life=enemyLife;
}
_According to the loop, id=enemyId, so it should be the last enemy id,
_So i create : int i=id-enemyCount+1; // to shoot enemy from first to last
_This is the condition to move:
for (int h=0;h<i+1;h++){
if(p[h].life>0){
int safedistance=sqrt((p[h].x-x)(p[h].x-x)+(p[h].y-y)(p[h].y-y));
if(safedistance<3000)
cout<<"move “<<abs(x)<<” "<<abs(y-500)<<endl;
}
}
_Last of all: cout<<"shoot "<<i<<endl;// to shoot

The problem is in test case 9, i don’t why it doesn’t move.

Oh sorry the struct is :
struct cord{
int id;
int x;
int y;
int life;
};

What does your variable i represent?

the variable i that I create in loop will only affect in the loop.

the variable i that I create means the id of the enemy.
Example: the test case have 2 enemies then in the first big loop the game will create 2 enemies, so in the code i wrote above, the variable id will equal to the last enemy id =>id=1.
so variable i will be: i=id-enemycount+1, because id=1, enemycount=2 so in the first loop: i=0;

In the end of code i wrote: cout<<"shoot "<<i<<endl;
so in the first loop, it will shoot 0, then in the second loop, it will shoot 1

Do you really need to compute it that way?
You’re searching for the id of the first enemy (the one that is given to you in the first iteration of the for loop) right? You prefer to compute it using the id of the last enemy (corresponding to the last iteration of the for loop) and the number of enemies. Why not save it in the first iteration?

Does it help?

Few pointers:

  • try to name properly your variables. Prefer something like “enemyIdToTarget” over “id”
  • don’t mix ids and counters. The first enemy given to you in input will not always have its id equal to 0, etc.
  • keep it simple. Don’t do unnecessary computing.

Thanks, but I think you missing the point here, Because i’m a newbie so sometimes my way is quite complicated, thanks for your advices. But the real big question is above, although my code complicated (i know), it still run from test case 1 to 8(i don’t mention about losing or winning because i just test first), but in test case 9, it should run away, but it just standing, and i can’t figure out why. Could you help me,please?

Use error stream (cerr) to display safedistance before the if to debug it.

Plus, you can’t print (cout) severals lines in 1 turn, because it will mess up your whole algo. So don’t print cout << “Move x y” in a for loop.

Use a var to store x and y-500 et print it outside the loop

Thanks a lot, it really helps me out, because i’m a newbie so your advice is like a manual book for me. Thanks.