简体   繁体   中英

My move algorithm isn't working properly

After much tinkering, I think I nailed down the source of my problem concerning my players not moving properly on the field:

 for (t = 1; t <= ROUNDS; t++){
      if (t % 10 == 0) {
      print_game (field);
     }
     if (teamsize > 1){

        for (m = 0; m < SIZE_TEAM; m++){
        if (team [m].presence == 1){
          if (team [m].direction == East){
             if (team [m].y == 24){
              if (field [team [m].x][team [m].y - 1] != 0){
                 rem (field [team [m].x][team [m].y - 1], teamsize);
                 field [team [m].x][team [m].y - 1] = team [m].id;
                 teamsize--;
                 }
                 else {
                 field [team [m].x][team [m].y - 1] = team [m].id;
                 }
                 }

              if (field [team [m].x][team [m].y + 1] != 0) {
                 rem (field [team [m].x][team [m].y + 1], teamsize);
                 field [team [m].x][team [m].y + 1] = team [m].id;
                 teamsize--;
              }
                 else {
                    field [team [m].x][team [m].y + 1] = team [m].id;
                 }
                 field [team [m].x][team [m].y] = 0;
              }
           else if (team [i].direction == West){
             if (team [m].y == 0){
              if (field [team [m].x][team [m].y + 1] != 0){
                 rem (field [team [m].x][team [m].y + 1], teamsize);
                 field [team [m].x][team [m].y + 1] = team [m].id;
                 teamsize--;
                 }
                 else {
                 field [team [m].x][team [m].y + 1] = team [m].id;
                 }
                }
              if (field [team [m].x][team [m].y - 1] != 0) {
                 rem (field [team [m].x][team [m].y - 1], teamsize);
                 field [team [m].x][team [m].y - 1] = team [m].id;
                 teamsize--;
              }
                 else {
                    field [team [m].x][team [m].y - 1] = team [m].id;
                 }
 field [team [m].x][team [m].y] = 0;
               }
               else if (team [i].direction == North){
                if (team [m].x == 0){
                  if (field [team [m].x + 1][team [m].y] != 0){
                     rem (field [team [m].x + 1][team [m].y], teamsize);
                     field [team [m].x + 1][team [m].y] = team [m].id;
                     teamsize--;
                     }
                     else {
                     field [team [m].x + 1][team [m].y] = team [m].id;
                 }
              if (field [team [m].x - 1][team [m].y] != 0){
                 rem (field [team [m].x - 1][team [m].y], teamsize);
                 field [team [m].x - 1][team [m].y] = team [m].id;
                 teamsize--;
              }
                 else {
                    field [team [m].x - 1][team [m].y] = team [m].id;
                 }
              field [team [m].x][team [m].y] = 0;
           }
           else if (team [i].direction == South){
            if (team [m].x == 24){
              if (field [team [m].x - 1][team [m].y] != 0){
                 rem (field [team [m].x - 1][team [m].y], teamsize);
                 field [team [m].x - 1][team [m].y] = team [m].id;
                 teamsize--;
                 }
                 else {
                 field [team [m].x - 1][team [m].y] = team [m].id;
                 }
              if (field [team [m].x + 1][team [m].y] != 0){
                 rem (field [team [m].x + 1][team [m].y], teamsize);
                 field [team [m].x + 1][team [m].y] = team [m].id;
                 teamsize--;
              }
                 else {
                    field [team [m].x + 1][team [m].y] = team [m].id;
                 }
                  field [team [m].x][team [m].y] = 0;
               }
            }
         }
       }
     }
    }
   }

  print_game (field);
   return 0;
}

How do I know this? Well, I tested parts of the code and the results came out just fine. Only when I included this huge loop did things get complicated. Anyway, let me know if you see any quirks.

Also, here's my rem function:

int rem (int id, int teamsize){
   int k;
   for (k = 0; k < teamsize; k++){
      if (team [k].id == id){
         team [k].presence = 0;
      }
   }
}

And my enum:

   enum move_direction {East = 1, West = 2, North = 3, South = 4};

By looking at how you have declared the variable k outside of for-loop when you could have done the following: for(int k=0;k < teamsize; ++k) I'm guessing you are an intermediate user of the C language like me.

Since you have not linked your previously posted question here, I going strictly by this post here only.

My understanding is that you are having trouble with the two arrays that return struct data, team and field .

Is it possible that the two structs are not compatible, or even worse non-scalar data?

When you code the team array into the two-dimensional field array, the linear team array is essentially an expression whose result must be gotten by evaluation. Although you have the individual struct data members with int return type, the team array would have to have team's struct return type in order for those instances to be elements of team array.

As you may already know, C's array is essentially a pointer that points to the first element in the array. This implementation of C language suggests that the two arrays are located in two different memory address spaces. Therefore, in order to retrieve the data in the field array, the compiler would have to traverse to the team array to retrieve necessary int data.

By thought process alone, it is logical to assume that int value should be returned to the array position brackets so that correct field array element can be accessed. But not everything works logically.

It is very possible that the code won't compile due to the very complexity of those non-scalar data points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM