java - 如何在程序生成的地牢中找到房间内的门数量?
问题描述
作为序言,我将详细说明当前项目是什么。我正在开发一款将玩家置于基于房间的地牢的游戏。房间通过门进出。
这是我的地板创建者的 generator() 方法。
turn(int, int, int) 方法接受一个方向(随机创建的数字 0 - 3)、两个起始位置以及 x 和 y 坐标,并返回一个包含先前数字的数组,但取决于方向,一个通过递增/递减一来修改。例如,如果方向为 0,则表示向上,y 坐标将减 1。在返回的数组中,第一个值是 X,第二个是 Y。
int roomTag = 1; //tags the room as created
int currentX = startLocation; //the column that navigates the map
int currentY = startLocation;
map[currentY][currentX] = roomTag;
int rooms = 0;
while(rooms < maxRooms) {
int direction = randomRange(0, 4);
currentX = turn(direction, currentX, currentY)[0];
currentY = turn(direction, currentX, currentY)[1];
if(currentX > 0 && currentY > 0 && currentX < maxSize && currentY < maxSize) {
if(map[currentY][currentX] != roomTag) {
map[currentY][currentX] = roomTag;
roomList[currentY][currentX] = new NormalRoom(this, currentX, currentY, rooms);
rooms++;
}
} else {
currentX = startLocation;
currentY = startLocation;
}
}
roomList[startLocation][startLocation] = new StartRoom(this, startLocation, startLocation, 0);
到目前为止,程序生成效果很好。它每次生成的地牢都不同,所有房间都水平或垂直连接到至少一个其他房间。
房间仅保存有关门、位置和敌人的信息(如果适用)。房间有一种搜索要创建哪些门的方法。这是在房间实例化时调用的方法。首先调用它来获取要创建的门的数量,然后将创建的布尔值设置为 true,然后再次运行该方法以创建门/将门添加到数组中。
int doorIndex = doorCount - 1;
int[][] tempMap = floor.getMap();
if(yLocation > 0) {
for(int i = 0; i < tempMap.length; i++) {
for(int j = 0; j < tempMap.length; j++) {
if(i == tempMap.length/2 && j == tempMap.length/2) System.out.print("() ");
else if(tempMap[j][i] == 1) System.out.print("[] ");
else System.out.print(" ");
}
System.out.println(" ");
}
if(tempMap[yLocation - 1][xLocation] == 1) {
System.out.println("UP DOOR IN: (" + yLocation + ", " + xLocation + ")");
if(created) {
door[doorIndex] = new Door(0);
doorIndex--;
} else {
doorCount++;
}
}
}
if(yLocation < floor.getMap().length - 1) {
if(tempMap[yLocation + 1][xLocation] == 1) {
System.out.println("DOWN DOOR IN: (" + yLocation + ", " + xLocation + ")");
if(created) {
door[doorIndex] = new Door(2);
doorIndex--;
} else {
doorCount++;
}
}
}
if(xLocation < floor.getMap().length - 1) {
if(tempMap[yLocation][xLocation + 1] == 1) {
System.out.println("RIGHT DOOR IN: (" + yLocation + ", " + xLocation + ")");
if(created) {
door[doorIndex] = new Door(1);
doorIndex--;
} else {
doorCount++;
}
}
}
if(xLocation > 0) {
if(tempMap[yLocation][xLocation - 1] == 1) {
System.out.println("LEFT DOOR IN: (" + yLocation + ", " + xLocation + ")");
if(created) {
door[doorIndex] = new Door(3);
} else {
doorCount++;
}
}
}
if(created) {
for(int i = 0; i < door.length; i++) {
door[i].instantiate();
}
}
问题似乎是它从不创建左门,也很少创建门。我似乎无法找出它为什么这样做。我已经坚持了一段时间,似乎无法找到导致它的原因,因为我找不到问题的任何一致性。
我倾向于发布更少的代码,但如果需要更多,请告诉我。
提前致谢。
编辑:我可能已经找到了解决方案。鉴于该方法在房间的实例化期间被调用,它无法找到其他房间,因为它们还不存在。房间只能为之前创建的房间创建门。如果它们是在之后创建的,则地图不会将它们列为现有。
考虑到这一点,我将尝试修改问题。
编辑2:这就是问题所在。创建地图后创建房间修复了它。我只是简单地将房间创建者与地图创建者分开,然后我根据地图创建房间。
解决方案
问题是我在创建房间时正在实例化房间,房间的部分实例化是找到门的数量和位置。问题在于我要求算法找到尚未创建的房间。
为了解决这个问题,我使用了我之前创建的地图,这是一个 2DArray,房间标记为 1,其他所有内容都标记为 0。地图完全创建后,我遍历地图,并在其中放置了一个房间在包含 Room 对象的单独 2DArray 中用 1 标记的坐标。
门现在准确地通向新房间,因为它们应该是。
推荐阅读
- python - 如何获取有关 wiki 页面恢复编辑的详细信息?
- sql - 如何在 SQL 中复制这个特定的 SUMIFS 示例?
- ruby-on-rails - Rails“无法写入未知属性”
- php - 如何使用外部 RDS 数据库在 AWS Elastic Beanstalk 中设置 Laravel 应用程序
- geometry - 多边形之间的交集作为线剪
- azure-iot-hub - Errno 32 断管 Azure IOT Edge
- python - python错误设置数组元素与序列
- ios - 将数据从第二个控制器内的静态表传递到第一个视图控制器
- swift - 如何在swiftui中按下按钮时更改按钮图像
- java - 摆动文本字段方向