我有一个网格,我添加不同的单元块,取决于我目前收到的旋转。根据下面(前方传感器,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。 –