简体   繁体   English

我的移动算法无法正常工作

[英]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: 另外,这是我的rem函数:

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. 通过查看在以下情况下如何在for循环之外声明变量k: for(int k=0;k < teamsize; ++k)我猜您是C的中间用户像我这样的语言。

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 . 我的理解是,您在返回结构数据的两个数组( teamfield上遇到了麻烦。

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. 当将team数组编码为二维字段数组时,线性team数组本质上是一个表达式,其结果必须通过求值来获得。 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. 尽管您具有具有返回类型为int的各个结构数据成员,但team数组将必须具有team的struct返回类型,以使这些实例成为team数组的元素。

As you may already know, C's array is essentially a pointer that points to the first element in the array. 您可能已经知道,C的数组本质上是一个指向数组中第一个元素的指针。 This implementation of C language suggests that the two arrays are located in two different memory address spaces. C语言的这种实现建议两个数组位于两个不同的内存地址空间中。 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. 因此,为了检索字段数组中的数据,编译器将不得不遍历team数组以检索必需的int数据。

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. 仅通过思考过程,就可以合理地假设应该将int值返回到数组位置括号,以便可以访问正确的字段数组元素。 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. 由于这些非标量数​​据点非常复杂,因此很可能无法编译代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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