gpt4 book ai didi

java - 从文本文件实现数据(迷宫)

转载 作者:行者123 更新时间:2023-12-01 11:31:36 31 4
gpt4 key购买 nike

所以我正在为我的一个类(class)开发一个项目,我们需要实现一种方法:

public void readMazeFromFile(String fileName) throws IOException,FileNotFoundException, MazeReadException {}

顾名思义,我们需要读取 .txt 文件并相应地设置迷宫。文件的数据存储如下:

5,5 //row, col for maze
Square,0,0,true,false,false,true,true,true //Object, row, col, other values
Square,0,1,true,false,true,false,true,true
Square,0,2,true,false,true,false,false,false
Square,0,3,true,false,false,false,false,false
Square,0,4,true,true,false,false,false,false
Square,1,0,false,false,true,true,true,true
Square,1,1,true,false,true,false,true,true
Square,1,2,true,true,false,false,false,false
Square,1,3,false,true,false,true,false,false
Square,1,4,false,true,false,true,false,false
Square,2,0,true,false,false,true,false,false
Square,2,1,true,false,true,false,false,false
Square,2,2,false,true,false,false,false,false
Square,2,3,false,true,false,true,false,false
Square,2,4,false,true,false,true,false,false
Square,3,0,false,true,false,true,false,false
Square,3,1,true,false,false,true,false,false
Square,3,2,false,true,false,false,false,false
Square,3,3,false,true,true,true,false,false
Square,3,4,false,true,false,true,false,false
Square,4,0,false,true,true,true,false,false
Square,4,1,false,true,true,true,false,false
Square,4,2,false,false,true,true,false,false
Square,4,3,true,false,true,false,false,false
Square,4,4,false,true,true,false,false,false
Explorer,0,0,Scary Name
Treasure,4,4,true
Treasure,2,2,false
Monster,4,4
Monster,3,3

由于某种原因,我实现的方法不起作用。当我运行讲师提供的测试驱动程序时,它说我在第 239 行有一个 nullPointerException ( maze.squares[r][c] = new Square(r, c); )

public void readMazeFromFile(String fileName) throws IOException, FileNotFoundException, MazeReadException {
Scanner scanFile = new Scanner(new File(fileName));

int lineNum = 0;
while (scanFile.hasNextLine()) {
String line = scanFile.nextLine();
Scanner parsed = new Scanner(line).useDelimiter(",");

Maze maze = new Maze();

if (lineNum == 0) {
if (!parsed.hasNextInt()) {
throw new MazeReadException("Rows and columns not specified", line, lineNum);
}

maze.rows = parsed.nextInt();
maze.cols = parsed.nextInt();

maze.squares = new Square[maze.rows][maze.cols];

}
while (parsed.hasNext()) {
if (parsed.next().equals("Square")) {
int r = parsed.nextInt();
int c = parsed.nextInt();
maze.squares[r][c] = new Square(r, c);
maze.squares[r][c].toObject(parsed);
} else {
if (parsed.next().equals("Explorer")) {
maze.explorer = new Explorer(maze);
maze.explorer.toObject(parsed);
} else if (parsed.next().equals("Treasure")) {
Treasure t = new Treasure(maze);
t.toObject(parsed);
maze.randOccupants.add(t);
//maze.randOccupants.get(maze.randOccupants.indexOf(t)).toObject(parsed);
} else if (parsed.next().equals("Monster")) {
Monster m = new Monster(maze);
m.toObject(parsed);
maze.randOccupants.add(m);
//maze.randOccupants.get(maze.randOccupants.indexOf(m)).toObject(parsed);
}
}
}
parsed.close();
lineNum++;
}
scanFile.close();
}

对正在发生的事情有什么想法吗?

最佳答案

在 Maze 的默认构造函数中,您需要初始化方 block 。像这样的事情:

public Maze() {
this.squares = new Square[DIM_X][DIM_Y];// max dimesions of the maze
// same for the other arrays
}

关于java - 从文本文件实现数据(迷宫),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341049/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com