简体   繁体   English

是否有太多 if、else-if 语句错误?

[英]Are too many if , else-if statements wrong?

I have to do a program that's simulating the move of an ant in a 5x5 grid map,of cells.我必须做一个程序来模拟 ant 在单元格的 5x5 网格 map 中的移动。 Imagine an 5x5 array in which, in position (2,2) and (3,3) there is food for ants to collect.想象一个 5x5 阵列,其中 position (2,2) 和 (3,3) 中有供蚂蚁收集的食物。 Every ant starts moving from its nest which is in a specific cell (1,4 or 4,1).每个 ant 都从位于特定单元格(1,4 或 4,1)中的巢穴开始移动。 There is a method called move() that i have to write,which moves the ant one cell in each call.我必须编写一个名为 move() 的方法,它在每次调用中将 ant 移动一个单元格。 The move can either be horizontal or vertical.移动可以是水平的或垂直的。 Also the ant cant move to his last position (to avoid going back and forth). ant 也不能移动到他的最后一个 position (以避免来回走动)。 Also each cell has 'smell points' and the ant prefers to go in the cell with the biggest points(so you compare its available options and pick the one with the biggest smell points,simple right?).此外,每个单元格都有“气味点”,ant 更喜欢 go 单元格中的最大点(所以你比较它的可用选项并选择气味点最大的那个,简单吧?)。 Now, the problem.现在,问题。 They want the ant to make completely random moves incase the cell has no smell points or the smell points of the available cells are equal.他们希望 ant 进行完全随机的移动,以防单元格没有气味点或可用单元格的气味点相等。 So to sum it up:所以总结一下:

  1. if statement to check in which cell the ant is. if 语句检查 ant 在哪个单元格中。 When you find it you know where to look for his previous move.当你找到它时,你就知道在哪里寻找他以前的举动。
  2. nested if statement to check in which cell the ant was last.嵌套 if 语句来检查 ant 是最后一个单元格。 That's 3 or 4 if statements in a 5x5 area.那是 5x5 区域中的 3 或 4 个 if 语句。 When you find the last,in which the ant can't move,you know which cells to compare.当您找到 ant 不能移动的最后一个时,您就知道要比较哪些单元格了。
  3. nested if statement between 2 or 3 cells to find the one with the most smell points.在 2 或 3 个单元格之间嵌套 if 语句,以找到气味点最多的一个。
  4. nested if statement in case their smell points are equal or 0. In this case the ant moves completely random( i made it via Math.random() ).嵌套 if 语句以防它们的气味点等于或 0。在这种情况下,ant 完全随机移动(我是通过 Math.random() 实现的)。 All the above is for just one cell.. Each cell has different options because of its location in map.以上所有内容仅适用于一个电池。每个电池都有不同的选项,因为它位于 map。 So to make this as you can see i need way too many if statements.所以要做到这一点,如你所见,我需要太多的 if 语句。 I can't even count them without getting mind blowned, But i can't imagine a way to make this without if statements.我什至无法在没有被吹嘘的情况下数数它们,但我无法想象没有 if 语句的方法。 Also every cell its different and the ant has different options to move, so I can't categorise it, For example.此外,每个单元格都不同,ant 有不同的移动选项,所以我无法对其进行分类,例如。 imagine the ant is in (1,1), It can go to (1.2) or (2,1) only, If it is at (4,2) it can go to (4,3)or(3,2)or(4.1)or(5.2).想象一下 ant 在 (1,1) 中,它可以 go 到 (1.2) 或 (2,1) 仅,如果它在 (4,2) 它可以 Z34D1F91FB2E514B8576FAB1A89A6BZ 到 (((()或(4.1)或(5.2)。 You have to make 4 if statement to check which one of these was his last position and then go on with the other nested ifs??.您必须创建 4 个 if 语句来检查其中一个是他的最后一个 position,然后是 go 与其他嵌套的 ifs??。 So what do you think?所以你怎么看? Is it wrong to have so many ifs?有这么多ifs有错吗?
// int pos[] = new int[2]; in the pos[0] is the position for the x, pos[1] for the y.
if(isAt(1,1))
        {
          if(wasAt(pos[0]+1,pos[1]))
          {
            previousX = pos[0];
            previousY = pos[1];
            pos[1] = pos[1] + 1;
          }
          else
          {
            previousX = pos[0];
            previousY = pos[1];
            pos[0] = pos[0] + 1;
          }
        }

This does sound like too many if statements.这听起来确实像太多的if语句。 Here are some suggestions:以下是一些建议:

  1. You should remember where the ant is in a variable.您应该记住 ant 在变量中的位置。 I don't think you need any if statements to check which cell the ant is in.我认为您不需要任何 if 语句来检查 ant 所在的单元格。

  2. You should remember where the ant was last in a different variable.您应该记住 ant 最后在不同变量中的位置。

  3. You should make a list of the surrounding points and their smell points.您应该列出周围的点及其气味点。 Then remove from this list the ants previous location.然后从此列表中删除蚂蚁以前的位置。

  4. Using the above collection, you can loop through to pick the right one or if there is more than one choose one at random from the collection.使用上面的集合,您可以循环选择正确的一个,或者如果有多个从集合中随机选择一个。

When you move the ant, set the previous location to the current location and then update the current location.移动 ant 时,将之前的位置设置为当前位置,然后更新当前位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM