2017-10-16 47 views
1

我有一个网格,我添加不同的单元块,取决于我目前收到的旋转。根据下面(前方传感器,0 ROT)更好的方法来检查重复if语句条件

超前传感器--->,左传感器^和右传感器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                                         @@@ 
@@@                                                                                         @@@ 
@@@                                                                                         @@@ 
@@@                                                                                         @@@ 
@@@                                                                                         @@@ 
@@@                                                                                         @@@ 
@@@                                                                                         @@@ 
@@@                                                                                         @@@ 
@@@                                                                                         @@@ 
@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ 

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

机器人的起始姿势

现在我正在检查我的机器人如何定位,以便它可以根据它的旋转正确添加障碍物。 I = 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

+0

我将不得不使用宏。但是这不是http://codereview.stackexchange.com的问题吗? –

+0

@ Jean-FrançoisFabre我不熟悉那个子堆栈。当我有问题时,Stackoverflow通常是我去的地方。我会看看寿。从理论上说,这不是一个代码审查问题,它是关于以另一种方式解决任务的逻辑的编程问题。 – Joel

+0

如果有人回答,很好。如果你的问题得到了解决,在codereview上发表相同的内容,你就会得到upvotes。 –

回答

2

避免使用这么多,如果你可以使用结构的表,保持所有你想比较的信息。
该结构将包含一个函数指针,所以如果所有的条件都没问题,位于结构表的x位置的函数将被执行。

我把一个小例子所以它更清楚你:

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(); 
      } 
     } 
    } 
} 
+1

我也会抛出一个定义哪个传感器进入哪个编号的枚举。因此你得到'{0,45,LEFT,LEFT,selector_fnc_one}'定义第一个分支。 – Ray

+0

我喜欢这种方式,但我不确定这会解决什么问题。首先,在GetCellState(grid,newStart.i,newStart.j + 1)中,你不检查它是否实际上应该是j或i上的+1。另一个注意事项。我非常喜欢比较传感器和度数的想法,就像你用'{0,45,5,5,FULL_DANGER,MAP_BORDER',''所做的那样。 upvoted,也许我可以做一些工作结构的方法。 – Joel

+0

哦,是的,我忘了+1 -1 etcc @joel,但我认为你也可以把它作为函数的指针,如果我有时间我会编辑它。 – Cjdcoy

相关问题