This commit is contained in:
muskenum 2016-05-01 14:56:24 +02:00
commit 296fa45c54
20 changed files with 9824 additions and 150 deletions

5
ZPMTest.dungeon Normal file
View File

@ -0,0 +1,5 @@
4x3
W W W W
W O Z W
W W W W

View File

@ -28,11 +28,18 @@ public class Dungeon {
* O: Field with ONeill * O: Field with ONeill
* J: Field with Jaffa * J: Field with Jaffa
* G: Gate * G: Gate
* S: Scale */ * S: Scale
* X: Gap
* R: Replicator */
Map<String, Tile> buildDungeon(File input) throws IOException Map<String, Tile> buildDungeon(File input) throws IOException
{ {
Tile oneilllocation = null; Tile defaultTile = new Field();
Tile jaffalocation = null; defaultTile.setX(-666);
defaultTile.setY(-666);
Tile oneilllocation = defaultTile;
Tile jaffalocation = defaultTile;
Tile replicatorlocation = defaultTile;
try(BufferedReader reader = new BufferedReader(new FileReader(input))) try(BufferedReader reader = new BufferedReader(new FileReader(input)))
{ {
String[] sizedata = reader.readLine().split("x"); // read size data at beginning of file String[] sizedata = reader.readLine().split("x"); // read size data at beginning of file
@ -51,49 +58,57 @@ public class Dungeon {
line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces
for (int x = 0; x < width; ++x) for (int x = 0; x < width; ++x)
{ {
Tile tile = null;
switch (line.charAt(x)) // set the dungeon up switch (line.charAt(x)) // set the dungeon up
{ {
case 'W': case 'W':
dungeon[y][x] = new Wall(); tile = new Wall();
break; break;
case 'F': case 'F':
dungeon[y][x] = new Field(); tile = new Field();
break; break;
case 'Z': case 'Z':
Field zpmfield = new Field(); tile = new Field();
zpmfield.setZPMOnTile(new ZPM()); tile.setZPMOnTile(new ZPM());
dungeon[y][x] = zpmfield;
break; break;
case 'B': case 'B':
Field boxfield = new Field(); tile = new Field();
boxfield.putABox(new Box()); tile.putABox(new Box());
dungeon[y][x] = boxfield;
break; break;
case 'O': case 'O':
Field oneillfield = new Field(); tile = new Field();
dungeon[y][x] = oneillfield; oneilllocation = tile;
oneilllocation = oneillfield;
break; break;
case 'J': case 'J':
Field jaffafield = new Field(); tile = new Field();
dungeon[y][x] = jaffafield; jaffalocation = tile;
jaffalocation = jaffafield;
break; break;
case 'G': case 'G':
dungeon[y][x] = tempgate; tile = tempgate;
break; break;
case 'S': case 'S':
dungeon[y][x] = tempscale; tile = tempscale;
scalecount++; scalecount++;
break; break;
case 'X':
tile = new Gap();
break;
case 'R':
tile = new Field();
replicatorlocation = tile;
break;
} }
tile.setY(y); tile.setX(x);
dungeon[y][x] = tile;
} }
} }
@ -103,9 +118,21 @@ public class Dungeon {
{ {
String[] scaledata = reader.readLine().split("-"); String[] scaledata = reader.readLine().split("-");
dungeon[Integer.parseInt(scaledata[0])][Integer.parseInt(scaledata[1])] = int sy = Integer.parseInt(scaledata[0]);
new Scale((Gate)dungeon[Integer.parseInt(scaledata[2])][Integer.parseInt(scaledata[3])], int sx = Integer.parseInt(scaledata[1]);
Integer.parseInt(scaledata[4])); int gy = Integer.parseInt(scaledata[2]);
int gx = Integer.parseInt(scaledata[3]);
int triggerweight = Integer.parseInt(scaledata[4]);
Gate gate = new Gate();
gate.setY(gy);
gate.setX(gx);
Scale scale = new Scale(gate, triggerweight);
scale.setY(sy);
scale.setX(sx);
dungeon[sy][sx] = scale;
} }
/* setting up Tile cross references */ /* setting up Tile cross references */
@ -139,6 +166,7 @@ public class Dungeon {
Map<String, Tile> playermap = new HashMap<>(); Map<String, Tile> playermap = new HashMap<>();
playermap.put("oneill", oneilllocation); playermap.put("oneill", oneilllocation);
playermap.put("jaffa", jaffalocation); playermap.put("jaffa", jaffalocation);
playermap.put("replicator", replicatorlocation);
return playermap; return playermap;
} }

View File

@ -1,129 +1,65 @@
package cicaprojekt; package cicaprojekt;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException; import java.io.IOException;
import java.util.Scanner; import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
public class Menu { public class Menu {
public static String tabulator = "\t"; public static void main(String[] args) throws IOException, InvocationTargetException, IllegalAccessException {
System.out.println("Continuously Integrated Cica Projekt - Proto");
public static void addTab() {
tabulator += '\t';
}
public static void removeTab() {
tabulator = tabulator.substring(0, tabulator.length() - 1);
}
public static void main(String[] args) throws IOException {
System.out.println("Continuously Integrated Cica Projekt - Skeleton");
System.out.println("Üdvözöllek a Babylon Simulator 2000 játékban! Kérlek válassz egy menüpontot!"); System.out.println("Üdvözöllek a Babylon Simulator 2000 játékban! Kérlek válassz egy menüpontot!");
Tester tester = new Tester();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean isExiting = false; boolean isExiting = false;
while (!isExiting) { while (!isExiting) {
System.out.println("1. Lépés"); try {
System.out.println("2. Doboz felvétele"); String[] input = br.readLine().split(" ");
System.out.println("3. Doboz lerakása"); switch (input[0]) {
System.out.println("4. Elforgatás"); case "loadMap":
System.out.println("5. Nézés"); tester.loadMap(input[1]);
System.out.println("6. Csillagkapu lövés"); break;
System.out.println("X. Kilépés"); case "listPlayers":
System.out.println(tester.listPlayers());
Player oNeill = new Player(new Field(), Direction.NORTH); break;
case "move":
Scanner sc = new Scanner(System.in); tester.move(input[1]);
break;
switch (sc.nextLine().charAt(0)) { case "boxLift":
case '1': System.out.println(tester.boxLift(input[1]));
System.out.println("ONeill [északi|nyugati|déli|keleti] irányba lép egyet,"); break;
System.out.println("Elfogadott bemenet: W, A, S, D"); case "boxDrop":
switch (sc.nextLine().charAt(0)) { System.out.println(tester.boxDrop(input[1]));
case 'W': break;
oNeill.move(Direction.NORTH); case "shootONeillsGun":
break; tester.shootONeillsGun(input[1]);
case 'A': break;
oNeill.move(Direction.WEST); case "rotate":
break; tester.rotate(input[1], input[2]);
case 'S': break;
oNeill.move(Direction.SOUTH); case "listStargates":
break; System.out.println(tester.listStargates());
case 'D': break;
oNeill.move(Direction.EAST); case "runAllTests":
break; boolean testresult = tester.runAllTests();
case 'X': if (testresult)
break; System.out.println("All tests successful!");
} else
break; System.out.println("Tests failed!");
case '2': break;
System.out.println("Doboz felvétele"); case "exit":
System.out.println("Elfogadott bemenet: L"); isExiting = true;
switch (sc.nextLine().charAt(0)) { break;
case 'L': }
oNeill.boxLift(); } catch (Exception e) {
break; e.printStackTrace();
case 'X': //TODO not so granular error handling
break;
}
break;
case '3':
System.out.println("Doboz lerakása");
System.out.println("Elfogadott bemenet: D");
switch (sc.nextLine().charAt(0)) {
case 'D':
oNeill.boxDrop();
break;
case 'X':
break;
}
break;
case '4':
System.out.println("Elforgás");
System.out.println("Elfogadott bemenet: L, R");
switch (sc.nextLine().charAt(0)) {
case 'L':
oNeill.rotateLeft();
break;
case 'D':
oNeill.rotateRight();
break;
case 'X':
break;
}
break;
case '5':
System.out.println("Nézés");
System.out.println("Elfogadott bemenet: W");
switch (sc.nextLine().charAt(0)) {
case 'W':
Tile t = oNeill.getCurrentTile().getAdjacentTile(oNeill.getFacingDirection());
System.out.println("O'Neill előtt egy " + t.toString() + "mező található");
break;
case 'X':
break;
}
break;
case '6':
System.out.println("Csillagkapu lövés");
System.out.println("Elfogadott bemenet: Y, B");
Tile t = oNeill.getCurrentTile();
switch (sc.nextLine().charAt(0)) {
case 'Y':
t.spawnStargate(Stargate.yellowStargate, oNeill.getFacingDirection());
break;
case 'B':
t.spawnStargate(Stargate.blueStargate, oNeill.getFacingDirection());
break;
case 'X':
break;
}
break;
case 'X':
System.out.println("Kilépés");
isExiting = true;
break;
} }
} }
} }
} }

View File

@ -8,10 +8,9 @@ public class Player extends PlayerBase {
private Box boxLifted; private Box boxLifted;
public Player(Tile startTile, Direction startDirection) { public Player(String name, Tile startTile, Direction startDirection) {
super(name, startTile, startDirection);
zpmContainer = new ArrayList<>(); zpmContainer = new ArrayList<>();
currentTile = startTile;
facingDirection = startDirection; /* Be lehetne állítani egy defaultot is, nem tudom, mennyire kéne */
} }
public void boxLift() { public void boxLift() {
@ -31,4 +30,8 @@ public class Player extends PlayerBase {
public void shootStargate(Stargate stargate) { public void shootStargate(Stargate stargate) {
this.currentTile.getAdjacentTile(facingDirection).spawnStargate(stargate, facingDirection); this.currentTile.getAdjacentTile(facingDirection).spawnStargate(stargate, facingDirection);
} }
public int getZPMCount(){
return zpmContainer.size();
}
} }

View File

@ -4,9 +4,23 @@ public class PlayerBase implements Destroyable {
protected Game game; protected Game game;
protected Tile currentTile; protected Tile currentTile;
protected Direction facingDirection; protected Direction facingDirection;
protected String name;
protected boolean destroyed;
public PlayerBase(String name, Tile startTile, Direction startDirection) {
this.name = name;
currentTile = startTile;
facingDirection = startDirection;
destroyed = false;
}
@Override
public String toString() {
return String.format("%s: %s", name, currentTile);
}
public void destroy() { public void destroy() {
destroyed = true;
} }
public Tile getCurrentTile() { public Tile getCurrentTile() {
@ -67,4 +81,8 @@ public class PlayerBase implements Destroyable {
public void setFacingDirection(Direction direction) { public void setFacingDirection(Direction direction) {
facingDirection = direction; facingDirection = direction;
} }
public boolean isDestroyed(){
return destroyed;
}
} }

View File

@ -52,4 +52,8 @@ public class Scale extends Field {
else else
gateConnected.setOpen(false); gateConnected.setOpen(false);
} }
public Gate getGateConnected() {
return gateConnected;
}
} }

View File

@ -1,17 +1,19 @@
package cicaprojekt; package cicaprojekt;
public class Stargate { public class Stargate {
public static final Stargate yellowStargate = new Stargate(); public static final Stargate yellowStargate = new Stargate("Yellow Stargate");
public static final Stargate blueStargate = new Stargate(); public static final Stargate blueStargate = new Stargate("Blue Stargate");
public static final Stargate redStargate = new Stargate(); public static final Stargate redStargate = new Stargate("Red Stargate");
public static final Stargate greenStargate = new Stargate(); public static final Stargate greenStargate = new Stargate("Green Stargate");
public /*final*/ Stargate other; //TODO find better ways to do this public /*final*/ Stargate other; //TODO find better ways to do this
private boolean isSpawned; private boolean isSpawned;
private Wall currentWall; private Wall currentWall;
private String name;
private Stargate() { private Stargate(String name) {
isSpawned = false; isSpawned = false;
this.name = name;
} }
public static void init() { public static void init() {
@ -27,12 +29,37 @@ public class Stargate {
public void setCurrentWall(Wall wall) { public void setCurrentWall(Wall wall) {
currentWall = wall; currentWall = wall;
if(wall != null) {
isSpawned = true;
}
else {
isSpawned = false;
}
} }
public boolean isOpen() { public boolean isOpen() {
return isSpawned; return isSpawned & other.isSpawned;
} }
public void teleport(Direction incomingDirection) { private Direction getExitDirection() {
if(currentWall.getAdjacentTile(Direction.EAST) == null)
return Direction.WEST;
else if(currentWall.getAdjacentTile(Direction.WEST) == null)
return Direction.EAST;
else if(currentWall.getAdjacentTile(Direction.NORTH) == null)
return Direction.SOUTH;
else
return Direction.NORTH;
}
public void teleport(PlayerBase player) {
player.setCurrentTile(other.getCurrentWall().getAdjacentTile(getExitDirection()));
}
@Override
public String toString() {
if(isSpawned)
return String.format("%s: %s", name, currentWall);
else return String.format("%s: not spawned", name, currentWall);
} }
} }

286
cicaprojekt/Tester.java Normal file
View File

@ -0,0 +1,286 @@
package cicaprojekt;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
public class Tester {
private Player oneill;
private Player jaffa;
private PlayerBase replicator;
Dungeon loadMap(String param) throws IOException{
Dungeon dungeon = new Dungeon();
Map playerLocations = dungeon.buildDungeon(new File(param));
oneill = new Player("ONeill", (Tile) playerLocations.get("oneill"), Direction.NORTH);
jaffa = new Player("Jaffa", (Tile) playerLocations.get("jaffa"), Direction.NORTH);
replicator = new PlayerBase("Replicator", (Tile) playerLocations.get("replicator"), Direction.NORTH);
return dungeon;
}
String listPlayers() {
return oneill.toString() + " " + jaffa.toString() + " " + replicator.toString();
}
void move(String param) {
switch (param) {
case "O" :
oneill.move(oneill.getFacingDirection());
break;
case "J" :
jaffa.move(jaffa.getFacingDirection());
break;
default:
//TODO error handling
break;
}
}
String boxLift(String param) {
switch (param) {
case "O" :
oneill.boxLift();
break;
case "J" :
jaffa.boxLift();
break;
}
return "box lifted";
}
String boxDrop(String param) {
switch (param) {
case "O" :
oneill.boxDrop();
break;
case "J" :
jaffa.boxDrop();
break;
}
return "box dropped";
}
void shootONeillsGun(String param) {
switch (param) {
case "B" :
oneill.shootStargate(Stargate.blueStargate);
break;
case "Y" :
oneill.shootStargate(Stargate.yellowStargate);
break;
}
}
String rotate(String playerParam, String directionParam) {
switch (playerParam) {
case "O" :
switch (directionParam) {
case "L" :
oneill.rotateLeft();
return oneill.getFacingDirection().toString();
case "R" :
oneill.rotateRight();
return oneill.getFacingDirection().toString();
default: return "Hiba, nem fordult!";
}
case "J" :
switch (directionParam) {
case "L" :
jaffa.rotateLeft();
return jaffa.getFacingDirection().toString();
case "R" :
jaffa.rotateRight();
return jaffa.getFacingDirection().toString();
default: return "Hiba, nem fordult!";
}
default: return "Hiba, nem létező játékos!";
}
}
String listStargates() {
String blue = Stargate.blueStargate.toString();
String yellow = Stargate.yellowStargate.toString();
String red = Stargate.redStargate.toString();
String green = Stargate.greenStargate.toString();
return blue + " " + yellow + " " + red + " " + green;
}
String getConnectedGateOpen(Scale s){
if(s.getGateConnected().isOpen())
return "gate open";
else return "gate closed";
}
/* custom Test annotation */
@Target(ElementType.METHOD) // it's for methods
@Retention(RetentionPolicy.RUNTIME) // we want to retain annotations in runtime
private @interface Test {}
/* run all methods annotated with @Test */
public boolean runAllTests() throws InvocationTargetException, IllegalAccessException
{
boolean testspassed = true;
for (Method m : this.getClass().getMethods()) // iterate over all methods of this
{
if (m.isAnnotationPresent(Test.class)) // if its annotated with @Test...
{
Boolean testresult = (Boolean) m.invoke(null); // call it!
if (!testresult)
{
testspassed = false;
}
}
}
return testspassed;
}
@Test
boolean moveTest() throws IOException{
boolean success = true;
loadMap("moveTest.dungeon");
String listOfPlayers = listPlayers();
if(!listOfPlayers.equals("ONeill: 1, 1 Jaffa: -666, -666 Replicator: -666, -666"))
success = false;
move("O");
listOfPlayers = listPlayers();
if(!listOfPlayers.equals("ONeill: 1, 2 Jaffa: -666, -666 Replicator: -666, -666"))
success = false;
return success;
}
@Test
boolean testStargates() throws IOException{
boolean success = true;
loadMap("testStargates.dungeon");
shootONeillsGun("B");
rotate("O", "L");
shootONeillsGun("Y");
String listOfStargates = listStargates();
if(!listOfStargates.equals("BlueStargate: 5, 10 YellowStargate: 10, 5 RedStargate: not spawned GreenStargate: not spawned"))
success = false;
move("O");
move("O");
move("O");
String listOfPlayers = listPlayers();
if(!listOfPlayers.equals("ONeill: 5, 9 Jaffa: -666, -666 Replicator: -666, -666"))
success = false;
return success;
}
@Test
boolean testScalesAndGates() throws IOException {
boolean success = true;
loadMap("testScalesAndGates.dungeon");
boxLift("O");
rotate("O", "L");
boxDrop("O");
String gateOpen = getConnectedGateOpen((Scale)oneill.getCurrentTile().getAdjacentTile(oneill.facingDirection));
if(!gateOpen.equals("gate open"))
success = false;
return success;
}
@Test
boolean gapTest() throws IOException{
loadMap("gapTest.dungeon");
move("O");
boolean success = oneill.isDestroyed();
return success;
}
@Test
boolean ZPMTest() throws IOException{
boolean success = true;
loadMap("ZPMTest.dungeon");
move("O");
if(oneill.getZPMCount() != 1)
success = false;
return success;
}
@Test
boolean testReplicatorPosition(){
String[] commands = {"loadMap testReplicatorPosition\n",
"move O\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
return results;
}
@Test
boolean timeUpTest(){
String[] commands = {"loadMap timeUpTest\n"};
String[] expectedOutputs = {};
boolean results = testOnSequenceOfCommands(commands, expectedOutputs);
return results;
}
@Test
boolean rotationTest() throws IOException {
boolean success = true;
loadMap("rotationTest.dungeon");
if(!rotate("O", "L").equals("WEST"))
success = false;
if(!rotate("O", "R").equals("NORTH"))
success = false;
return success;
}
@Test
boolean testBoxes() throws IOException{
boolean success = true;
loadMap("testBoxes.dungeon");
if(!boxLift("O").equals("box lifted"))
success = false;
if(!boxDrop("O").equals("box dropped"))
success = false;
return success;
}
private boolean testOnSequenceOfCommands(String[] commands, String[] expectedOutputs) {
return true;
}
}

View File

@ -8,10 +8,27 @@ public abstract class Tile {
protected Map<Direction, Tile> adjacentTile; protected Map<Direction, Tile> adjacentTile;
protected ZPM zpmOnTile; protected ZPM zpmOnTile;
protected Stack<Box> boxStack; protected Stack<Box> boxStack;
protected int y = -666;
protected int x = -666;
public Tile() { public Tile() {
adjacentTile = new HashMap<Direction, Tile>(); adjacentTile = new HashMap<Direction, Tile>();
boxStack = new Stack<>();
}
public int getX() { return this.x; }
public int getY() { return this.y; }
public void setX(int x) {
if (x >= 0)
this.x = x;
}
public void setY(int y) {
if (y >= 0)
this.y = y;
} }
public Tile getAdjacentTile(Direction direction) { public Tile getAdjacentTile(Direction direction) {
@ -49,4 +66,9 @@ public abstract class Tile {
return null; return null;
return boxStack.pop(); return boxStack.pop();
} }
@Override
public String toString() {
return String.format("%d, %d", x, y);
}
} }

View File

@ -8,21 +8,28 @@ public class Wall extends Tile {
} }
public void spawnStargate(Stargate stargate, Direction direction) { public void spawnStargate(Stargate stargate, Direction direction) {
if (sg == null) clearStargate();
if (sg == null) {
sg = stargate; sg = stargate;
sg.setCurrentWall(this);
}
else else
return; return;
} }
public void clearStargate() { public void clearStargate() {
sg = null; if(sg != null) {
sg.setCurrentWall(null);
sg = null;
}
} }
public void onEntry(PlayerBase playerBase) { public void onEntry(PlayerBase playerBase) {
if (sg == null) { if (sg == null) {
return; return;
} else { } else {
sg.teleport(playerBase.facingDirection); sg.teleport(playerBase);
} }
} }

6
gapTest.dungeon Normal file
View File

@ -0,0 +1,6 @@
4x3
W W W W
W O X W
W W W W

9283
graphicsclasses_diagram.mdj Normal file

File diff suppressed because it is too large Load Diff

8
moveTest.dungeon Normal file
View File

@ -0,0 +1,8 @@
5x5
W W W W W
W O F F W
W F F F W
W F F F W
W W W W W

5
rotate.dungeon Normal file
View File

@ -0,0 +1,5 @@
3x3
W W W
W O W
W W W

6
testBoxes.dungeon Normal file
View File

@ -0,0 +1,6 @@
4x4
W W W W
W O B W
W F F W
W W W W

View File

@ -0,0 +1,5 @@
4x3
W W W W
W O X W
W W W W

View File

@ -0,0 +1,6 @@
4x4
W W W W
W R O W
W S B W
W W W W

View File

@ -0,0 +1,8 @@
5x4
W W W W W
W O F F G
W F S B W
W W W W W
2-2-1-4-1

6
testStargates.dungeon Normal file
View File

@ -0,0 +1,6 @@
5x4
W W W W W
W O F F W
W F F F W
W W W W W

5
timeUpTest.dungeon Normal file
View File

@ -0,0 +1,5 @@
4x3
X X X X
X O X Z
X X X X