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

로봇 시작 자세

이제 로봇이 어떻게 배치되어 있는지 확인하여 회전에 따라 장애물을 올바르게 추가 할 수 있습니다.

는 지금 내가이 (조각) 같은 경우 명령문의 무리를하고 있어요 = X I = Y, J :

(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-loop를 사용하는 방법을 생각했지만 정확히 어떻게 작동하는지 정확히 알지 못합니다. 현재로서는 반복적이고 추악합니다.

전체 파일 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

나는 어떤 센서가 어느 번호로가는지를 정의하는 enum을 던질 것이다. 따라서 예를 들어 '{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, 어쩌면 내가 struct 접근 방식 밖으로 뭔가를 만들 수 있습니다. – Joel

+0

oh yea 나는 +1 -1 etcc @joel을 잊어 버린다.하지만 나는 그것을 편집 할 시간이 있다면 함수의 포인터처럼 구조체에 넣을 수 있다고 생각한다. – Cjdcoy

관련 문제