[英]Storing characters from an input maze file into 2d array (Java)
我无法将正确的值转换为二维数组。
我的代码应该解决迷宫。 它从输入的.txt文件中读取迷宫结构,将结构排列成2d数组,然后将迷宫绘制到绘图面板中,然后绘制一个解决方案的动画。 我一直坚持将结构放入数组中。
我的代码从这样的输入文件中读取:
2 3
+-+-+-+
|S| |
+ + + +
| |E|
+-+-+-+
前两个数字是迷宫的高度和宽度。 因此,该迷宫的高度为2行,宽度为3列。
到目前为止,这是我的代码。
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class MazeSolver {
// The name of the file describing the maze
static String mazefile;
static int width;
static int height;
static int arrayWidth;
static int arrayHeight;
static char[][] mazeArrays;
public static void main(String[] args) throws FileNotFoundException {
if (handleArguments(args)) {
mazeArrays = readMazeFile(mazefile, arrayHeight, arrayWidth);
System.out.println(Arrays.deepToString(mazeArrays));
DrawMaze.draw(mazeArrays, height, width);
if (solveMaze())
System.out.println("Solved!");
else
System.out.println("Maze has no solution.");
}
else {
System.out.println("The arguments are invalid.");
}
}
// Handle the input arguments
static boolean handleArguments(String[] args) {
if (args.length > 4 || args.length < 1) {
System.out.println("There are too many or too few command line arguments");
return false;
}
if (args.length == 1) {
mazefile = args[0];
File file = new File(mazefile);
if (!file.canRead()) {
return false;
}
return true;
}
if (args.length == 2) {
mazefile = args[0];
File file = new File(mazefile);
if (!file.canRead()) {
return false;
}
int cellsize = Integer.parseInt(args[1]);
if (cellsize < 10) {
return false;
}
return true;
}
if (args.length == 3) {
mazefile = args[0];
File file = new File(mazefile);
if (!file.canRead()) {
return false;
}
int cellsize = Integer.parseInt(args[1]);
int borderwidth = Integer.parseInt(args[2]);
if (borderwidth < 5) {
return false;
}
return true;
}
if (args.length == 4) {
mazefile = args[0];
File file = new File(mazefile);
if (!file.canRead()) {
return false;
}
int cellsize = Integer.parseInt(args[1]);
int borderwidth = Integer.parseInt(args[2]);
int sleeptime = Integer.parseInt(args[3]);
if (sleeptime < 0 || sleeptime > 10000) {
return false;
}
return true;
}
return false;
}
// Read the file describing the maze.
static char[][] readMazeFile(String mazefile, int arrayHeight, int arrayWidth) throws FileNotFoundException {
Scanner scanner = new Scanner(new File(mazefile));
height = scanner.nextInt();
System.out.println(height);
width = scanner.nextInt();
System.out.println(width);
arrayHeight = 2 * height + 1;
arrayWidth = 2 * width + 1;
char[][] mazeArrays = new char[arrayHeight][arrayWidth];
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
for (int row = 0; row < arrayHeight; row++) {
for (int col = 0; col < line.length(); col++) {
mazeArrays[row][col] = line.charAt(col);
}
}
}return mazeArrays;
}
// Solve the maze.
static boolean solveMaze() {
return true;
}
}
问题出在方法readMazeFile
这是输出:
2
3
+-+-+-+
|S| |
+ + + +
| |E|
+-+-+-+
[[+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +], [+, -, +, -, +, -, +]]
Solved!
看起来我的问题是扫描仪永远不会移动到下一行,但我相信我正在写它。 2d数组正确存储第一行,但不会移动到“| S | |” 之后。
我也期待将白色空间存储到阵列中的问题。
提前致谢!
你在输出中看到的是输入的最后一行,而不是第一行。 这是因为你在迷宫的每一行都存储了每一行:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
for (int row = 0; row < arrayHeight; row++) {
for (int col = 0; col < line.length(); col++) {
mazeArrays[row][col] = line.charAt(col);
}
}
}
我怀疑你打算:
for (int row = 0; row < arrayHeight; row++) {
String line = scanner.nextLine();
System.out.println(line);
for (int col = 0; col < arrayWidth; col++) {
mazeArrays[row][col] = line.charAt(col);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.