我有一个带有数字的csv文件(即0,2,34,0,2,...)都在一行上。我正在使用扫描仪从文件中读取它们。该文件代表一个非常大的游戏内地图。我知道这张地图的宽度和高度。扫描此文件时,我只想捕捉下图中的棕色矩形(游戏地图的一部分)。我正在扫描文件,并希望将csv文件中的值放入short []。
int mapWidth = 8;
int mapHeight = 6;
int rectangleWidth = 5;
int rectangleHeight = 3;
short[] tmpMap = new short[rectangleWidth * rectangleHeight];
Scanner scanner = new Scanner(new File(path));
scanner.useDelimiter(", ");
我也很幸运地知道我需要的矩形的四个角。深棕色方块(我需要捕获的矩形的四个角)可以表示为:
int topLeftIndex = 17;
int topRightIndex = 21;
int bottomLeftIndex = 33;
int bottomRightIndex = 37;
我知道在我的扫描方法中,我可以查看是否在矩形范围内,以及带有以下内容的蓝色突出显示框:
if (count >= topLeftIndex && count <= bottomRightIndex){
//within or outside (east and west) of rectangle
我无法考虑识别和不存储蓝色高亮方块的逻辑。
此矩形的大小,整个地图的大小和深褐色点只是数字示例,并且会发生变化,但我会一直都知道它们。任何人都可以帮助我吗?
这是我到目前为止所做的:
private static short[] scanMapFile(String path, int topLeftIndex,
int topRightIndex, int bottomLeftIndex, int bottomRightIndex)
throws FileNotFoundException {
Scanner scanner = new Scanner(new File(path));
scanner.useDelimiter(", ");
short[] tmpMap = new short[mapWidth * mapHeight];
int count = 0;
int arrayIndex = 0;
while(scanner.hasNext()){
if (count >= topLeftIndex && count <= bottomRightIndex){
//within or outside (east and west) of rectangle
if (count == bottomRightIndex){ //last entry
tmpMap[arrayIndex] = Short.parseShort(scanner.next());
break;
} else { //not last entry
tmpMap[arrayIndex] = Short.parseShort(scanner.next());
arrayIndex++;
}
} else {
scanner.next(); //have to advance scanner
}
count++;
}
scanner.close();
return tmpMap;
}
注意:这不是学校作业 :)非常感谢任何帮助。
答案 0 :(得分:1)
你可以这样:
public boolean isInside(int n) {
if(n >= topLeftIndex && n <= bottomRightIndex) {
if(n % mapWidth >= topLeftIndex % mapWidth
&& mapWidth % mapWidth <= bottomRightIndex % mapWidth) {
return true;
}
}
return false;
}
首先检查您已检查过的内容,然后检查“列”是否正确。 如果您知道左上角索引,右下角索引和地图宽度,则此方法始终有效。
答案 1 :(得分:0)
以下是我为捕获所有方面所做的工作:
int topLeftChunkIndex = characterX - (chunkWidth / 2) + ((characterY - (chunkHeight / 2)) * mapWidth);
int topRightChunkIndex = topLeftChunkIndex + chunkWidth - 1;
//int bottomRightChunkIndex = characterX + (chunkWidth / 2) + ((characterY + (chunkHeight / 2)) * mapWidth);
//int bottomLeftChunkIndex = bottomRightChunkIndex - chunkWidth + 1;
int[] leftChunkSides = new int[chunkHeight];
int[] rightChunkSides = new int[chunkHeight];
for (int i = 0; i < chunkHeight; i++){
leftChunkSides[i] = topLeftChunkIndex + (mapWidth * i);
rightChunkSides[i] = topRightChunkIndex + (mapWidth * i);
}
这是我后来检查的方式:
public static boolean isInsideMapChunk(int n, int[] leftChunkSides, int[] rightChunkSides) {
for (int i = 0; i < chunkHeight; i++){
if (n >= leftChunkSides[i] && n <= rightChunkSides[i]){
return true;
}
}
return false;
}