0

I had to program a little game in Java for a project for school. The game is RushHour, and you can see an example of it here: http://www.thinkfun.com/play-online/rush-hour/.

My teachers ask me now to allow my code to read an external file so that the initial state of the game is no longer hard coded in my main, and that the parameters of the game can be customized (the size of the board, the number of the cars in the game...)

Here's my first JSON file, which sets a classic game.

{
"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"
            }
        }

}

I'm trying to find how to read the file and to reuse it's output for creating a RushHourGame object. Here's the constructor.

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);
    }
}

I tried to use a BufferedReader, like this.

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;
}

But I am not sur how to use it for parsing my JSON file. Somebody can help me?

1 Answers1

0

Multiple answers to your question can be found here. I prefer the answer provided by StaxMan:

The simplest option is Jackson:

MyObject ob = new ObjectMapper().readValue(jsonString, RushHourState.class);

The above code should automatically handle the JSON/Java Object binding, using the Java POJOs provided below:

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;
    }
}

Note: I renamed the variable 'char' in the Car class to 'charr' because char is a Java reserved keyword. Hope you find this helpful!

Community
  • 1
  • 1
  • Thanks for answering. Your answer looks very interresting, I'll check that in detail as soon as I can and I come back to tel you if it worked ! – T. Labrador Ruiz Apr 25 '16 at 20:20
  • Well, I think the code is not very helpful because I have already made my classes for Car, Board, Position... And what you made doesn't suits with it. Plus, I think using setters is not the solution, because I don't want the user to have the possibility of changing these values. The constructor of the class RushHourGame calls the cnstructor of all the other classes, so, I just need the constructor of RushHourGame to retrieve the datas and values in the JSON file, and to make a new RushHourGame object with it. But thanks for the Jackson solution. – T. Labrador Ruiz Apr 26 '16 at 08:32