【问题标题】:Creating loop to check when paths (ArrayLists) have the same position at same index创建循环以检查路径(ArrayLists)何时在相同索引处具有相同位置
【发布时间】:2021-01-14 16:05:15
【问题描述】:

我有一个 Player 类,其中包含一个 path 属性; path 属性是 ArrayList<Node>

每个Players 路径可以有不同的大小。

对于当前的Player我想比较一下其他的Playerpaths,看看同一个元素和索引是不是同一个位置;然后只更改当前的path

例子:

        for(Player p : listAI()) {
            if(p == bot || p.getPath() == null || path == null) continue;
            for (int i = 0; i < Math.min(p.getPath().size(), path.size()); i++) {
                if (path.get(i).position.equals(p.getPath().get(i).position)) {
                    path.add(i,MoveType.None);
                }
            }
        }

原始路径:

  • currentBot = [5:3, 5:4, 5:5, 6:2] //我们正在检查的机器人
  • bot1 = [5:3, 6:4, 7:2, 3:9]
  • bot2 = [5:3, 6:2, 7:2, 5:5, 5:8]

处理使用 NONE 更新路径的 for 循环(我们正在尝试找出的那个);应该只改变当前机器人的路径。

改变它:

它应该一次查看其他机器人路径。如果它在同一个索引处找到相同的位置,那么它只会通过添加一个空白移动来更新当前的机器人路径。

示例循环(bot1 和 bot2 路径在整个检查过程中永远不会改变):

第一次迭代:(currentBot 检查 bot1 以查看它是否在相同索引处具有相同位置

  1. currentBot 将路径与 bot1 进行比较

  2. currentBot 看到索引 0 是一样的

  3. currentBot 在索引 0 处插入一个空白移动;

第一次迭代后的currentBot路径:[0:0, 5:3, 5:4, 5:5, 6:2]

第二次迭代:(currentBot 检查 bot2 是否在相同索引处具有相同位置

  1. currentBot 现在将新路径与 bot2 进行比较

  2. currentBot 看到索引 3 相同

  3. currentBot 在索引 3 处插入一个空白移动

第二次迭代后的 currentBot 路径:[0:0, 5:3, 5:4, 0,0, 5:5, 6:2]

循环结束: findPath 算法返回新路径;


有了这些信息,我只想修改上面示例中所示的路径之一,在添加空白位置后,我基本上将位置移动到 1 个索引上。

即使比较不同大小的路径,有人可以帮我实现吗?尽可能高效?

【问题讨论】:

  • playerList 和路径变量的最大大小是多少?另外,如果 path 是 List,path 和 playerList 的大小是否一直相同?
  • @BSangappa 它可能会有所不同。作为一款网络游戏。每个路径的目标可能不同

标签: java arraylist indexing


【解决方案1】:

你可以做的是,维护一个 Index List,它存储 path List 和 playerListindex 位置> 具有相同的值。一旦你有了相同值的索引位置,你就可以在 path 列表中的那些索引位置做一些事情。

例如,看看下面的代码 sn -p

        List<Integer> path = new ArrayList<Integer>();

    path.add(0);
    path.add(1);
    path.add(5);

    List<Integer> playerList = new ArrayList<Integer>();

    playerList.add(1);
    playerList.add(5);
    playerList.add(9);
    
    List<Integer> indexList = new ArrayList<Integer>();
    
    if(playerList.size() < path.size()) {
        for (int i = 0; i < playerList.size(); i++) {
            if (playerList.get(i) == path.get(i)) {
                indexList.add(i);
            }
        }
    }else {
        for (int i = 0; i < path.size(); i++) {
            if (playerList.get(i) == path.get(i)) {
                indexList.add(i);
            }
        }
    }       
    System.out.println("Need change/modification in following indexes : " + indexList);
    
    for (int i = 0; i < indexList.size(); i++) {
        //Do something at path.get(i)
    }

我们在这里所做的是,我们正在检查 path List 的索引 i 处的值是否等于 的索引 i 处的值playerList 列表。如果相等,我们可以将该索引添加到 indexList。完成后,我们可以在 path List 的那些位置使用 indexListdoSomeThing 的索引值。

使用 if 条件我们检查哪个 List 的大小更小,如果 path List 的大小小于 playerList,那么我们从 循环0 到路径列表的大小 或者如果 playerList 的大小小于 路径列表 那么,我们正在从 0 循环到 playerList 的大小。这种条件检查帮助我们避免出现 IndexOutOfBoundsException。

希望您对这种方法有所了解。

【讨论】:

  • 当我做与你的回答类似的事情时;它没有给我预期的输出
【解决方案2】:

您可以像这样使用 hashmap 存储玩家路径或创建玩家地图字符串时的索引值,并且可以是 String 以外的任何东西,在这种情况下,它检查 playerMap 是否在相同索引处具有相同的字符串,例如播放器路径。

 public static void main(String[] args) {
        HashMap<Integer, String> playerMap = new HashMap<Integer, String>();
        HashMap<Integer, String> playerPath = new HashMap<Integer, String>();
        for(int i = 0; i < playerMap.size(); i++) {
            if(playerMap.containsKey(i) == playerPath.containsKey(i % playerPath.size())){
                System.out.println("Found where player standing");
        }
}

如果 player.path 有至少一个匹配的值是 playerList,以上代码返回 true

【讨论】:

  • 我想知道它是否在相同的元素上包含相同的内容。不只是如果它包含。玩家可以很好地使用相同的路径,但我不希望他们同时使用它。
  • 不完全了解您的内容,但包含检查给定元素是否存在于两个列表中。 Java 定义:如果在列表中找到指定的元素,则返回 true,否则返回 false。
  • 我需要知道它是否包含在每个列表中的相同位置
  • 那么就可以使用HashMap了
  • 好的,所以我在 hashmap 上做了一些示例,让您了解仅使用 List 的明显方法,但成本太高,因为会有嵌套循环,我假设您已经在使用 gameloop这也是开销。
猜你喜欢
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 2020-08-20
  • 2023-02-12
  • 1970-01-01
相关资源
最近更新 更多