diff --git a/Assignment 3/COSC3P91-A2_Description_Brett&Michael.pdf b/Assignment 3/COSC3P91-A2_Description_Brett&Michael.pdf deleted file mode 100644 index 4747bef..0000000 Binary files a/Assignment 3/COSC3P91-A2_Description_Brett&Michael.pdf and /dev/null differ diff --git a/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java index 19457e3..f47122b 100644 --- a/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 3/src/ca/cosc3p91/a2/game/GameEngine.java @@ -1,6 +1,8 @@ package ca.cosc3p91.a2.game; import ca.cosc3p91.a2.gameobjects.*; +import ca.cosc3p91.a2.gameobjects.factory.BuildingFactory; +import ca.cosc3p91.a2.gameobjects.factory.InhabitantFactory; import ca.cosc3p91.a2.player.*; import ca.cosc3p91.a2.util.Print; import ca.cosc3p91.a2.util.Time; @@ -227,7 +229,8 @@ public class GameEngine implements Runnable { if (args.length < 2) { System.err.println("Args must include type!"); } else { - Building type = determineBuildingType(args[1]); + BuildingFactory bfactory = new BuildingFactory(); + Building type = bfactory.getBuilding(args[1]); if (type == null) System.err.println("Args are not a valid building!"); else if (this.map.build(new Tile(), type) ) { @@ -240,7 +243,8 @@ public class GameEngine implements Runnable { if (args.length < 2) { System.err.println("Args must include type!"); } else { - Inhabitant type = determineInhabitantType(args[1]); + InhabitantFactory ifactory = new InhabitantFactory(); + Inhabitant type = ifactory.getInhabitant(args[1]); if (type == null) System.err.println("Args are not a valid inhabitant!"); else if (this.map.train(type) ) { @@ -302,51 +306,4 @@ public class GameEngine implements Runnable { } } - private static char determineChar(String str){ - char c = ' '; - if (str.trim().length() == 1) - c = str.charAt(0); - return c; - } - - private static Building determineBuildingType(String argument){ - argument = argument.toLowerCase(); - char c = determineChar(argument); - - if (argument.contains("gold") || argument.contains("good") || c == 'g') { - return new SaulGoodMine(ResourceStages.goldStages[0]); - } else if (argument.contains("iron") || c == 'i') { - return new IronMine(ResourceStages.ironStages[0]); - } else if (argument.contains("wood") || argument.contains("lumber") || c == 'w' || c == 'l') { - return new LumberMine(ResourceStages.woodStages[0]); - } else if (argument.contains("archer") || c == 'a') { - return new ArcherTower(); - } else if (argument.contains("can") || c == 'c') { - return new Cannon(); - } - - return null; - } - - private static Inhabitant determineInhabitantType(String argument) { - argument = argument.toLowerCase(); - char c = determineChar(argument); - - if (argument.contains("soldier") || c == 's') { - return new Soldier(); - } else if (argument.contains("knight") || c == 'k') { - return new Knight(); - } else if (argument.contains("work") || c == 'w') { - return new Worker(); - } else if (argument.contains("collect") || c == 'c') { - return new Collector(); - } else if (argument.contains("cat")) { - return new Catapult(); - } else if (argument.contains("arch") || c == 'a') { - return new Archer(); - } - - return null; - } - } diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/factory/BuildingFactory.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/factory/BuildingFactory.java new file mode 100644 index 0000000..ef9af9d --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/factory/BuildingFactory.java @@ -0,0 +1,25 @@ +package ca.cosc3p91.a2.gameobjects.factory; + +import ca.cosc3p91.a2.gameobjects.*; +import ca.cosc3p91.a2.util.Util; + +public class BuildingFactory { + public Building getBuilding(String buildingName) { + buildingName = buildingName.toLowerCase(); + char c = Util.getFirstChar(buildingName); + + if (buildingName.contains("gold") || buildingName.contains("good") || c == 'g') { + return new SaulGoodMine(ResourceStages.goldStages[0]); + } else if (buildingName.contains("iron") || c == 'i') { + return new IronMine(ResourceStages.ironStages[0]); + } else if (buildingName.contains("wood") || buildingName.contains("lumber") || c == 'w' || c == 'l') { + return new LumberMine(ResourceStages.woodStages[0]); + } else if (buildingName.contains("archer") || c == 'a') { + return new ArcherTower(); + } else if (buildingName.contains("can") || c == 'c') { + return new Cannon(); + } + + return null; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/gameobjects/factory/InhabitantFactory.java b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/factory/InhabitantFactory.java new file mode 100644 index 0000000..327ffd7 --- /dev/null +++ b/Assignment 3/src/ca/cosc3p91/a2/gameobjects/factory/InhabitantFactory.java @@ -0,0 +1,27 @@ +package ca.cosc3p91.a2.gameobjects.factory; + +import ca.cosc3p91.a2.gameobjects.*; +import ca.cosc3p91.a2.util.Util; + +public class InhabitantFactory { + public Inhabitant getInhabitant(String inhabitantName) { + inhabitantName = inhabitantName.toLowerCase(); + char c = Util.getFirstChar(inhabitantName); + + if (inhabitantName.contains("soldier") || c == 's') { + return new Soldier(); + } else if (inhabitantName.contains("knight") || c == 'k') { + return new Knight(); + } else if (inhabitantName.contains("work") || c == 'w') { + return new Worker(); + } else if (inhabitantName.contains("collect") || c == 'c') { + return new Collector(); + } else if (inhabitantName.contains("cat")) { + return new Catapult(); + } else if (inhabitantName.contains("arch") || c == 'a') { + return new Archer(); + } + + return null; + } +} diff --git a/Assignment 3/src/ca/cosc3p91/a2/util/Util.java b/Assignment 3/src/ca/cosc3p91/a2/util/Util.java index e909ac0..7d7c5c8 100644 --- a/Assignment 3/src/ca/cosc3p91/a2/util/Util.java +++ b/Assignment 3/src/ca/cosc3p91/a2/util/Util.java @@ -12,4 +12,11 @@ public class Util { return Math.min(Math.max(i, min), max); } + public static char getFirstChar(String str){ + char c = ' '; + if (str.trim().length() == 1) + c = str.charAt(0); + return c; + } + } diff --git a/untitled104.iml b/untitled104.iml index 61baa9c..af7f350 100644 --- a/untitled104.iml +++ b/untitled104.iml @@ -3,7 +3,7 @@ - +