From 1b829fba1e146424bf764914e92ebf803c2d4d21 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Mon, 30 Dec 2019 16:04:19 +0100 Subject: [PATCH] BreakOut 4.* Vorgabe --- src/break_out/controller/JSONReader.java | 111 +++++++++++++++++++++++ src/break_out/model/IBall.java | 6 ++ src/break_out/model/ILevel.java | 5 + 3 files changed, 122 insertions(+) create mode 100644 src/break_out/controller/JSONReader.java diff --git a/src/break_out/controller/JSONReader.java b/src/break_out/controller/JSONReader.java new file mode 100644 index 0000000..1c2279e --- /dev/null +++ b/src/break_out/controller/JSONReader.java @@ -0,0 +1,111 @@ +package break_out.controller; + + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import break_out.Constants; + +/** + * The JSONReader reads the content of an json file. + * + * @author dmlux, modified by I.Schumacher + * + */ +public class JSONReader { + + /** + * The project path to the JSON file + */ + private String path; + + /** + * The stones stored as List> + */ + private List> rects = new ArrayList>(); + + /** + * The stones stored as 2D-int-array + */ + private int[][] stones = new int[Constants.SQUARES_Y][Constants.SQUARES_X]; + + /** + * The counter with the number of trials that are allowed to break out the stones of the level + */ + private Long lifecount = null; + + /** + * The constructor needs an path to create the JSONReader + * + * @param path + * The absolute path to the JSON file + */ + public JSONReader(String path) { + this.path = path; + loadJsonValues(); + } + + /** + * Getter for the stones of the JSON file + * + * @return The List> of stones + */ + public List> getStonesListOfLists() { + return rects; + } + + /** + * Getter for the stones of the JSON file + * + * @return The stones as 2D-Array + */ + public int[][] getStones2DArray() { + for (int i = 0; i < rects.size(); i++) { + for (int j = 0; j < rects.get(i).size(); j++) { + stones[i][j] = (int)rects.get(i).get(j).longValue(); + } + } + return stones; + } + + /** + * Getter for the lifeCounter of the JSON file + * + * @return The lifeCounter + */ + public int getLifeCounter() { + return (int)lifecount.longValue(); + } + + /** + * Loader for the both values "fields" and "maxDrops" of the JSON file + * + */ + @SuppressWarnings("unchecked") + private void loadJsonValues() { + JSONParser parser = new JSONParser(); + try { + Object obj = parser.parse(new FileReader(path)); + + String jsonStr = obj.toString(); + JSONObject json = (JSONObject) JSONValue.parse(jsonStr); + rects = (List>) json.get("field"); + lifecount = (Long)json.get("maxLives"); + + } catch (ParseException ex) { + ex.printStackTrace(); + } catch (FileNotFoundException ex) { + ex.printStackTrace(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/break_out/model/IBall.java b/src/break_out/model/IBall.java index 8474dec..b2078ce 100644 --- a/src/break_out/model/IBall.java +++ b/src/break_out/model/IBall.java @@ -1,5 +1,7 @@ package break_out.model; +import java.util.ArrayList; + public interface IBall { // Exercise 1 @@ -11,4 +13,8 @@ public interface IBall { // Exercise 2 public boolean hitsPaddle(Paddle paddle); public void reflectOnPaddle(Paddle paddle); + + // Exercise 4 + public boolean hitsStone(ArrayList stones); + public Stone getHitStone(); } \ No newline at end of file diff --git a/src/break_out/model/ILevel.java b/src/break_out/model/ILevel.java index d3e55b5..f3fc306 100644 --- a/src/break_out/model/ILevel.java +++ b/src/break_out/model/ILevel.java @@ -1,5 +1,7 @@ package break_out.model; +import java.util.ArrayList; + public interface ILevel { // Exercise 1 public Ball getBall(); @@ -10,4 +12,7 @@ public interface ILevel { // Exercise 3 public void setFinished(boolean finished); + + // Exercise 4 + public ArrayList getStones(); } \ No newline at end of file