我有一個網格,我添加不同的單元塊,取決於我目前收到的旋轉。根據下面(前方傳感器,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:
我將不得不使用宏。但是這不是http://codereview.stackexchange.com的問題嗎? –
@ Jean-FrançoisFabre我不熟悉那個子堆棧。當我有問題時,Stackoverflow通常是我去的地方。我會看看壽。從理論上說,這不是一個代碼審查問題,它是關於以另一種方式解決任務的邏輯的編程問題。 – Joel
如果有人回答,很好。如果你的問題得到了解決,在codereview上發表相同的內容,你就會得到upvotes。 –