在 Java 中从 txt 文件创建迷宫时,是什么导致字符丢失和起始位置不正确?

问题描述 投票:0回答:3

将 txt 文件转换为二维字符数组并获取特定字符的索引。

嗨!我有这个任务,我需要读取 txt 文件的地图并找到最短路线。 到目前为止,我已将 txt 文件转换为 char 数组,但如果我在控制台上打印地图,则会丢失一些字符。出于某种原因我不明白,我没有得到正确的起点位置。

我的想法是使用我在二元迷宫中发现的相同逻辑。如果你读了这篇文章并认为我在做一些完全愚蠢的事情,那么请告诉我 :D

首先我创建了两个方法来获取地图中有多少行和列。

private static int getRowsOfTheMap() throws IOException {
    int rows = 0;
    BufferedReader reader = getReader(filePath);
    while (reader.readLine() != null) rows++;
    reader.close();
    return rows;
}

private static int getColumnsOfTheMap() throws IOException {
    BufferedReader reader = getReader(filePath);
    String firstRow = reader.readLine();

    return firstRow.length();
}

然后我创建了一个方法来获取地图作为二维字符数组

private static char[][] getMapFromFile() throws IOException {

    char[][] map = new char[getRowsOfTheMap()][getColumnsOfTheMap()];

    BufferedReader reader = getReader(filePath);

    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[0].length; j++) {
            map[i][j] = (char) reader.read();
        }
    }
    return map;
}

以及获取起点的方法

private static int[] getStartingPoint(char[][] map) {
    int[] startingPoint = {
        -1,
        -1
    };
    for (int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[i].length; j++) {
            if (map[i][j] == 'X') {
                startingPoint[0] = i;
                startingPoint[1] = j;
            }
        }
    }
    return startingPoint;
}

来自 txt 文件和地图输出的地图。

预期的位置是 [1,2] 实际是 [1,3]

java maze
3个回答
0
投票
public static char[][] readMaze(Path file) throws IOException {
    return Files.lines(file).map(String::toCharArray).toArray(char[][]::new);
}

无需任何标题即可阅读您的迷宫。只需存储迷宫本身。

你的问题来自阅读行分隔符。这些存在,因为

readLine
调用没有消耗


0
投票

您的问题似乎出在与读取文件并将其存储在地图中相关的逻辑上。

您可以简单地读取文本文件并存储所有行。

那么,总行数=行数

总列数 = 第一行的大小

这样,您就不必多次打开文件了。

关于读取每一行以存储在 char 数组中 =>

由于您的输入包含空格和换行符,因此最好使用

reader.readline()
而不是
reader.read()
.

从下面阅读有关 read() 和 readLine() 的更多信息:

BufferedReader read() 不工作

BufferedReader 不会读取输入的最后一行

但是,您也可以使用更简单的逻辑,例如下面的示例代码来读取文件并存储它:

private static char[][] getMapFromFile() throws IOException {

    List < String > allLines = Files.readAllLines(Paths.get(filePath));
    int totalRows = allLines.size();
    int totalColumns = allLines.get(0).length();

    char[][] map = new char[totalRows][totalColumns];

    for (int i = 0; i < totalRows; i++) {
        String currentLine = allLines.get(i);
        for (int j = 0; j < totalColumns; j++) {
            map[i][j] = currentLine.charAt(j);
        }
    }
    return map;
}

0
投票

我能够生成正确的地图,并且

[1, 2]
使用以下代码作为起点;改编自你的。

我创建了一个

Maze
类,使用
map
方法来填充
map
rows
columns
字段。
此外,我创建了一个
startingPoint
方法来返回起点行和列索引的
int[]

最后,我创建了一个重写的
toString
方法来打印迷宫。

您可以利用

Arrays.binarySearch
找到
X
.

class Maze {
    File file;
    char[][] map;
    int rows, columns;

    Maze(String path) throws IOException {
        file = new File(path);
        map();
    }

    void map() throws IOException {
        StringBuilder string = new StringBuilder();
        String line;
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String newline = System.lineSeparator();
            rows = 0;
            while ((line = reader.readLine()) != null) {
                string.append(line).append(newline);
                rows++;
                columns = line.length();
            }
        }
        BufferedReader reader = new BufferedReader(new StringReader(string.toString()));
        map = new char[rows][columns];
        int index = 0;
        while ((line = reader.readLine()) != null)
            map[index++] = line.toCharArray();
    }

    int[] startPoint() {
        int index = 0;
        int column;
        for (char[] row : map) {
            if ((column = Arrays.binarySearch(row, 'X')) > 0)
                return new int[] { index, column };
            index++;
        }
        return null;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        String newline = System.lineSeparator();
        for (char[] row : map)
            string.append(row).append(newline);
        return string.toString();
    }
}
11111
1 X 1
1 1 1
1   1
111 1

[1, 2]
© www.soinside.com 2019 - 2024. All rights reserved.