From 924a5acc261fc87495bcfaa3f073088767060b46 Mon Sep 17 00:00:00 2001 From: ericnerdo Date: Sun, 24 Apr 2016 22:25:20 +0200 Subject: [PATCH] Tile refactored Every setItemOnTile call were replaced. --- cicaprojekt/Dungeon.java | 62 +++++++--------------------------------- cicaprojekt/Player.java | 4 +-- cicaprojekt/Tile.java | 14 +++++---- 3 files changed, 21 insertions(+), 59 deletions(-) diff --git a/cicaprojekt/Dungeon.java b/cicaprojekt/Dungeon.java index 2bc7815..79a98ea 100644 --- a/cicaprojekt/Dungeon.java +++ b/cicaprojekt/Dungeon.java @@ -1,35 +1,9 @@ package cicaprojekt; import java.io.*; -import java.util.HashMap; -import java.util.Map; public class Dungeon { - /* NOTE: this function assumes that the parameter input is a well-formatted dungeon file. - * Such file looks like as follows: - * - * x - * - * - * ... - * ... - * > - * - * ---- - * ... - * ... - * ---- - * - * where the map matrix is a matrix of the following chars: - * W: Wall - * F: Field - * Z: Field with a ZMP - * B: Field with a Box - * O: Field with ONeill - * J: Field with Jaffa - * G: Gate - * S: Scale */ - Map buildDungeon(File input) throws IOException + cicaprojekt.Tile buildDungeon(File input) throws IOException { Tile oneilllocation = null; Tile jaffalocation = null; @@ -43,9 +17,8 @@ public class Dungeon { Tile[][] dungeon = new Tile[width][height]; String line = null; - Gate tempgate = new Gate(); - Scale tempscale = new Scale(tempgate, Integer.MAX_VALUE); - int scalecount = 0; + Gate gate = new Gate(); + Gate lastgate = gate; for (int y = 0; y < height; ++y) { line = reader.readLine().replaceAll("\\s",""); // read line and remove whitespaces @@ -63,13 +36,13 @@ public class Dungeon { case 'Z': Field zpmfield = new Field(); - zpmfield.setItemOnTile(new ZPM()); + zpmfield.setZPMOnTile(new ZPM()); dungeon[y][x] = zpmfield; break; case 'B': Field boxfield = new Field(); - boxfield.setItemOnTile(new Box()); + boxfield.putABox(new Box()); dungeon[y][x] = boxfield; break; @@ -86,27 +59,19 @@ public class Dungeon { break; case 'G': - dungeon[y][x] = tempgate; + dungeon[y][x] = gate; + lastgate = gate; + gate = new Gate(); break; case 'S': - dungeon[y][x] = tempscale; - scalecount++; + dungeon[y][x] = new Scale(lastgate, 5); break; } } } - reader.readLine(); // throw empty line away - - for (int i = 0; i < scalecount; ++i) // set up scale-gate connections - { - String[] scaledata = reader.readLine().split("-"); - - dungeon[Integer.parseInt(scaledata[0])][Integer.parseInt(scaledata[1])] = - new Scale((Gate)dungeon[Integer.parseInt(scaledata[2])][Integer.parseInt(scaledata[3])], - Integer.parseInt(scaledata[4])); - } + // NOTE: code seems to be correct till this point based on a debugger run-through /* setting up Tile cross references */ for (int y = 0; y < height; ++y) @@ -135,11 +100,6 @@ public class Dungeon { } } } - - Map playermap = new HashMap<>(); - playermap.put("oneill", oneilllocation); - playermap.put("jaffa", jaffalocation); - - return playermap; + return oneilllocation; } } diff --git a/cicaprojekt/Player.java b/cicaprojekt/Player.java index b266db2..184f159 100644 --- a/cicaprojekt/Player.java +++ b/cicaprojekt/Player.java @@ -13,11 +13,11 @@ public class Player extends PlayerBase{ } public void boxLift() { - boxLifted = (Box) currentTile.getAdjacentTile(facingDirection).removeItemFromTile(); + boxLifted = (Box) currentTile.getAdjacentTile(facingDirection).getABox(); } public void boxDrop() { - currentTile.getAdjacentTile(facingDirection).setItemOnTile(boxLifted); + currentTile.getAdjacentTile(facingDirection).putABox(boxLifted); boxLifted = null; } diff --git a/cicaprojekt/Tile.java b/cicaprojekt/Tile.java index 05f9868..d8da251 100644 --- a/cicaprojekt/Tile.java +++ b/cicaprojekt/Tile.java @@ -12,12 +12,6 @@ public abstract class Tile { public Tile(){ adjacentTile = new HashMap(); - - adjacentTile.put(Direction.NORTH, Field.testField); - adjacentTile.put(Direction.EAST, Field.testField); - adjacentTile.put(Direction.SOUTH, Field.testField); - adjacentTile.put(Direction.WEST, Field.testField); - zpmOnTile = new ZPM(); } public Tile getAdjacentTile(Direction direction) { @@ -34,6 +28,10 @@ public abstract class Tile { public abstract void onExit(PlayerBase playerBase); + public void setZPMOnTile(ZPM zpm) { + zpmOnTile = zpm; + } + public ZPM getZPMFromTile() { ZPM zpm = zpmOnTile; zpmOnTile = null; @@ -41,10 +39,14 @@ public abstract class Tile { } public void putABox(Box box) { + if(box == null) + return; boxStack.push(box); } public Box getABox(){ + if(boxStack.isEmpty()) + return null; return boxStack.pop(); } }