1
0
Fork 0

BreakOut 4.* Vorgabe

This commit is contained in:
rxbn_ 2019-12-30 16:04:19 +01:00
parent 4bec1047ed
commit 1b829fba1e
3 changed files with 122 additions and 0 deletions

View File

@ -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 <code>JSONReader</code> 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<List<Long>>
*/
private List<List<Long>> rects = new ArrayList<List<Long>>();
/**
* 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<List<Long>> of stones
*/
public List<List<Long>> 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<List<Long>>) json.get("field");
lifecount = (Long)json.get("maxLives");
} catch (ParseException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

View File

@ -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<Stone> stones);
public Stone getHitStone();
}

View File

@ -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<Stone> getStones();
}