diff --git a/data/enemies.dat b/data/enemies.dat new file mode 100644 index 0000000..dd529da --- /dev/null +++ b/data/enemies.dat @@ -0,0 +1,4 @@ +Enemy Name: Henry Pelatt +Description: A guy +Starting Room: Circle Room +Damage Given: 25 \ No newline at end of file diff --git a/data/words.dat b/data/words.dat index fc4e942..5142139 100644 --- a/data/words.dat +++ b/data/words.dat @@ -56,4 +56,5 @@ towels, item open, verb i, verb unlock, verb -drop, verb \ No newline at end of file +drop, verb +HenryPelatt, enemy \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Combat.java b/src/com/bayviewglen/zork/Combat.java new file mode 100644 index 0000000..65707c7 --- /dev/null +++ b/src/com/bayviewglen/zork/Combat.java @@ -0,0 +1,12 @@ +package com.bayviewglen.zork; + +import com.bayviewglen.zork.Entities.Entity; + +public class Combat { + private Entity player; + private Entity enemy; + public Combat(Entity player, Entity enemy) { + this.player = player; + this.enemy = enemy; + } +} diff --git a/src/com/bayviewglen/zork/Command.java b/src/com/bayviewglen/zork/Command.java index ead2e74..009b8b5 100644 --- a/src/com/bayviewglen/zork/Command.java +++ b/src/com/bayviewglen/zork/Command.java @@ -28,13 +28,14 @@ class Command { private String direction; private ArrayList otherWords; private String item; + private String enemy; /** * Create a command object. First and second word must be supplied, but * either one (or both) can be null. The command word should be null to * indicate that this was a command that is not recognised by this game. */ - public Command(String firstWord, ArrayList otherWords, String direction, String item) { + public Command(String firstWord, ArrayList otherWords, String direction, String item, String enemy) { commandWord = firstWord; this.otherWords = otherWords; this.direction = direction; @@ -51,6 +52,7 @@ class Command { if(direction.equals("d")) this.direction = "down"; this.item = item; + this.enemy = enemy; } /** @@ -100,4 +102,10 @@ class Command { public String getItem() { return item; } + public String getEnemy() { + return enemy; + } + public boolean hasEnemy() { + return !enemy.equals(""); + } } diff --git a/src/com/bayviewglen/zork/CommandWords.java b/src/com/bayviewglen/zork/CommandWords.java index 70c57e0..64bc734 100644 --- a/src/com/bayviewglen/zork/CommandWords.java +++ b/src/com/bayviewglen/zork/CommandWords.java @@ -64,6 +64,13 @@ class CommandWords { } } + public static boolean isEnemy(String aString) { + try { + return m_words.get(aString).equals("enemy"); + } catch(Exception e) { + return false; + } + } /* * Print all valid commands to System.out. */ diff --git a/src/com/bayviewglen/zork/Entities/Enemies/Enemy.java b/src/com/bayviewglen/zork/Entities/Enemies/Enemy.java new file mode 100644 index 0000000..2e9a75f --- /dev/null +++ b/src/com/bayviewglen/zork/Entities/Enemies/Enemy.java @@ -0,0 +1,45 @@ +package com.bayviewglen.zork.Entities.Enemies; + +import com.bayviewglen.zork.Entities.Entity; + +public class Enemy extends Entity{ + private int damageGiven; + private String name; + private String description; + private String room; + + public Enemy(){ + super(100.0, 100.0); + damageGiven = 10; + } + + public Enemy(int damageGiven){ + super(100.0, 100.0); + this.damageGiven = damageGiven; + } + + public void setName(String name) { + this.name = name; + } + + public void setDescription(String desc) { + this.description = desc; + } + + public void setRoom(String room) { + this.room = room; + } + + public String getName() { + return name; + } + public String getDescription() { + return description; + } + public String getRoom() { + return room; + } + public void setDamageGiven(int damageGiven) { + this.damageGiven = damageGiven; + } +} diff --git a/src/com/bayviewglen/zork/Entities/Enemy.java b/src/com/bayviewglen/zork/Entities/Enemy.java deleted file mode 100644 index 08b44a7..0000000 --- a/src/com/bayviewglen/zork/Entities/Enemy.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.bayviewglen.zork.Entities; - -public class Enemy { - int damageGiven; - - public Enemy(){ - super(); - damageGiven = 10; - } - - public Enemy(int damageGiven){ - super(); - this.damageGiven = damageGiven; - } - -} diff --git a/src/com/bayviewglen/zork/Game.java b/src/com/bayviewglen/zork/Game.java index 99a0b41..2fa92fe 100644 --- a/src/com/bayviewglen/zork/Game.java +++ b/src/com/bayviewglen/zork/Game.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.Scanner; import com.bayviewglen.zork.Entities.Player; +import com.bayviewglen.zork.Entities.Enemies.Enemy; import com.bayviewglen.zork.Items.*; /** @@ -37,6 +38,8 @@ class Game { // masterRoomMap.get("GREAT_ROOM") will return the Room Object that is the // Great Room (assuming you have one). private HashMap masterRoomMap; + private HashMap masterEnemyMap; + private Combat currentCombat = null; //private HashMap itemsInRooms = new HashMap(); private void initRooms(String fileName) throws Exception { @@ -108,6 +111,34 @@ class Game { e.printStackTrace(); } } + + private void initEnemies(String fileName) throws Exception { + masterEnemyMap = new HashMap(); + Scanner enemyScanner = null; + Enemy e = null; + try { + enemyScanner = new Scanner(new File(fileName)); + + while (enemyScanner.hasNext()) { + e = new Enemy(); + // Read the Name + String enemyName = enemyScanner.nextLine(); + e.setName(enemyName.split(":")[1].trim()); + // Read the Description + String enemyDescription = enemyScanner.nextLine(); + e.setDescription(enemyDescription.split(":")[1].replaceAll("
", "\n").trim()); + // Read the Room + String startingRoom = enemyScanner.nextLine(); + e.setRoom(startingRoom.split(":")[1].trim()); + // Read the Damage Given + int damageGiven = Integer.parseInt(enemyScanner.nextLine().split(":")[1].trim()); + e.setDamageGiven(damageGiven); + } + }catch(Exception ex) { + } + masterEnemyMap.put(e, e.getRoom()); + enemyScanner.close(); + } /** * Create the game and initialise its internal map. @@ -115,6 +146,7 @@ class Game { public Game() { try { initRooms("data/rooms.dat"); + initEnemies("data/enemies.dat"); currentRoom = masterRoomMap.get("CIRCLE_ROOM"); } catch (Exception e) { e.printStackTrace(); @@ -135,7 +167,8 @@ class Game { System.out.println("\nType 'help' if you need help, consult the wiki \non GitHub if you are confused and enjoy the game!\n"); System.out.println("\n\nEscape Casa Loma: A text-based escape game"); System.out.println("---------------------\n"); - System.out.println(currentRoom.longDescription()); + System.out.print(currentRoom.longDescription()); + System.out.println(currentRoom.exitString()); boolean finished = false; while (!finished) { Command command = parser.getCommand(); @@ -325,6 +358,23 @@ class Game { System.out.println("Drop what?"); } break; + case "attack": + if(currentCombat == null) { + if(command.hasEnemy()) { + Class clazz; + Enemy object; + try { + clazz = Class.forName("com.bayviewglen.zork.Enemies." + command.getEnemy().substring(0, 1).toUpperCase().trim() + command.getEnemy().substring(1).trim()); + Constructor ctor = clazz.getConstructor(); + object = (Enemy) ctor.newInstance(); + currentCombat = new Combat(player, object); + }catch(Exception e) { + + } + } + + } + break; default: return false; } @@ -361,8 +411,28 @@ class Game { } else { currentRoom = nextRoom; - System.out.println(currentRoom.longDescription()); + System.out.print(currentRoom.longDescription()); + boolean hasEnemy = false; + Enemy enemy = null; + String room = ""; + for (String i : masterEnemyMap.values()) { + if(currentRoom.getRoomName().equals(i)) { + hasEnemy = true; + room = i; + } + } + for (Enemy i : masterEnemyMap.keySet()) { + if(masterEnemyMap.get(i).equals(room)) { + enemy = i; + } + } + if(hasEnemy) { + System.out.println(enemy.getName() + ", " + enemy.getDescription() + " has appeared!"); + System.out.println(currentRoom.exitString()); + }else { + System.out.println(currentRoom.exitString()); + } } } - + } \ No newline at end of file diff --git a/src/com/bayviewglen/zork/Parser.java b/src/com/bayviewglen/zork/Parser.java index f0c34df..2c429e7 100644 --- a/src/com/bayviewglen/zork/Parser.java +++ b/src/com/bayviewglen/zork/Parser.java @@ -35,6 +35,7 @@ class Parser { String verb = ""; String direction = ""; String item = ""; + String enemy = ""; boolean open = false; //String word2; ArrayList words = new ArrayList(); @@ -61,6 +62,8 @@ class Parser { } }else if(CommandWords.isItem(words.get(i))){ item = words.get(i); + }else if(CommandWords.isEnemy(words.get(i))){ + enemy = words.get(i); }else{ otherWords.add(words.get(i)); } @@ -68,11 +71,11 @@ class Parser { //System.out.println(verb); if (CommandWords.isCommand(verb)) if(!open) - return new Command(verb, otherWords, direction, item); + return new Command(verb, otherWords, direction, item, enemy); else - return new Command("open", otherWords, direction, item); + return new Command("open", otherWords, direction, item, enemy); else - return new Command(null, otherWords, direction, item); + return new Command(null, otherWords, direction, item, enemy); } /** diff --git a/src/com/bayviewglen/zork/Room.java b/src/com/bayviewglen/zork/Room.java index eb1b14f..93bf97f 100644 --- a/src/com/bayviewglen/zork/Room.java +++ b/src/com/bayviewglen/zork/Room.java @@ -158,14 +158,14 @@ class Room { */ public String longDescription() { - return "Room: " + roomName + "\n\n" + description + "\n" + exitString(); + return "Room: " + roomName + "\n\n" + description + "\n"; } /** * Return a string describing the room's exits, for example "Exits: north * west ". */ - private String exitString() { + public String exitString() { String returnString = "Exits:"; Set keys = exits.keySet(); for (Iterator iter = keys.iterator(); iter.hasNext();)