检查重复的if语句条件的更好方法

时间:2017-10-16 10:47:00

标签: c

我有一个网格,其中我添加了不同的单元格块,具体取决于我当前正在接收的轮换。

@@@ = Map border ### = Obstacle 0-9 = Distance to goal-node ??? = Goal Path

机器人的起始姿势如下图所示(前方传感器,0 Rot)

前方传感器---&gt ;,左传感器^和右传感器v

@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                         ###                                             @@@
@@@                                     ###  S  ###                                         @@@
@@@                                   4   3 ???   3   4                                     @@@
@@@                               4   3   2 ???   2   3   4                                 @@@
@@@                           4   3   2   1   G   1   2   3   4                              @@@
@@@                               4   3   2   1   2   3   4                                 @@@
@@@                                   4   3   2   3   4                                     @@@
@@@                                       4   3   4                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@                                                                                         @@@
@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@

现在我正在检查我的机器人的位置,以便根据它的旋转正确添加障碍物。 我= y, j = x

现在我正在做一堆像这样的if语句(摘录):

currentRot介于0-360之间)。

if(currentRot >= 0 && currentRot <= 45){ 
    printf("\n0-45");
    if (ir.sensor[5] > FULL_DANGER){ //left
          if(GetCellState(grid,newStart.i-1,newStart.j) != MAP_BORDER){ChangeCellState(grid,newStart.i-1,newStart.j,-3);printf("\n1,1");goto SUCCESS;} //add obs in map
    }
    if (ir.sensor[2] > FULL_DANGER){ //right
          if(GetCellState(grid,newStart.i+1,newStart.j) != MAP_BORDER){ChangeCellState(grid,newStart.i+1,newStart.j,-3);printf("\n1,2");goto SUCCESS;}
    }
    if (ir.sensor[0] > FULL_DANGER || ir.sensor[7] > FULL_DANGER){ //ahead
          if(GetCellState(grid,newStart.i,newStart.j+1) != MAP_BORDER){ChangeCellState(grid,newStart.i,newStart.j+1,-3);printf("\n1,3");goto SUCCESS;}
    }
}
if(currentRot >= 45 && currentRot <= 90){
     printf("\n45-90");
     if (ir.sensor[5] > FULL_DANGER){ //left
           if(GetCellState(grid,newStart.i,newStart.j-1) != MAP_BORDER){ChangeCellState(grid,newStart.i,newStart.j-1,-3);printf("\n1,1");goto SUCCESS;} //add obs in map
     }
     if (ir.sensor[2] > FULL_DANGER){ //right
           if(GetCellState(grid,newStart.i,newStart.j+1) != MAP_BORDER){ChangeCellState(grid,newStart.i,newStart.j+1,-3);printf("\n1,2");goto SUCCESS;}
     }
     if (ir.sensor[0] > FULL_DANGER || ir.sensor[7] > FULL_DANGER){ //ahead
           if(GetCellState(grid,newStart.i-1,newStart.j) != MAP_BORDER){ChangeCellState(grid,newStart.i-1,newStart.j,-3);printf("\n1,3");goto SUCCESS;}
     }
 }

现在进入问题:是否有更好(更聪明)的方法来检查这些条件并应用相同的逻辑?就像,我想过使用嵌套的for循环,但确切地说这将是如何工作的我不太确定。截至目前,它非常重复和丑陋。

可以找到整个文件here, row 519

1 个答案:

答案 0 :(得分:2)

要避免使用这么多,如果你可以使用一个结构表来保存你想要比较的所有信息。
结构将包含一个函数指针,所以如果所有条件都正常,则函数位于张贴&#39; x&#39;您的结构表将被执行。

我会举一个例子让你更清楚:

typedef struct s_selector
{
  int first_value;
  int second_value;
  int which_sensor;
  int which_sensor_two;
  int danger;           //danger and map border aren't needed
  int map_border;       //danger and map border aren't needed
  void (*fcpointer) (void); //this is the key part that will execute the different actions
  int offset_i;
  int offset_j;
} g_selector;

//table of structure, to compare data, execute your funtion
g_selector selector[] = {
  {0, 45, 5, 5, FULL_DANGER, MAP_BORDER),
  selector_fct_one, -1, 0},
  {0, 45, 2, 2 FULL_DANGER, MAP_BORDER),
  selector_fct_two, 1, 0},
  {0, 45, 0, 7, FULL_DANGER, MAP_BORDER),
  selector_fct_three, 0, 1},
  {45, 90, 5, 5, FULL_DANGER, MAP_BORDER),
  selector_fct_four, 0, -1},
  {45, 90, 2, 2 FULL_DANGER, MAP_BORDER),
  selector_fct_five, 0, 1},
  {45, 90, 0, 7, FULL_DANGER, MAP_BORDER),
  selector_fct_six, -1, 0},
};

//I didnt wrotte all the functions but i think that you get the idea.
void selector_fct_one()
{
    ChangeCellState(grid,newStart.i-1,newStart.j,-3);
    printf("\n1,1");
    goto SUCCESS;
}

void selector_fct_two()
{
    ChangeCellState(grid,newStart.i+1,newStart.j,-3);
    printf("\n1,2");
    goto SUCCESS;
}

/* here you got all the conditions stacked in one if, when you advance in the
** table of structure, if all the conditions match then the fonction located
** at the location 'x' of the structure will be executed.*/

int current_rotation () 
{
  for (int j = 0; j <= 7; j++) //this loop goes trough all the sensor
    {
      for (int i = 0; i < 6; i++) // this loop check all the structure table
        {
      if (currentRot >= selector[i].first_value &&
          currentRot <= selector[i].second_value &&
          ir.sensor[j] > selector[i].danger &&
          (j == selector[i].which_sensorsensor ||
          j == selector[i].which_sensor_two) &&
          (GetCellState(grid,newStart.i + selector[i].offset_i,
          newStart.j + selector[i].offset_j) != selector[i].MAP_BORDER))
          {
            selector[i].fcpointer();
          }
        }
    }
}