2017-10-16 129 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