我不得不用Java为学校的一个项目编写一个小游戏。游戏是尖峰时刻,你可以在这里看到一个例子:http://www.thinkfun.com/play-online/rush-hour/。
我的老师现在要求我允许我的代码读取外部文件,这样游戏的初始状态就不再在我的main中硬编码,并且可以自定义游戏的参数(棋盘的大小,游戏中的汽车数量......
这是我的第一个JSON文件,它设置了一个经典游戏。
{
"ClassicBoard" :
{
"height" : "6",
"width" : "6",
"exit":
{
"row" : "2",
"column" : "5"
}
},
"ListOfCars" :
{
"car2" :
{
"char" : "2",
"size" : "3",
"orientation" : "vertical",
"currentPosition":
{
"row" : "2",
"column": "2"
}
},
"car3" :
{
"char" : "3",
"size" : "3",
"orientation" : "vertical",
"currentPosition":
{
"row" : "2",
"column": "4"
}
}
},
"redCar":
{
"char" : "1",
"size" : "2",
"orientation" : "horizontal",
"currentPosition":
{
"row" : "2",
"column": "0"
}
}
}
我正在尝试找到如何读取文件并重用它的输出来创建 RushHourGame 对象。下面是构造函数。
public RushHourGame(Board board, List <Car> cars, Car redCar) throws RushHourException
{
this.board = board;
this.redCar = redCar;
if(((redCar.getOrientation() == Orientation.HORIZONTAL)
&& (board.getExit().getRow() != redCar.getCurrentPosition().getRow()))
|| (((redCar.getOrientation() == Orientation.VERTICAL)
&& (board.getExit().getColumn() != redCar.getCurrentPosition().getColumn()))))
{
throw new RushHourException("Car must be aligned with the exit, "
+ "a default game has been created");
}
board.put(redCar);
for (Car putCars : cars)
{
board.put(putCars);
}
}
我尝试使用BufferedReader,就像这样。
public static String readFile(String filename)
{
String result ="";
try
{
BufferedReader br = new BufferedReader (new FileReader(filename));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
line = br.readLine();
}
result = sb.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
但我并不怀疑如何使用它来解析我的 JSON 文件。有人可以帮助我吗?
可以在此处找到您问题的多个答案。我更喜欢StaxMan提供的答案:
最简单的选择是杰克逊:
MyObject ob = new ObjectMapper().readValue(jsonString, RushHourState.class);
上面的代码应该使用下面提供的Java POJO自动处理JSON/Java对象绑定:
public class RushHourState {
private ArrayList<Car> listOfCars;
private ClassicBoard classicBoard;
private Car redCar;
public ArrayList<Car> getListOfCars() {
return listOfCars;
}
public void setListOfCars(ArrayList<Car> listOfCars) {
this.listOfCars = listOfCars;
}
public ClassicBoard getClassicBoard() {
return classicBoard;
}
public void setClassicBoard(ClassicBoard classicBoard) {
this.classicBoard = classicBoard;
}
public Car getRedCar() {
return redCar;
}
public void setRedCar(Car redCar) {
this.redCar = redCar;
}
}
// ---------------------------------------------------
public class ClassicBoard {
private int height;
private int width;
private HashMap<String, String> exit;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public HashMap<String, String> getExit() {
return exit;
}
public void setExit(HashMap<String, String> exit) {
this.exit = exit;
}
}
// ---------------------------------------------------
public class Car {
private int charr;
private int size;
private String orientation;
private HashMap<String, String> currentPosition;
public int getCharr() {
return charr;
}
public void setCharr(int charr) {
this.charr = charr;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getOrientation() {
return orientation;
}
public void setOrientation(String orientation) {
this.orientation = orientation;
}
public HashMap<String, String> getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(HashMap<String, String> currentPosition) {
this.currentPosition = currentPosition;
}
}
注意:我将 Car 类中的变量"char"重命名为"charr",因为 char 是 Java 保留关键字。希望对您有所帮助!