【发布时间】:2016-02-21 04:56:06
【问题描述】:
在这里,我有一个程序正在创建恒星和行星的地图。它这样做是:
- 使用
indexMap[][]创建一个多维“索引”数组。这仅仅意味着对于地图上的每个 x 和 y 坐标,它都会被赋予一个从 1 开始计数的数字。e.g. indexMap[0][0] = 1, indexMap[1][0] = 2. indexMap[2][0] = 3... etc. - 然后我们创建我们的“象限”,它们本质上是行星的太阳系和随机位置的恒星。这将在地图中找到一个合适的随机位置,然后它将扫描周围区域的 20 个索引,为它们提供值。这些值以
Map<Integer, Systems> map的形式出现,Systems 是一个 java 类,用于存储象限数、物体类型(即行星)及其 x 和 y 坐标的值,而 Integer 是来自@987654324 的当前索引@ 正在检查中。
这一切看起来都很好,但是当我遍历我的所有系统(大约第 48 行)时,我遇到了问题。根据下面的控制台日志,在创建的 6 颗恒星和 42 颗行星中,迭代时只发现了 5 颗行星和 1 颗恒星。此外,值得注意的是,它们都在同一个象限中,即使那样,如果你要计算实际上在第 6 象限中的行星的数量,并不是所有的天体都在那个象限中被发现。
我是在错误地迭代,还是我没有预见到另一个问题?
代码
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
【问题讨论】:
-
有很多代码需要有人阅读!
-
是的,很抱歉,这就是为什么我一开始就简要解释了它是如何工作的
-
我会编辑它并使其更短
-
您在
put调用的地图中使用 indexMap 的方式似乎存在问题。看起来您的索引图是基于象限的,但是每次创建象限时,您都会迭代 i 和 j 值,这些值会添加到象限坐标中,这似乎会覆盖之前的象限值。因此,在迭代中,您只能看到最后一个象限的值。 -
@aro_tech 感谢您的评论,这让我对如何解决问题有了一个很好的了解。希望它有效:)