From 349d9084860afe1f705fc4235e3365978a4f7687 Mon Sep 17 00:00:00 2001 From: mike Date: Sun, 5 Mar 2023 20:58:35 -0500 Subject: [PATCH] Created single merged determineType for building args & balanced defense stage costs --- .../src/ca/cosc3p91/a2/game/GameEngine.java | 72 +++++++++---------- .../a2/gameobjects/DefenseStages.java | 12 ++-- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java index 6f6c63c..bf35ed7 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java +++ b/Assignment 2/src/ca/cosc3p91/a2/game/GameEngine.java @@ -12,7 +12,7 @@ import java.io.InputStreamReader; import java.util.Random; import java.util.Scanner; -public class GameEngine implements Runnable { +public class GameEngine implements Runnable { public static final double GOLD_FACTOR = 5; public static final double IRON_FACTOR = 1; @@ -188,7 +188,6 @@ public class GameEngine implements Runnable { ((ResourceBuilding) b).update(this.map.getTownHall()); } } - //System.out.println("Updating"); try { if (rd.ready()) { String in = sc.nextLine(); @@ -200,7 +199,7 @@ public class GameEngine implements Runnable { if (args.length < 2) { System.err.println("Args must include type!"); } else { - Building type = determineType(args[1]); + Building type = (Building)determineType(args[1],new Cannon()); if (type == null) System.err.println("Args are not a valid building!"); else if (this.map.build(new Tile(), type) ) { @@ -212,7 +211,7 @@ public class GameEngine implements Runnable { if (args.length < 2) { System.err.println("Args must include type!"); } else { - Inhabitant type = determineInhabitantType(args[1]); + Inhabitant type = (Inhabitant)determineType(args[1],new Worker()); if (type == null) System.err.println("Args are not a valid inhabitant!"); else if (this.map.train(type) ) { @@ -237,43 +236,40 @@ public class GameEngine implements Runnable { } } - private static Building determineType(String building){ - building = building.toLowerCase(); + private static Object determineType(String argument, T type){ + argument = argument.toLowerCase(); char c = ' '; - if (building.trim().length() == 1) - c = building.charAt(0); - if (building.contains("gold") || building.contains("good") || c == 'g') { - return new SaulGoodMine(ResourceStages.goldStages[0]); - } else if (building.contains("iron") || c == 'i') { - return new IronMine(ResourceStages.ironStages[0]); - } else if (building.contains("wood") || building.contains("lumber") || c == 'w' || c == 'l') { - return new LumberMine(ResourceStages.woodStages[0]); - } else if (building.contains("archer") || c == 'a') { - return new ArcherTower(); - } else if (building.contains("can") || c == 'c'){ - return new Cannon(); + if (argument.trim().length() == 1) + c = argument.charAt(0); + + if (type instanceof Building) { + 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(); + } + } else if (type instanceof Inhabitant) { + if (argument.contains("soldier") || argument.contains("sold") || 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; } - private static Inhabitant determineInhabitantType(String inhabitant){ - inhabitant = inhabitant.toLowerCase(); - char c = ' '; - if (inhabitant.trim().length() == 1) - c = inhabitant.charAt(0); - if (inhabitant.contains("soldier") || inhabitant.contains("sold") || c == 's') { - return new Soldier(); - } else if (inhabitant.contains("knight") || c == 'k') { - return new Knight(); - } else if (inhabitant.contains("work")|| c == 'w') { - return new Worker(); - } else if (inhabitant.contains("collect") || c == 'c') { - return new Collector(); - } else if (inhabitant.contains("cat")){ - return new Catapult(); - } else if (inhabitant.contains("archer") || c == 'a'){ - return new Archer(); - } - return null; - } } diff --git a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java index c1aba1e..a13f8c0 100644 --- a/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java +++ b/Assignment 2/src/ca/cosc3p91/a2/gameobjects/DefenseStages.java @@ -9,37 +9,37 @@ public class DefenseStages { public static class ArcherTowerStage1 extends DefenseStage { public ArcherTowerStage1() { - super(100, 0, 0, new Time().offsetMinutes(1), 25, 250, 4,6); + super(100, 0, 0, new Time().offsetMinutes(1), 25, 75, 4,6); } } public static class ArcherTowerStage2 extends DefenseStage { public ArcherTowerStage2() { - super(150, 0, 1, new Time().offsetMinutes(15), 25, 250, 8,8); + super(150, 50, 1, new Time().offsetMinutes(15), 25, 200, 8,8); } } public static class ArcherTowerStage3 extends DefenseStage { public ArcherTowerStage3() { - super(200, 0, 2, new Time().offsetHours(1), 25, 250, 12,12); + super(200, 100, 2, new Time().offsetHours(1), 50, 300, 12,12); } } public static class CannonStage1 extends DefenseStage { public CannonStage1() { - super(125, 0, 0, new Time().offsetMinutes(2), 25, 250, 8,4); + super(125, 0, 0, new Time().offsetMinutes(2), 100, 25, 8,4); } } public static class CannonStage2 extends DefenseStage { public CannonStage2() { - super(175, 0, 1, new Time().offsetMinutes(20), 25, 250, 12,6); + super(175, 50, 1, new Time().offsetMinutes(20), 125, 25, 12,6); } } public static class CannonStage3 extends DefenseStage { public CannonStage3() { - super(225, 0, 2, new Time().offsetHours(1), 25, 250, 14,8); + super(225, 100, 2, new Time().offsetHours(1), 200, 50, 14,8); } }