迭代地图时,似乎会跳过某些值

时间:2016-02-21 04:56:07

标签: java hashmap iteration

这里我有一个程序正在创建一个恒星和行星的地图。它通过以下方式实现:

  1. 创建多维索引数组'与indexMap[][]。这对于地图上的每个x和y坐标都是简单的意思,它会从1开始向前计数。e.g. indexMap[0][0] = 1, indexMap[1][0] = 2. indexMap[2][0] = 3... etc.
  2. 然后我们创建我们的&#34;象限&#34;它们主要是行星的太阳系和随机位置的恒星。这将在地图中找到合适的随机位置,然后它将扫描周围区域中的20个索引,为它们提供值。这些值以Map<Integer, Systems> map的形式出现,Systems是一个java类,它存储象限数的值,正文的类型(即行星)及其x和y坐标,而Integer是来自的当前索引。正在检查的indexMap[][]
  3. 这一切似乎都很好,但是当我遍历我的所有系统(第48行)时,我遇到了问题。根据下面的控制台日志,在创建的6颗星和42颗行星中,迭代时只发现了5颗行星和1颗恒星。此外,值得注意的是,它们都是同一象限的一部分,即便如此,如果你要计算实际上在象限6中的行星数量,并不是所有的物体都能找到那个象限。

    我是否正在不正确地进行迭代,或者是否还有另一个我无法预见的问题?

    CODE

    public class Systems 
    {
        public float x, y;
        public int body, quadNum;
    
        public Systems(int quadNum, int body, float x, float y)
        {
            this.quadNum = quadNum;
            this.body = body;
            this.x = x;
            this.y = y;
        }
    }
    
    public class UniverseGen 
    {   
        private float chanceNum, systemChance = 0.02f, planetChance = 0.1f;
    
        private Map<Integer, Systems> map = new HashMap<Integer, Systems>();
    
        public int[][] indexMap;
    
        private int indexWidth, indexHeight;
        public int quadNum = 1;
        private int numOfStars = 0, numOfPlanets = 0;
    
        private int space = 0, star = 1, planet = 2;
    
        public Map<Integer, Systems> initMap(int width, int height)
        {   
            indexWidth = width / BodyManager.TILE_WIDTH;
            indexHeight = height / BodyManager.TILE_HEIGHT;
    
            indexMap = new int[indexWidth][indexHeight];
    
            //Create Index Array
            System.out.println("Creating Index Array...");
            int indexCount = 0;
            for (int y=0; y<indexMap[0].length; y++)
            {
                for (int x=0; x<indexMap.length; x++)
                {
                    indexMap[x][y] = indexCount;
                    indexCount++;
                }
            }
    
            //Create Quadrants
            System.out.println("Creating Quadrants...");
            for (int y=0; y<indexHeight; y++)
            {
                for (int x=0; x<indexWidth; x++)
                {
                    createQuadrants(x, y);
                }
            }
    
            System.out.println("Number of Stars: " + numOfStars);
            System.out.println("Number of Planets: " + numOfPlanets);
    
            for (Systems s : map.values())
            {
                if (s.body != 0)
                {
                    System.out.println("Body: " + s.body + " Quadrant: " + s.quadNum + " x, y:" + s.x + ", " + s.y);
                }
            }
    
            return map;
        }
    
        private void createQuadrants(int x, int y)
        {
            int tileX = x * BodyManager.TILE_WIDTH;
            int tileY = y * BodyManager.TILE_HEIGHT;
    
            int planetCount = 0;
    
            if (x <= 0 || y <= 0 || x >= indexWidth || y >= indexHeight)
            {
                map.put(indexMap[x][y], new Systems(0, space, tileX, tileY));
            }
            else if (randomChance() <= systemChance && checkSystems(x, y))
            {
    
                System.out.println("Star: " + tileX + ", " + tileY + " Quad: " + quadNum);
                //Create Quadrant
                for (int i=-20; i<=20; i++)
                {
                    for (int j=-20; j<=20; j++)
                    {
                        //Surrounding tiles
                        int newX = (tileX + (i * BodyManager.TILE_WIDTH));
                        int newY = (tileY + (j * BodyManager.TILE_HEIGHT));
    
                        //If outside world boundaries, skip
                        if (x + i <= 0 || y + j <= 0 || x + i >= indexWidth || y + j >= indexHeight) 
                            continue;
                        //Create star at origin of system
                        else if (i == 0 && j == 0)
                        {
                            map.put(indexMap[x + i][y + j], new Systems(quadNum, star, tileX, tileY));
                        }
                        //When checking one tile outside the origin, create space
                        else if (i >= -1 && i <= 1 && j >= -1 && j <= 1)
                            map.put(indexMap[x + i][y + j], new Systems(quadNum, space, newX, newY));
                        //If planet parameters work out, create planet
                        else if (i >= -6 && i <= 6 && j >= -6 && j <= 6 &&
                                planetCount <= 6 &&
                                randomChance() <= planetChance && 
                                checkPlanets(x, y))
                        {
                            System.out.println("Planet: " + newX + ", " + newY);
                            map.put(indexMap[x + i][y + j], new Systems(quadNum, planet, newX, newY));
                            planetCount++;
                            numOfPlanets++;
                        }   
                        //If none of these create space
                        else
                            map.put(indexMap[x + i][y + j], new Systems(quadNum, space, newX, newY));
                    }
                }
                numOfStars++;
                quadNum++;
            }
            else
            {
                //Set to empty object
                map.put(indexMap[x][y], new Systems(0, space, tileX, tileY));
            }
        }
    
    
    //Some code has been deleted to shorten this post
    

    这是控制台输出:

    Creating Index Array...
    Creating Quadrants...
    Star: 1000, 840 Quad: 1
    Planet: 880, 880
    Planet: 900, 940
    Planet: 920, 840
    Planet: 920, 900
    Planet: 920, 920
    Planet: 940, 740
    Planet: 980, 900
    Star: 900, 860 Quad: 2
    Planet: 820, 860
    Planet: 840, 820
    Planet: 860, 860
    Planet: 880, 820
    Planet: 880, 980
    Planet: 900, 940
    Planet: 920, 900
    Star: 1080, 860 Quad: 3
    Planet: 980, 780
    Planet: 980, 840
    Planet: 1040, 760
    Planet: 1040, 820
    Planet: 1060, 920
    Planet: 1080, 940
    Planet: 1120, 740
    Star: 900, 880 Quad: 4
    Planet: 780, 880
    Planet: 800, 880
    Planet: 800, 980
    Planet: 840, 960
    Planet: 860, 960
    Planet: 880, 820
    Planet: 880, 920
    Star: 1020, 880 Quad: 5
    Planet: 900, 940
    Planet: 920, 760
    Planet: 920, 1000
    Planet: 940, 760
    Planet: 940, 800
    Planet: 960, 840
    Planet: 960, 960
    Star: 1060, 1160 Quad: 6
    Planet: 940, 1060
    Planet: 940, 1080
    Planet: 940, 1280
    Planet: 980, 1060
    Planet: 980, 1080
    Planet: 1000, 1100
    Planet: 1020, 1240
    Number of Stars: 6
    Number of Planets: 42
    Body: 2 Quadrant: 6 x, y:940.0, 1060.0
    Body: 2 Quadrant: 6 x, y:980.0, 1060.0
    Body: 2 Quadrant: 6 x, y:940.0, 1080.0
    Body: 2 Quadrant: 6 x, y:980.0, 1080.0
    Body: 2 Quadrant: 6 x, y:1000.0, 1100.0
    Body: 1 Quadrant: 6 x, y:1060.0, 1160.0
    

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的方法。感谢@aro_tech让我走上正轨。

我不会详细介绍,但我必须分别从地图本身访问象限编号,并修复了我在这篇文章中编辑过的checkSystems()方法的一些问题。

相关问题